| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + xunits=None and yunits=None passed as kwargs are treated as "no action" | ||
| 2 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | + | ||
| 4 | + Many (but not all) of the methods on `.Axes` take the (undocumented) | ||
| 5 | + kwargs *xunits* and *yunits* that will update the units on the given | ||
| 6 | + Axis by calling `.Axis.set_units` and `.Axis.update_units`. | ||
| 7 | + | ||
| 8 | + Previously if `None` was passed it would clear the value stored in | ||
| 9 | + :py:attr:`.Axis.units` which will in turn break converters (notable | ||
| 10 | + `.StrCategoryConverter`) which rely on the value in | ||
| 11 | + :py:attr:`.Axis.units` to work properly. | ||
| 12 | + | ||
| 13 | + This changes the semantics of ``ax.meth(..., xunits=None, | ||
| 14 | + yunits=None)`` from "please clear the units" to "do the default thing | ||
| 15 | + as if they had not been passed" which is consistent with the standard | ||
| 16 | + behavior of Matplotlib keyword arguments. | ||
| 17 | + | ||
| 18 | + If you were relying on passing ``xuints=None`` to plotting methods to | ||
| 19 | + clear the `.Axes.units` attribute, directly call `.Axis.set_units` (and | ||
| 20 | + `.Axis.update_units` if you also require the converter to be updated). | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2172,7 +2172,15 @@ def update_datalim_bounds(self, bounds): | |||
| 2172 | 2172 | self.dataLim.set(mtransforms.Bbox.union([self.dataLim, bounds])) | |
| 2173 | 2173 | ||
| 2174 | 2174 | def _process_unit_info(self, xdata=None, ydata=None, kwargs=None): | |
| 2175 | - """Look for unit *kwargs* and update the axis instances as necessary""" | ||
| 2175 | + """ | ||
| 2176 | + Look for unit *kwargs* and update the axis instances as necessary | ||
| 2177 | + | ||
| 2178 | + | ||
| 2179 | + .. warning :: | ||
| 2180 | + | ||
| 2181 | + This method may mutate the dictionary passed in an kwargs and | ||
| 2182 | + the Axes instances attached to this Axes. | ||
| 2183 | + """ | ||
| 2176 | 2184 | ||
| 2177 | 2185 | def _process_single_axis(data, axis, unit_name, kwargs): | |
| 2178 | 2186 | # Return if there's no axis set | |
@@ -2188,10 +2196,12 @@ def _process_single_axis(data, axis, unit_name, kwargs): | |||
| 2188 | 2196 | if kwargs is not None: | |
| 2189 | 2197 | units = kwargs.pop(unit_name, axis.units) | |
| 2190 | 2198 | if self.name == 'polar': | |
| 2199 | + # handle special casing to allow the kwargs | ||
| 2200 | + # thetaunits and runits to be used with polar | ||
| 2191 | 2201 | polar_units = {'xunits': 'thetaunits', 'yunits': 'runits'} | |
| 2192 | 2202 | units = kwargs.pop(polar_units[unit_name], units) | |
| 2193 | 2203 | ||
| 2194 | - if units != axis.units: | ||
| 2204 | + if units != axis.units and units is not None: | ||
| 2195 | 2205 | axis.set_units(units) | |
| 2196 | 2206 | # If the units being set imply a different converter, | |
| 2197 | 2207 | # we need to update. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ | |||
| 5 | 5 | from matplotlib.axes import Axes | |
| 6 | 6 | import matplotlib.pyplot as plt | |
| 7 | 7 | import matplotlib.category as cat | |
| 8 | + from matplotlib.testing.decorators import check_figures_equal | ||
| 8 | 9 | ||
| 9 | 10 | ||
| 10 | 11 | class TestUnitData: | |
@@ -269,3 +270,28 @@ def test_mixed_type_update_exception(self, ax, plotter, xdata): | |||
| 269 | 270 | with pytest.raises(TypeError): | |
| 270 | 271 | plotter(ax, [0, 3], [1, 3]) | |
| 271 | 272 | plotter(ax, xdata, [1, 2]) | |
| 273 | + | ||
| 274 | + | ||
| 275 | + @pytest.mark.style('default') | ||
| 276 | + @check_figures_equal(extensions=["png"]) | ||
| 277 | + def test_overriding_units_in_plot(fig_test, fig_ref): | ||
| 278 | + from datetime import datetime | ||
| 279 | + | ||
| 280 | + t0 = datetime(2018, 3, 1) | ||
| 281 | + t1 = datetime(2018, 3, 2) | ||
| 282 | + t2 = datetime(2018, 3, 3) | ||
| 283 | + t3 = datetime(2018, 3, 4) | ||
| 284 | + | ||
| 285 | + ax_test = fig_test.subplots() | ||
| 286 | + ax_ref = fig_ref.subplots() | ||
| 287 | + for ax, kwargs in zip([ax_test, ax_ref], | ||
| 288 | + ({}, dict(xunits=None, yunits=None))): | ||
| 289 | + # First call works | ||
| 290 | + ax.plot([t0, t1], ["V1", "V2"], **kwargs) | ||
| 291 | + x_units = ax.xaxis.units | ||
| 292 | + y_units = ax.yaxis.units | ||
| 293 | + # this should not raise | ||
| 294 | + ax.plot([t2, t3], ["V1", "V2"], **kwargs) | ||
| 295 | + # assert that we have not re-set the units attribute at all | ||
| 296 | + assert x_units is ax.xaxis.units | ||
| 297 | + assert y_units is ax.yaxis.units | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments