Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support floats in image_range #3028

Merged
merged 3 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/metpy/plots/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,9 +1043,12 @@ def _cmap_obj(self):
def _norm_obj(self):
"""Return the normalization object.

Converts the tuple image range to a matplotlib normalization instance.
If `image_range` is a matplotlib normalization instance, returns it, otherwise converts
the tuple image range to a matplotlib normalization instance.

"""
if isinstance(self.image_range, plt.Normalize):
return self.image_range
return plt.Normalize(*self.image_range)
dopplershift marked this conversation as resolved.
Show resolved Hide resolved

def clear(self):
Expand Down Expand Up @@ -1302,7 +1305,7 @@ class ColorfillTraits(MetPyHasTraits):
For example, the Blue-Purple colormap from Matplotlib can be accessed using 'BuPu'.
"""

image_range = Union([Tuple(Int(allow_none=True), Int(allow_none=True)),
image_range = Union([Tuple(Float(allow_none=True), Float(allow_none=True)),
Instance(plt.Normalize)], default_value=(None, None))
image_range.__doc__ = """A tuple of min and max values that represent the range of values
to color the rasterized image.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions tests/plots/test_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from io import BytesIO
import warnings

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytest
Expand Down Expand Up @@ -613,6 +614,60 @@ def test_colorfill(cfeature):
return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.0062)
def test_colorfill_with_image_range(cfeature):
"""Test that we can use ContourFillPlot with image_range bounds."""
data = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))

contour = FilledContourPlot()
contour.data = data
contour.level = 700 * units.hPa
contour.field = 'Temperature'
contour.colormap = 'coolwarm'
contour.colorbar = None
contour.image_range = (273.15, 350)

panel = MapPanel()
panel.area = (-110, -60, 25, 55)
panel.layers = [cfeature.STATES]
panel.plots = [contour]

pc = PanelContainer()
pc.panel = panel
pc.size = (8, 8)
pc.draw()

return pc.figure


@pytest.mark.mpl_image_compare(
remove_text=True, tolerance=0.0062, filename='test_colorfill_with_image_range.png'
dopplershift marked this conversation as resolved.
Show resolved Hide resolved
)
def test_colorfill_with_normalize_instance_image_range(cfeature):
"""Test that we can use ContourFillPlot with image_range bounds."""
data = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))

contour = FilledContourPlot()
contour.data = data
contour.level = 700 * units.hPa
contour.field = 'Temperature'
contour.colormap = 'coolwarm'
contour.colorbar = None
contour.image_range = plt.Normalize(vmin=273.15, vmax=350)

panel = MapPanel()
panel.area = (-110, -60, 25, 55)
panel.layers = [cfeature.STATES]
panel.plots = [contour]

pc = PanelContainer()
pc.panel = panel
pc.size = (8, 8)
pc.draw()

return pc.figure


@pytest.mark.mpl_image_compare(remove_text=True, tolerance=0.)
def test_colorfill_horiz_colorbar(cfeature):
"""Test that we can use ContourFillPlot with a horizontal colorbar."""
Expand Down