简体   繁体   English

如何在matplotlib中的一个图上绘制由不同日期但相同时间戳组成的时间序列

[英]How to plot time series that consists of different dates but same timestamps on one graph in matplotlib

I have data that shows some values collected on three different dates: 2015-01-08, 2015-01-09 and 2015-01-12. 我有数据显示在三个不同日期收集的一些值:2015-01-08,2015-01-09和2015-01-12。 For each date there are several data points that have timestamps. 对于每个日期,有几个具有时间戳的数据点。

Date/times are in a list and it looks as follows: 日期/时间在列表中,如下所示:

['2015-01-08-09:00:00', '2015-01-08-10:00:00', '2015-01-08-11:00:00', '2015-01-08-12:00:00', '2015-01-08-13:00:00', '2015-01-09-14:00:00', '2015-01-09-15:00:00', '2015-01-09-16:00:00', '2015-01-12-09:00:00', '2015-01-12-10:00:00', '2015-01-12-11:00:00']

On the other hand I have corresponding values (floats) in another list: 另一方面,我在另一个列表中有相应的值(浮点数):

[12210.0, 12210.0, 12180.0, 12240.0, 12250.0, 12420.0, 12390.0, 12400.0, 12380.0, 12450.0, 12460.0]

To put all this together and plot a graph I use following code: 为了将所有这些放在一起并绘制图表,我使用以下代码:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as md
import dateutil
from matplotlib.font_manager import FontProperties

timestamps = ['2015-01-08-09:00:00', '2015-01-08-10:00:00', '2015-01-08-11:00:00', '2015-01-08-12:00:00', '2015-01-08-13:00:00', '2015-01-09-14:00:00', '2015-01-09-15:00:00', '2015-01-09-16:00:00', '2015-01-12-09:00:00', '2015-01-12-10:00:00', '2015-01-12-11:00:00']

ticks = [12210.0, 12210.0, 12180.0, 12240.0, 12250.0, 12420.0, 12390.0, 12400.0, 12380.0, 12450.0, 12460.0]

plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=90 )

dates = [dateutil.parser.parse(s) for s in timestamps]

ax=plt.gca()
ax.set_xticks(dates)
ax.tick_params(axis='x', labelsize=8)

xfmt = md.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)

plt.plot(dates, ticks, label="Price")

plt.xlabel("Date and time", fontsize=12)
plt.ylabel("Price", fontsize=12)
plt.suptitle("Price during last three days", fontsize=12)

plt.legend(loc=0,prop={'size':8})

plt.savefig("figure.pdf")

When I try to plot these datetimes and values I get a messy graph with the line going back and forth. 当我试图绘制这些日期时间和值时,我会得到一个乱七八糟的图形,其中来回的线条。

It looks like the dates are being ignored and only timestamps are taken in account which is the reason for the messy chart. 看起来日期被忽略,只考虑时间戳,这是杂乱图表的原因。 I tried to edit the datetimes to have the same date and consecutive timestamps and it fixed the chart. 我试图编辑日期时间以具有相同的日期和连续的时间戳,并修复了图表。 However, I must have dates as well.. 但是,我也必须有约会。

What am I doing wrong? 我究竟做错了什么?

When I try to plot these datetimes and values I get a messy graph with the line going back and forth. 当我试图绘制这些日期时间和值时,我会得到一个乱七八糟的图形,其中来回的线条。

Your plots are going all over the place because plt.plot connects the dots in the order you give it. 你的情节遍布整个地方,因为plt.plot按照你给它的顺序连接点。 If this order is not monotonically increasing in x, then it looks "messy". 如果这个顺序在x中没有单调增加,那么它看起来很“混乱”。 You can sort the points by x first to fix this. 您可以先按x对点进行排序以解决此问题。 Here is a minimal example: 这是一个最小的例子:

import numpy as np
import pylab as plt

X = np.random.random(20)
Y = 2*X+np.random.random(20)

idx = np.argsort(X)
X2 = X[idx]
Y2 = Y[idx]

fig,ax = plt.subplots(2,1)
ax[0].plot(X,Y)
ax[1].plot(X2,Y2)
plt.show()

在此输入图像描述

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

相关问题 如何使用matplotlib在同一张图上绘制具有不同采样率的两个时间序列 - how to plot two time series that have different sample rates on the same graph with matplotlib Matplotlib绘图时间序列图 - Matplotlib plot time series graph 如何从 Matplotlib 和 plot 上的时间序列图中显示日期 - How to show date from a time-series graph on Matplotlib a plot 使用不同的时间戳和超过一天的 datetime.time 格式绘制时间序列 - Plot time series with different timestamps and datetime.time format that goes over one day 如何在Python中的同一个图上绘制两个不同的间隔时间序列 - How can I plot two different spaced time series on one same plot in Python Matplotlib 绘制不同周期的时间序列 - Matplotlib Plot time series with different periodicity 如何按时间序列绘制图 - How to plot graph in time series 绘制具有不同日期的两个时间序列的值 - Plot values of two time series with different dates 如何在同一图上一个接一个地绘制多个时间序列 - How to plot multiple time series one after the other on the same plot 如何按给定的频率分组,例如说不同日期的每小时,并为时间序列数据集中的一列创建一组箱形图? - How to group by a given frequency let say Hourly for different dates, and create a set of box plot for one column in a time series data set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM