简体   繁体   English

如何根据 python 中的日期和时间并按日期和时间排序 plot 数据?

[英]How can I plot data based on and ordered by date and time in python?

I have two lists, one containing datetime elements and another containing numerical values.我有两个列表,一个包含日期时间元素,另一个包含数值。 I want to plot a scatter plot in which the X axis is the date and the Y axis is the numerical value.我想 plot 散点图 plot ,其中 X 轴是日期,Y 轴是数值。 The format of the datetime is the next: "YYYY-MM-DD HH:mm:ss".日期时间的格式如下:“YYYY-MM-DD HH:mm:ss”。

datetimes = ['2015-01-10 10:00:00', '2015-01-11 10:00:00', '2015-01-10 11:00:00', '2015-01-10 10:30:00']
numerical_values = [3.0, 2.0, 10.0, 4.0]

How can I plot the scatter ordered by datetimes?我怎样才能 plot 按日期时间排序的散点图?

You can sort your lists and keep track of your coordinate doing:您可以对列表进行排序并跟踪您的坐标:

datetimes = ['2015-01-10 10:00:00', '2015-01-11 10:00:00', '2015-01-10 11:00:00', '2015-01-10 10:30:00']
numerical_values = [3.0, 2.0, 10.0, 4.0]

x_y = sorted([(x,y) for (x,y) in zip(datetimes,numerical_values)], key=lambda z: z[0])

x = []
y = []

for X, Y in x_y:
    x.append(X)
    y.append(Y)

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

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