Home > Net >  How to change the start and end value on second Y axis matlib python
How to change the start and end value on second Y axis matlib python

Time:09-15

How do you change the start and end points of each of the Y axis on a sub plot? I would like the left y-axis to be from 0 to 10000, and the right y-axis to be from 8000 to 18000.

Below is my attempt, but I am only able to change the right y-axis units.

Thanks.

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(results['Date&Time'],results['PnL_Total'], color='g', label="PnL Total")
ax2.plot(dax['Date'], dax['Open'], color='b', label="Dax Value")
ax1.set_ylabel('PnL Total', color='g')
ax2.set_ylabel('Dax', color='b')
plt.ylim(8000, 18000)
plt.vlines(x=[XDate1], ymin=0, ymax=(18000), colors='purple', ls=':', lw=2)
plt.vlines(x=[XDate2], ymin=0, ymax=(18000), colors='purple', ls=':', lw=2)
plt.show()

CodePudding user response:

Use ax.set_ylim

Here is an example

fig, ax = plt.subplots()
ax.plot(range(10),np.random.normal(0,1,10))
ax.set_ylim(-3,3)

ax2 = ax.twinx()
ax2.plot(range(10),np.random.normal(0,1,10))
ax2.set_ylim(-6,6)

In your case you need to use

ax1.set_ylim(0,10000)
ax2.set_ylim(8000, 18000)
  • Related