In matplolib for a time line diagram can I set to y-axis different values on the left and make another y-axis to the right with other scale?
I am using 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 in this plot?
In span selector:
plt.axvspan(76, 76, facecolor='g', alpha=1)
I want to right text to characterize this span for example 'This is span selector' how can I make it?
Create multiple y axes with a shared x axis. This is done by creating a twinx axes, turning all spines but the right one invisible and offset its position using set_position . Note that this approach uses matplotlib.
By using the twinx() method we create two twin y-axes. In the above example, by using the twinx() method we create two y-axes and plot the same data by using the plot() method.
You want twinx
example. The gist if it is:
ax = plt.gca()
ax2 = ax.twinx()
You can then plot 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With