-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
Wrong/not useful error message when plotting incompatible x and y #20452
Comments
Picking out a bit more of the traceback we have ~/source/p/matplotlib/matplotlib/lib/matplotlib/axes/_base.py in <genexpr>(.0)
526 else:
527 labels = [label] * n_datasets
--> 529 result = (make_artist(x[:, j % ncx], y[:, j % ncy], kw,
530 {**kwargs, 'label': label})
531 for j, label in enumerate(labels))
533 if return_kwargs:
534 return list(result)
ZeroDivisionError: integer division or modulo by zero
and the error is coming from |
I don't know
gives: |
However,
and I think that our broadcasting in plot follows the MATLAB column-major rules. |
x = np.ones(10)
y = np.ones((0,10))
fig, ax = plt.subplots()
ax.plot(x,y) Gives (as does any other value that is mismatched, rather than just 0) |
Sorry for not being clear. What I meant is that in the dimension that numpy does its broadcasting, if you run it to 0 you get back a dimension with 0 on a side. However for MATLAB reasons, the rules on how we broadcast between the x and y values for multiple datasets is a bit different than the numpy broadcasting progression ax.plot(np.arange(10), np.ones((10, 3))) # 3 lines
ax.plot(np.arange(10), np.ones((10, 2))) # 2 lines
ax.plot(np.arange(10), np.ones((10, 1))) # 1 lines
# ax.plot(np.arange(10), np.ones((10, 0))) # 0 lines or expolde? In this progression "do nothing" seems like the right choice. |
Thanks, I guess given that |
Labeling as a good first issue as I think it needs a short-circuit return just above the traceback identified above (or maybe where we call that generator from) and a test. It will require reading and understanding some intermediate level Python, but no API design. |
Hello, if its okay can I give it a try? |
@jperezg-st Please do! |
Fixed by #20530. |
When plotting plt.plot(np.ones(10), np.ones((10,0)) it raises a ZeroDivisionError, which confused me much.
Code for reproduction
This raises the error:
Expected outcome
I think however, it should either raise a ValueError, similar to plotting plt.plot(np.ones(2), np.ones(10)), i.e. something of the form:
ValueError: x and y must have same first dimension, but have shapes (10,) and (10, 0)
An alternative would be not ploting anything (similar to the behaviour of plt.plot(np.nan, np.nan) ) and potentially raise a warning.
Matplotlib version
Matplotlib version 3.3.2
module://ipykernel.pylab.backend_inline
Python version 3.8.5
[@tacaswell edited to add markup]
The text was updated successfully, but these errors were encountered: