Matplotlib diagrams with 2 y axis

In matplolib for a timeline, can I set the y axis to different values ​​on the left and make another y axis to the right with a different scale?

I use this:

import matplotlib.pyplot as plt plt.axis('normal') plt.axvspan(76, 76, facecolor='g', alpha=1) plt.plot(ts, 'b',linewidth=1.5) plt.ylabel("name",fontsize=14,color='blue') plt.ylim(ymax=100) plt.xlim(xmax=100) plt.grid(True) plt.title("name", fontsize=20,color='black') plt.xlabel('xlabel', fontsize=14, color='b') plt.show() 

Can I give 2 y axis on this graph?

In selector selector:

  plt.axvspan(76, 76, facecolor='g', alpha=1) 

I want to change the text to characterize this range, for example "This is a range selector", how can I do it?

+7
source share
1 answer

You want a twinx example . The bottom line is:

 ax = plt.gca() ax2 = ax.twinx() 

Then you can apply to the first axes with

 ax.plot(...) 

and the second with

 ax2.plot(...) 

In your case (I think) you want:

 import matplotlib.pyplot as plt ax = plt.gca() ax2 = ax.twinx() plt.axis('normal') ax2.axvspan(74, 76, facecolor='g', alpha=1) ax.plot(range(50), 'b',linewidth=1.5) ax.set_ylabel("name",fontsize=14,color='blue') ax2.set_ylabel("name2",fontsize=14,color='blue') ax.set_ylim(ymax=100) ax.set_xlim(xmax=100) ax.grid(True) plt.title("name", fontsize=20,color='black') ax.set_xlabel('xlabel', fontsize=14, color='b') plt.show() 
+16
source

All Articles