I am trying to plot 3 series with 2 on the left y-axis and 1 on the right using secondary_y
, but it's not clear to me how to define the right y-axis scale as I did on the left with ylim=()
.
I have seen this post: Interact directly with axes
… but once I have:
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.randn(10,3))
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(df.index,df.iloc[:,[0,2]])
ax2.plot(df.index, df.iloc[:,2])
plt.show()
doesn't produce anything at all. I am using:
I found these links helpful:
You need to use set_ylim
on the appropriate ax.
For example:
ax2 = ax1.twinx()
ax2.set_ylim(bottom=-10, top=10)
Also, reviewing your code, it appears that you are specifying your iloc
columns incorrectly. Try:
ax1.plot(df.index, df.iloc[:, :2]) # Columns 0 and 1.
ax2.plot(df.index, df.iloc[:, 2]) # Column 2.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10,3))
print (df)
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(df.index,df.iloc[:,[0,2]])
ax2.plot(df.index, df.iloc[:,2])
plt.show()
You can do this without directly calling ax.twinx():
#Plot the first series on the LH y-axis
ax1 = df.plot('x_column','y1_column')
#Add the second series plot, and grab the RH axis
ax2 = df.plot('x_column','y2_column',ax=ax1)
ax2.set_ylim(0,10)
Note: Only tested in Pandas 19.2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.