1

Why is this matplotlib code giving me a weird exception? I'm going for two rows of plots. The top row is supposed to show true vs. pred and the bottom row is supposed to show percent error.

yy = func(*X)
fig, axes = plt.subplots(1, len(X))
for ax,_x in zip(axes,X):
 ax.plot(_x, y, 'b.')
 ax.plot(_x, yy, 'r.')
fig, axes = plt.subplots(2, len(X))
for ax,_x in zip(axes,X):
 ax.plot(_x, yy/y-1, 'r.')
plt.show()

Traceback:

 File "pysr.py", line 235, in main
 ax.plot(_x, yy/y-1, 'r.')
AttributeError: 'numpy.ndarray' object has no attribute 'plot'
asked Jun 29, 2016 at 23:32
1
  • Could you give a little more information on what X, y, and func are? I'm not able to reproduce the error you showed. Just a thought, if the code you are running has a typo and is instead for ax, _x in zip(X, axes), that would reproduce the error (and be a typo I could totally see myself making.) Commented Jun 29, 2016 at 23:47

1 Answer 1

8

If len(X) is>1, axes will be a 2D array of AxesSubplot instances. So when you loop over axes, you actually get a slice along one dimension of the axes array.

To overcome this, you could use axes.flat:

for ax,_x in zip(axes.flat,X):

Also if you are trying to plot all these on one figure, you don't need to call plt.subplots twice, as that will create two figures.

It may be easier to index the axes array like this:

yy = func(*X)
fig, axes = plt.subplots(2, len(X))
for i,_x in enumerate(X):
 axes[0, i].plot(_x, y, 'b.')
 axes[0, i].plot(_x, yy, 'r.')
 axes[1, i].plot(_x, yy/y-1, 'r.')
plt.show()
answered Jun 30, 2016 at 0:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.