在matplotlib中自动缩放,在同一张图表中绘制不同的时间序列 [英] autoscaling in matplotlib, plotting different time series in same chart
问题描述
我有一个主"熊猫数据框,该数据框具有几个术语的极性"值的时间序列.我想使用其中的4个,所以我提取了4个单独的数据帧,其中包含时间序列(所有术语的时间序列相同,但极性值不同.)
I have a 'master' panda dataframe that has a time series of 'polarity' values for several terms. I want to work with 4 of them, so I extracted 4 separate dataframes, containing the time series(same time series for all of the terms, but different polarity values.)
我使用下面的代码将它们绘制在4个独立的matplotlib图形中
I plotted them in 4 separate matplotlib graphs, using the code below
fig, axes = plt.subplots(nrows=2, ncols=2)
polarity_godzilla.plot(ax=axes[0,0]); axes[0,0].set_title('Godzilla')
polarity_henry_kissinger.plot(ax=axes[0,1]); axes[0,1].set_title('Henry Kissinger')
polarity_bmwi.plot(ax=axes[1,0]); axes[1,0].set_title('BMWi')
polarity_duran_duran.plot(ax=axes[1,1]); axes[1,1].set_title('Duran Duran')
现在,我想将它们全部绘制在同一张图中,因此我对每个图的幅值有所了解,因为matplotlib的自动缩放比例仅通过查看这些图就可以对幅值产生错误的印象.
Now, I want to graph them all in the same graph so I have an idea of the magnitude of each graph, because the auto scaling of matplotlib can give the wrong impression about the magnitude by just looking at the graphs.
两个问题: 1)有没有办法在绘制时设置Y轴的最小值和最大值? 2)我不是matplotlib的专家,所以我不确定如何使用不同的颜色,标记,标签等在同一张图中绘制4个变量.我尝试nrows = 1,ncols = 1,但无法绘制任何内容
Two questions: 1) Is there are way to set the min and max values of the Y-axis when plotting? 2) I am not an expert in matplotlib, so I am not sure how to plot the 4 variables in the same graph using different colors, markers, labels, etc. I tried nrows = 1, ncols = 1 but can't plot anything.
谢谢
推荐答案
axes[i,j].set_ylim([min,max], auto=False)
将在第i,j
个图中设置图的y极限. auto=False
防止破坏您的设置.
axes[i,j].set_ylim([min,max], auto=False)
will set the y-limits of the plot in the i,j
th plot. auto=False
keeps it from clobbering your settings.
您可以通过调用plt.hold(True)
,绘制一堆图,然后调用plt.show()
或plt.savefig(filename)
,在同一张图上绘制多条线.
You can plot multiple lines on the same graph by calling plt.hold(True)
, drawing a bunch of plots, and then calling plt.show()
or plt.savefig(filename)
.
您可以将颜色代码作为第三个位置参数传递给plt.plot()
.语法有点拜占庭式(继承自MATLAB).它记录在 matplotlib.pyplot.plot 文档中.您可以将此参数作为style='k--'
传递给DataFrame.plot
.
You can pass a color code into plt.plot()
as a third positional argument. The syntax is a little byzantine (it's inherited from MATLAB); it's documented in the matplotlib.pyplot.plot documentation. You can pass this argument to DataFrame.plot
as (for example) style='k--'
.
对于您的情况,我会尝试
For your case, I would try
fig, ax = plt.axes()
plt.hold(True)
polarity_godzilla.plot(ax=ax, style="k-o", label="Godzilla")
polarity_henry_kissinger(ax=ax, style="b-*", label="Kissinger")
#etc.
plt.legend() #to draw a legend with the labels you provided
plt.show() #or plt.savefig(filename)
这篇关于在matplotlib中自动缩放,在同一张图表中绘制不同的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!