我一直在搜索整个谷歌,但看起来我无法找到我正在寻找的确切内容.
所以,基本上,我有两个列表:一个列表包含时间戳数据,第二个列表包含与之对应的值.
现在我的问题是:我的时间戳采用以下格式
['Mon Sep 1 16:40:20 2015', 'Mon Sep 1 16:45:20 2015',
'Mon Sep 1 16:50:20 2015', 'Mon Sep 1 16:55:20 2015']
那么,在matplotlib中使用哪种时间格式?我试图直截了当地绘制这个但是它给了我:
ValueError: invalid literal
我可以使用datetime.datetime.strptime进行转换吗?如果没有,那么另一种方式是什么呢?
以正确的格式转换时间戳后,如何使用相应的值绘制新转换的时间戳?
我可以使用matplotlib.pyplot.plot(时间,数据)还是我必须使用plot_date方法来绘制它?
解决方法:
是的,使用strptime
import datetime
import matplotlib.pyplot as plt
x = ['Mon Sep 1 16:40:20 2015', 'Mon Sep 1 16:45:20 2015',
'Mon Sep 1 16:50:20 2015', 'Mon Sep 1 16:55:20 2015']
y = range(4)
x = [datetime.datetime.strptime(elem, '%a %b %d %H:%M:%S %Y') for elem in x]
(fig, ax) = plt.subplots(1, 1)
ax.plot(x, y)
fig.show()
标签:python,datetime,matplotlib,plot
来源: https://codeday.me/bug/20190824/1708182.html