python3:用pyp绘制完整日期

2024-09-26 22:44:53 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我试着在pyplot图的x轴上画出完整的日期。日期格式为<2015:00-01:00 我已经把它转换成了一个数字,他跟着代码。。。在

import matplotlib.dates as mdates
import datetime
import matplotlib.pyplot as plt
# xList is obtained from a csv file, it's a list of strings
xList = ['2015-11-20 00:01:00' for u in range(100)]
x = [mdates.date2num(datetime.datetime.strptime(op,'%Y-%m-%d %H:%M:%S')) for op in xList]
# yes that line is horrible, but it was okay for the playground
# I convert the string into the date format, and then convert it into a number
y = [u for u in range(len(x))
plt.plot_date(x,y)
plt.show()

结果是一条斜线(当使用plot_date时,确实是出于某种原因)只在x轴上显示日期的时间部分。这是一个问题,因为我的数据跨越了可变的日期,我想知道事件发生在哪一天。在

我还尝试传递转换后的日期格式(不传递mdates.date2num我得到了同样的结果。 任何帮助都将不胜感激。在


Tags: theinimportfordatetimedatematplotlibis
1条回答
网友
1楼 · 发布于 2024-09-26 22:44:53

不用绘图日期,只需使用plot函数。但是,加载项

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))
plt.plot(x,range(len(x))
plt.gcf().autofmt_xdate()

当您调用plot函数时。在

相关问题 更多 >

    热门问题