[英]How can I plot two different spaced time series on one same plot in Python
I have two different spaced time series that I want to plot on one same graph. 我有两个不同的间隔时间序列,我想在同一个图上绘制。
Both of them are series between 12:30:00~1:25:00 but their time sequence are different: one is 5 seconds and the other is about 10.3 seconds. 它们都是12:30:00~1:25:00之间的系列,但它们的时间顺序不同:一个是5秒,另一个是大约10.3秒。 The type of both series is "pandas.core.series.Series".
两个系列的类型是“pandas.core.series.Series”。 The type of the time index is string and made from strftime.
时间索引的类型是字符串,由strftime构成。 For example, Series A would be:
例如,A系列将是:
12:30:05 0.176786
12:30:15 0.176786
12:30:26 0.176786
...
13:22:26 0.002395
13:22:37 0.002395
13:22:47 0.001574
and Series B would be: 和B系列将是:
12:30:05 0.140277
12:30:10 0.140277
12:30:15 0.140277
...
13:24:20 0.000642
13:24:25 0.000642
13:24:30 0.000454
I have tried to plot both of the series on one same plot by: 我试图通过以下方式在同一个图上绘制这两个系列:
import matplotlib.pyplot as plt
A.plot()
B.plot()
plt.gcf().autofmt_xdate()
plt.show()
and it works like this: 它的工作原理如下:
It is obvious that the blue lines in the first graph vanishes around 12:55:05 because series A has only half x points of B's and plot() only arrange the plot based on the order of x-axis, not the time interval. 很明显,第一张图中的蓝线在12:55:05左右消失,因为系列A只有半个x点的B,而plot()只根据x轴的顺序排列图,而不是时间间隔。
It will be quite clear if I plot series A alone: 如果我单独绘制A系列,那将是非常清楚的:
What I want is to make the two series shown in one same plot and arranged based on the true time interval. 我想要的是使两个系列显示在同一个图中并根据真实的时间间隔排列。 Ideally, the plot should be similarly as:
理想情况下,情节应该类似于:
I hope I've made my point clear. 我希望我已经说清楚了。 If anything confusing, please let me know.
如果有什么令人困惑的,请告诉我。
This is creating datetimes directly, not converting them to strings; 这是直接创建日期时间,而不是将它们转换为字符串; you might want
matplotlib.dates.datestr2num
instead, depending on your original format. 您可能需要
matplotlib.dates.datestr2num
,具体取决于您的原始格式。 Then they get converted to matplotlib's datetime representation. 然后他们转换为matplotlib的日期时间表示。 This seems like a hassle but means the spacing will be correct for times.
这似乎很麻烦,但意味着时间间隔是正确的。
import matplotlib.pyplot as plt
from matplotlib.dates import date2num , DateFormatter
import datetime as dt
# different times from the same timespan
TA = map(lambda x: date2num(dt.datetime(2015, 6, 15, 12, 1, x)),
range(1, 20, 5))
TB = map(lambda x: date2num(dt.datetime(2015, 6, 15, 12, 1, x)),
range(1, 20, 3))
A = [1.2, 1.1, 0.8, 0.66]
B = [1.3, 1.2, 0.7, 0.5, 0.45, 0.4, 0.3]
fig, ax = plt.subplots()
ax.plot_date(TA, A, 'b--')
ax.plot_date(TB, B, 'g:')
ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.