FIX: (broken)bar(h) math before units#12903
Conversation
60264b2 to
01f85af
Compare
|
I like the fix. Any reason not to add your example code as a test? |
59deaf4 to
3e8129b
Compare
Yep, done! |
a37c072 to
98be140
Compare
45cf538 to
39c592f
Compare
|
EDIT: ooops, had an errant print statement in there what was being appended to the end of each of the stdout streams, and that was different each time. The files themselves are identical. 🙄 |
39c592f to
1792e48
Compare
1792e48 to
53458cc
Compare
|
I like the point about |
|
That was a test that failed my original change but now passes, so yay tests! But I think that case is covered in test_category |
|
Cool, never mind then. |
lib/matplotlib/axes/_axes.py
Outdated
| x0 = x | ||
| x = self.convert_xunits(x) | ||
| width = self.convert_xunits(width) | ||
| width = self._convert_dx(x0, x, width, self.convert_xunits) |
There was a problem hiding this comment.
Just don't convert x on the line above, and let convert_dx do the conversion of it (and return it as well if you're worried about the performance of converting twice)? It looks a bit weird that convert_dx takes unitized first and third args, but already converted second arg.
There was a problem hiding this comment.
I did it for performance reasons (both unitized and non unitized are needed for xerr as well. ). But I have no evidence the call takes any significant time.
QuLogic
left a comment
There was a problem hiding this comment.
Some alignment issues; also needs a rebase.
53458cc to
78b1bfe
Compare
TST: Add test of broken_barh FIX: make bar work with timedeltas TST: check bar and barh work with timedelta DOC: fix private method docstring FIX: revert list comprehension TST: fix test FIX: throw error xr not tw-tuple
2c7e237 to
1dfdc29
Compare
lib/matplotlib/axes/_axes.py
Outdated
| else: | ||
| raise ValueError('invalid orientation: %s' % orientation) | ||
|
|
||
| x, height, width, y, linewidth = np.broadcast_arrays( |
There was a problem hiding this comment.
Should this go below unit conversion? Otherwise I think units will be dropped if width is a list of unitized stuff?
There was a problem hiding this comment.
y = [1 ,2, 4]
w = [timedelta(hours=2), timedelta(hours=3), timedelta(hours=3)]
print(np.broadcast_arrays(y, w))I don't think this drops units.
[array([1, 2, 4]), array([datetime.timedelta(seconds=7200),
datetime.timedelta(seconds=10800),
datetime.timedelta(seconds=10800)], dtype=object)]
There was a problem hiding this comment.
This will drop stuff like pint units or astropy units.
I remember some discussion a while ago which basically reached the conclusion that we cannot call any numpy function before unit handling (tbh I was rather unhappy about that, but the goal is not to relitigate this here), perhaps @dopplershift will remember this better.
There was a problem hiding this comment.
Can someone provide an example? This works fine for all the categorical, unit, and date tests. If we are supporting something that doesn’t survive broadcasting we need to start testing it.
I’m not sure why we broadcast here at all anyway.
There was a problem hiding this comment.
AFAICS, broadcasting is used to support scalar parameters such as plt.bar([1, 2, 3], [3,5,2], width=0.5)
There was a problem hiding this comment.
For the record, broadcast_arrays will drop pint units--it might possibly work with astropy since it's an ndarray subclass. Broadcasting itself works fine, it's calling functions that need to allocate new arrays that's the problem; there are currently no hooks to allow overriding what type to use when allocating an empty array.
There was a problem hiding this comment.
Agreed - sorry, should have responded here - I did move the broadcast after the convert...
|
Discussion on units and numpy functions pending. Otherwise this looks good. |
|
See e.g. #7562 (review), #7562 (comment) for units discussion; some comments are also present e.g. in the errorbar() implementation. (Personally, I think "not being able to use numpy functions at all before units handling" is really not a great policy, but heh...) |
|
Yes, this is truly a PITA But see latest for an attempt to deal with all the goofy cases (empty arguments, wrapped arguments, singletons, numpy arrays; am I missing any?) |
|
So, can we introduce a testing dependency like pandas for pint so we can have an example of a units package that wraps? They have matplotlib units registry. We can of course skip the test if pint won't install. I think pint is pretty lightweight. W/o that example, its pretty hard to test that we don't break things. |
|
Let's not relitigate this here; there have been extremely long threads elsewhere already. |
|
Relitigate having |
|
General unit support strategy. |
import matplotlib.pyplot as plt
import numpy as np
import pint
ureg = pint.UnitRegistry()
ureg.setup_matplotlib(True)
x = [1., 2] * ureg.cm
height = [10, 20]
#ax.bar(datetime(2018, 1, 1), 1,
# width=timedelta(hours=3))
fig, ax = plt.subplots()
ax.bar(x, height, width=[.2] * ureg.cm)
plt.show()works fine.
fails with I think thats on I'll note that ax.plot(0.1 * ureg.cm, 1, 'd')works, but gives a |
|
@jklymak For future reference for testing, matplotlib/lib/matplotlib/tests/test_units.py Lines 12 to 37 in 4a85eb5 |
|
@dopplershift yeah, I know about that. But I wasn't sure it was the same enough.. I still think its worth thinking about adding pint as a (optional) test dependency. Astropy too, if they have a robust units framework. Also your input on #13254 would probably be helpful if you didn't see it. |
|
Well, the whole goal of that class in
(#13254 is in my backlog) |
PR Summary
OK, this works w/
bar,barh, andbroken_barh, and I think it even passes the tests....EDIT: now works w/ bar, but see issue below with the pdf (and pgf) determinism tests...EDIT: OK, this may not work: i.e. consider
ax.bar(['a', 'b'], ['c', 'd']). Width defaults to 0.8, so we can't add 0.8 to 'a'. It works fine fordatetimeandtimedeltabecause they have addition defined, but you can't add a float to a string.So, we may have to go back to something like
delta = convert_units_delta(x, delta)and then assume that the converter can do the math.Closes #12862
Also closes #11290, though it doesn't address the call for a timedelta formatter, but at least it doesn't crash! Now time deltas work for both
barandbarh.fails on master. Now works by making sure that the timedelta math is done before the unit conversion is done.
PR Checklist