Skip to content

Commit 2c6bd2d

Browse files
dcherianshoyer
authored andcommitted
Prevent Inf from screwing colorbar scale. (#2120)
pd.isnull([np.inf]) is True while np.isfinite([np.inf]) is False. Let's use the latter.
1 parent a525405 commit 2c6bd2d

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ Bug fixes
6565
By `Deepak Cherian <https://github.com/dcherian>`_.
6666
- ``plot.line()`` learned new kwargs: ``xincrease``, ``yincrease`` that change the direction of the respective axes.
6767
By `Deepak Cherian <https://github.com/dcherian>`_.
68+
- Colorbar limits are now determined by excluding ±Infs too.
69+
By `Deepak Cherian <https://github.com/dcherian>`_.
6870

6971
.. _whats-new.0.10.3:
7072

xarray/plot/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def _determine_cmap_params(plot_data, vmin=None, vmax=None, cmap=None,
160160
"""
161161
import matplotlib as mpl
162162

163-
calc_data = np.ravel(plot_data[~pd.isnull(plot_data)])
163+
calc_data = np.ravel(plot_data[np.isfinite(plot_data)])
164164

165165
# Handle all-NaN input data gracefully
166166
if calc_data.size == 0:

xarray/tests/test_plot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,15 @@ def test_center(self):
426426
assert cmap_params['levels'] is None
427427
assert cmap_params['norm'] is None
428428

429+
def test_nan_inf_are_ignored(self):
430+
cmap_params1 = _determine_cmap_params(self.data)
431+
data = self.data
432+
data[50:55] = np.nan
433+
data[56:60] = np.inf
434+
cmap_params2 = _determine_cmap_params(data)
435+
assert cmap_params1['vmin'] == cmap_params2['vmin']
436+
assert cmap_params1['vmax'] == cmap_params2['vmax']
437+
429438
@pytest.mark.slow
430439
def test_integer_levels(self):
431440
data = self.data + 1

0 commit comments

Comments
 (0)