简体   繁体   English

如何 plot 两个系列在 python 中的比例非常不同

[英]How to plot two series with very different scales in python

I'm a beginner in python.我是 python 的初学者。 I have to plot two graphs in the same plot.我必须在同一个 plot 中的 plot 两个图表。 One of my graphs is velocity, which ranges between (-1,1), and the other one is groundwater, which ranges between (10,12).我的一张图是速度,介于 (-1,1) 之间,另一张是地下水,介于 (10,12) 之间。 When I use the following code, the graphs become very small.当我使用以下代码时,图表变得非常小。

ax1 = plt.subplot(111)
ax2 = ax1.twinx()
df=pd.read_excel ('final-all-filters-0.6.xlsx')
df['Date']=pd.to_datetime(df['Date'])
date = df['Date']
gwl = df['gwl']
v =df['v']
plt.plot(date,gwl, color='deepskyblue',linewidth=2)
plt.plot(date,v, color='black',linewidth=2)
ax1.grid(axis='y')
ax1.xaxis.set_major_locator(matplotlib.dates.YearLocator())
ax1.xaxis.set_minor_locator(matplotlib.dates.MonthLocator((1,3,5,7,9,11)))
ax1.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("\n%Y"))
ax1.xaxis.set_minor_formatter(matplotlib.dates.DateFormatter("%b"))
ax1.grid(which='minor', alpha=0.3, linestyle='--')
ax1.grid(which='major', alpha=2)
for spine in ax1.spines.values():
        spine.set_edgecolor('gray')
ax1.tick_params(axis='x',  which='both', colors='gray')
ax1.tick_params(axis='y', colors='gray')
ax1.set_ylabel('v', color='g')
ax2.set_ylabel('GWL', color='b')
plt.show()

在此处输入图像描述

But when I add the ax1.set_ylim(-1, 1) and ax2.set_ylim(10, 12) to my code, one of the graph was disappered!但是当我将ax1.set_ylim(-1, 1)ax2.set_ylim(10, 12)添加到我的代码中时,其中一个图表消失了! 在此处输入图像描述

I think it does plot the black graph, but it's out of range.我认为它确实 plot 黑色图表,但它超出了范围。 You can check that by adding 11 or something to the black plot value.您可以通过向黑色 plot 值添加 11 或其他内容来检查。
Maybe you can try using ax2.set_yticks(np.arange(-1, 1, 0.5)) instead of set_ylim and/or using ax2.autoscale(enable=True, axis=y)也许您可以尝试使用ax2.set_yticks(np.arange(-1, 1, 0.5))而不是set_ylim和/或使用ax2.autoscale(enable=True, axis=y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM