[英]How to plot two time series data with different time frequency on same axis in python?
[英]How can I plot two different spaced time series on one same plot in Python
我有两个不同的间隔时间序列,我想在同一个图上绘制。
它们都是12:30:00~1:25:00之间的系列,但它们的时间顺序不同:一个是5秒,另一个是大约10.3秒。 两个系列的类型是“pandas.core.series.Series”。 时间索引的类型是字符串,由strftime构成。 例如,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
和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
我试图通过以下方式在同一个图上绘制这两个系列:
import matplotlib.pyplot as plt
A.plot()
B.plot()
plt.gcf().autofmt_xdate()
plt.show()
它的工作原理如下:
很明显,第一张图中的蓝线在12:55:05左右消失,因为系列A只有半个x点的B,而plot()只根据x轴的顺序排列图,而不是时间间隔。
如果我单独绘制A系列,那将是非常清楚的:
我想要的是使两个系列显示在同一个图中并根据真实的时间间隔排列。 理想情况下,情节应该类似于:
我希望我已经说清楚了。 如果有什么令人困惑的,请告诉我。
这是直接创建日期时间,而不是将它们转换为字符串; 您可能需要matplotlib.dates.datestr2num
,具体取决于您的原始格式。 然后他们转换为matplotlib的日期时间表示。 这似乎很麻烦,但意味着时间间隔是正确的。
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.