-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Improving error message for width and position type mismatch in violinplot #30545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
lib/matplotlib/axes/_axes.py
Outdated
| if any(isinstance(p, (datetime.datetime, datetime.date)) | ||
| for p in positions): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it enough to check the first position? I think mixed positions are not allowed anyway. Also, do we need to handle np.datetime64 arrays?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hasanrashid You've marked this as resolved. Please comment how my above questions are addressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timhoffm Yes, it is enough to check the first position. The position argument can't mix datetime with float. width can't mix timedelta and float either.
I was approaching this by converting entries into numeric equivalent until it was suggested that I provide clearer message instead.
Also, yes, datetime64 isn't needed either. I was thinking of all test cases, but I realize datetime64 isn't a supported type in violinplot.
Making the adjustments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is datetime64 not supported? Should we rather fix this? (Can be later, but already add the check here?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I try a testcase like this:
def test_np_datetime64_positions_with_float_widths_behaves_like_numeric():
"""Test that np.datetime64 positions with float widths do not raise TypeError (treated as numeric)."""
fig, ax = plt.subplots()
try:
vpstats = violin_plot_stats()
positions = [np.datetime64('2020-01-01'), np.datetime64('2021-01-01')]
widths = [0.5, 1.0]
# Should not raise TypeError, but may raise downstream if not supported
ax.violin(vpstats, positions=positions, widths=widths)
finally:
plt.close(fig)This produces the following error:
numpy._core._exceptions._UFuncBinaryResolutionError: ufunc 'add' cannot use operands with types dtype('float64') and dtype('<M8[D]')
I took this to mean that datetime64 was not an expected type of datetime for biolinplots- thus unsupported.
If we need to investigate adding datetime64 support in violineplot, should it be another issue? We are updating the error message enforcing timedelta with datetimes; datetime64 support- to me- seems like it should be investigated in a separate issue (which I am happy to open and work on)
timhoffm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please integrate the tests here:
matplotlib/lib/matplotlib/tests/test_axes.py
Line 4124 in ee6c7f6
| def test_violinplot_bad_widths(): |
The dviread module logs some information at the debug level (e.g., dvi "specials"). Allow printing them from the CLI, to ease verification of dvi internals.
Although event.step is only nonzero for scroll events, it seems reasonable to always add it to str(MouseEvent). After all, that str() always contains e.g. dblclick, which doesn't make sense for motion_notify_event either. (IOW the alternative would be to more carefully write different str()s for each kind of MouseEvents.)
timhoffm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please clean up the commits? There are several unrelated commits included.
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
|
Still several unrelated commits. Do you need help with cleaning up the history? |
|
This PR intends to close [ENH]: Support using datetimes as positions argument to violin(...) #30417
It should be possible to set the position of a violin plot to be a datetime. Currently, an attempt to do this results in this error message: TypeError: unsupported operand type(s) for +: 'float' and 'datetime.datetime'
The error stems from trying to perform operations between float and datetime if datetime was provided as position arguments.
The proposed solution improves the error message to be:
"If positions are datetime/date values, pass widths as datetime.timedelta (e.g., datetime.timedelta(days=10)) or numpy.timedelta64.unit tests are in tests\test_violinplot_datetime.py
I had opened another PR 30508, but messed up the commits while making changes. I am making this one after reading the suggestion here: #30508 (comment) by @rcomer . This change updates the error message instead of converting the position and width