1

I was trying to make two subplots with twinx axis, 3 curves (curvey,curve1 and curve2) on the first subplot and 3 curves (curvey, curve3 and curve4) on the second subplot. But I'm not getting what I want. I wonder if you guys can help me please.

x = df['curvex']
y = df['curvey']
y1 = df['curve1']
y2 = df['curve2']
y3=df['curve3']
y4=df['curve4']

fig, ax = plt.subplots(figsize=(40,10), dpi=500)

plt.subplot(2,1,1)

fig.subplots_adjust(right=0.75)
twin1 = ax.twinx()
twin2 = ax.twinx()

twin2.spines['right'].set_position(('axes', 1.02))

p1, = ax.plot(x,y, 'bo--', label='curvey', markersize=0.1, linewidth=0.2)
p2, = twin1.plot(x,y1, 'go--', label='curvey1', markersize=0.1, linewidth=0.2)
p3, = twin2.plot(x,y2, 'ro--', label='curvey2', markersize=0.1, linewidth=0.2)
 
ax.set_xlabel('CURVEX')
ax.set_ylabel('CURVEY') 
twin1.set_ylabel('CURVE1')
twin2.set_ylabel('CURVE2')

ax.yaxis.label.set_color(p1.get_color())
twin1.yaxis.label.set_color(p2.get_color())
twin2.yaxis.label.set_color(p3.get_color())

tkw = dict(size=0.5, width=0.2)
ax.tick_params(axis='y', colors=p1.get_color(), **tkw)
twin1.tick_params(axis='y', colors=p2.get_color(), **tkw)
twin2.tick_params(axis='y', colors=p3.get_color(), **tkw)

ax.legend(handles=[p1,p2,p3])



plt.subplot(2,1,2)

fig.subplots_adjust(right=0.75)
twin3 = ax.twinx()
twin4 = ax.twinx()

twin4.spines['right'].set_position(('axes', 1.02))

p1, = ax.plot(x,y, 'bo--', label='curvey', markersize=0.1, linewidth=0.2)
p4, = twin3.plot(x,y3, 'go--', label='curvey4', markersize=0.1, linewidth=0.2)
p5, = twin4.plot(x,y4, 'ro--', label='curvey5', markersize=0.1, linewidth=0.2)
 
ax.set_xlabel('CURVEX')
ax.set_ylabel('CURVEY') 
twin3.set_ylabel('CURVE4')
twin4.set_ylabel('CURVE5')

ax.yaxis.label.set_color(p1.get_color())
twin3.yaxis.label.set_color(p4.get_color())
twin4.yaxis.label.set_color(p5.get_color())

tkw = dict(size=0.5, width=0.2)
ax.tick_params(axis='y', colors=p1.get_color(), **tkw)
twin3.tick_params(axis='y', colors=p4.get_color(), **tkw)
twin4.tick_params(axis='y', colors=p5.get_color(), **tkw)

ax.legend(handles=[p1,p4,p5])

plt.show()

This is what I was getting. Y-axes on the left should be separated, for each subplot. Curvey, curve1 and curve2 should be on the first subplot, and curvey, curve3 and curve4 should be on the second subplot.In total, I want to see 3 curves in each subplot. I can't see legend neither. Y scales on the left and right are wrong also since the code is not good, I guess

enter image description here

1 Answers1

1

Your problem probably lies in these lines:

twin1 = ax.twinx()
twin2 = ax.twinx()

Incorrectly, you make twins for the same axis. Change the two subplot creation lines to this single line:

fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(40,10), dpi=500)

After that you can do:

twin1 = ax1.twinx()
twin2 = ax2.twinx()

Also, don't forget to plot and close the first figure before start working on the second figure:

ax.legend(handles=[p1,p2,p3])
plt.show()
plt.close()
czeni
  • 427
  • 2
  • 11
  • Hi czeni, first I want to thank you for try to help. Although there was a change in the result, it wasn't what I want. I'll attach screen of the desired plot I am looking for. – Jerick Rodriguez Jul 23 '21 at 00:15
  • I have been trying to change a bit the code that you suggested and I think I've finally gotten what I want. Thanks man – Jerick Rodriguez Jul 23 '21 at 02:25
  • @JerickRodriguez In that case if my answer is correct, could you please accept the answer. Thanks. – czeni Jul 23 '21 at 09:39