0

I am trying to have a multiple subplots with twinx() for the charts, but met with this error: TypeError: 'AxesSubplot' object does not support item assignment The code is as following:

fig, ax = plt.subplots(4,5,figsize=(8,8))
ax2[0,0]=ax[0,0].twinx()

When I try another way without the arrays:

fig, ax = plt.subplots(4,5,figsize=(8,8))
ax2=ax.twinx()

it comes out with another error:

AttributeError: 'numpy.ndarray' object has no attribute 'twinx'

1 Answer 1

1

Are you trying to create a twinx axis for each subplot? If so, you can do:

fig, ax = plt.subplots(4,5, figsize=(8,8))

ax2 = np.array([a.twinx() for a in ax.ravel()]).reshape_like(ax)

Then ax2 is a 2D-array with the same shape as ax, here 4,5; and each element of ax2 is a twinx instance of the corresponding element of ax.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, am trying to have twinx for each subplot. Thank you very much! It works, I changed the last part to .reshape(ax.shape)

Your Answer

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.