Skip to content

Commit

Permalink
Merge pull request #3028 from wxmann/support-floats-in-image-range
Browse files Browse the repository at this point in the history
Support floats in image_range
  • Loading branch information
dopplershift committed May 3, 2023
2 parents 8f2a753 + a5c4656 commit 0c7ce09
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
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)

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'
)
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

0 comments on commit 0c7ce09

Please sign in to comment.