且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有日期时间基的python绘图茎

更新时间:2023-02-26 17:07:59

似乎茎x轴仅允许使用浮点数,因此您可以将日期转换为时间戳(浮点数),然后进行绘图.要在轴上显示日期,请使用 .xticks().这是一个示例:

It seems that stem x axis only admits floats, so you can convert your dates to timestamp (float) and then plotting. For showing the date on the axis use .xticks(). Here is an example:

import numpy as np
import matplotlib.pyplot as plt
from time import mktime
from datetime import datetime

ticks = [ "2013-9-28 11:00:00.234", "2013-9-28 11:10:00.123", "2013-9-28 11:40:00.654", "2013-9-28 11:50:00.341", "2013-9-28 12:00:00.773"]
y = np.array([10, 12, 9, 15, 11])
x = [mktime(datetime.strptime(i, "%Y-%m-%d %H:%M:%S.%f").timetuple()) for i in ticks]

plt.stem(x,y)
plt.xticks(x, ticks)
plt.show()