0

I am trying to plot histograms with two y axes.

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 8))
ax2 = ax.twinx() 

but this line (ax2 = ax.twinx()) results in error:

'numpy.ndarray' object has no attribute 'twinx'
JohanC
  • 71,591
  • 8
  • 33
  • 66
  • 2
    ax is a list/array of axes, not a single axis object. – Dr. Snoopy Jul 03 '21 at 23:08
  • 2
    When `nrows` or `ncols` are larger than `1`, `plt.subplots(...)` creates a numpy array of axes. You can use `ax2 = ax[0,0].twinx() ` in the given code. The [matplotlib docs](https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html) suggest calling the variable `axs` in that case. Or `fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(....)` – JohanC Jul 03 '21 at 23:09

0 Answers0