Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Secondary y axis limit in pandas plot

Tags:

Is there a way to set the limit for the secondary Y axis in pandas df.plot

I have the following plotting statement. Is there a way to simply add ylim for secondary axis? as in "secondary_ylim=(0,1)"

df[["Date","Col1","Col2"]].plot(x="date",y=["Col1","Col2"],secondary_y="Col2",ylim = (0,1))
like image 873
Abist Avatar asked Apr 09 '19 17:04

Abist


1 Answers

Interesting.... I don't know if there is another way to get the axes for the secondary y_axes.

But, you could do it this way:

df = pd.DataFrame({'Date':pd.date_range('2019-02-01', periods=10), 'Col1':np.random.randint(0,10,10), 'Col2':np.random.randint(100,500, 10)})
ax = df[["Date","Col1","Col2"]].plot(x="Date",y=["Col1","Col2"],secondary_y="Col2", ylim = ([0,5]))
ax.set_ylim(0,5)
fig = ax.get_figure()
ax = fig.get_axes()
ax[1].set_ylim(0,250)

or as @Stef points out, you can use the right_ax

ax = df[["Date","Col1","Col2"]].plot(x="Date",y=["Col1","Col2"],secondary_y="Col2", ylim = ([0,5]))
ax.right_ax.set_ylim(0,250)

Output:

enter image description here

like image 117
Scott Boston Avatar answered Sep 18 '22 09:09

Scott Boston