Skip to content

Commit

Permalink
Xlim single value warning (#637)
Browse files Browse the repository at this point in the history
* Updated method name for call to remove warning

* Checking if xlim values are the same. If so add time to expand so a warning is not raised.

* Adding both new and old method for backward compatability.

* Updated test to allow for taking longer.
  • Loading branch information
kenkehoe authored Mar 13, 2023
1 parent cf01a53 commit e6f779a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
5 changes: 4 additions & 1 deletion act/plotting/act_cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,7 @@ def _generate_cmap(name, lutsize):
# register the colormaps so that they can be accessed with the names act_XXX
for name, cmap in cmap_d.items():
full_name = 'act_' + name
mpl.cm.register_cmap(name=full_name, cmap=cmap)
try:
matplotlib.colormaps.register(name=full_name, cmap=cmap, force=True)
except AttributeError:
matplotlib.cm.register_cmap(name=full_name, cmap=cmap)
14 changes: 14 additions & 0 deletions act/plotting/timeseriesdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,20 @@ def set_xrng(self, xrng, subplot_index=(0,)):
if self.axes is None:
raise RuntimeError('set_xrng requires the plot to be displayed.')

# If the xlim is set to the same value for range it will throw a warning
# This is to catch that and expand the range so we avoid the warning.
if xrng[0] == xrng[1]:
if isinstance(xrng[0], np.ndarray) and np.issubdtype(xrng[0].dtype, np.datetime64):
print(f'\nAttempting to set xlim range to single value {xrng[0]}. '
'Expanding range by 2 seconds.\n')
xrng[0] = xrng[0] - np.timedelta64(1, 's')
xrng[1] = xrng[1] + np.timedelta64(1, 's')
elif isinstance(xrng[0], dt.datetime):
print(f'\nAttempting to set xlim range to single value {xrng[0]}. '
'Expanding range by 2 seconds.\n')
xrng[0] = xrng[0] - dt.timedelta(seconds=1)
xrng[1] = xrng[1] + dt.timedelta(seconds=1)

self.axes[subplot_index].set_xlim(xrng)

# Make sure that the xrng value is a numpy array not pandas
Expand Down
Binary file added act/tests/baseline/test_xlim_correction_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions act/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,3 +1157,16 @@ def test_groupby_plot():
display.axes[i, j].tick_params(pad=-20)
ds.close()
return display.fig


@pytest.mark.mpl_image_compare(tolerance=30)
def test_xlim_correction_plot():
ds = arm.read_netcdf(sample_files.EXAMPLE_MET1)

# Plot data
xrng = [datetime(2019, 1, 1, 0, 0, 0), datetime(2019, 1, 1, 0, 0, 0)]
display = TimeSeriesDisplay(ds)
display.plot('temp_mean', time_rng=xrng)

ds.close()
return display.fig
2 changes: 1 addition & 1 deletion act/tests/test_qc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ def test_qc_speed():
ds.qcfilter.add_test(name, index=failed_qc, test_meaning='Value above threshold')

time_diff = datetime.utcnow() - start
assert time_diff.seconds <= 3
assert time_diff.seconds <= 4


@pytest.mark.skipif(not PYSP2_AVAILABLE, reason="PySP2 is not installed.")
Expand Down

0 comments on commit e6f779a

Please sign in to comment.