栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用matplotlib在x轴上针对特定日期绘制数据

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用matplotlib在x轴上针对特定日期绘制数据

您正在做的事情很简单,以至于只使用plot而不是plot_date最容易。plot_date对于更复杂的情况非常有用,但是没有它就可以轻松完成所需的设置。

例如,根据上面的示例:

import datetime as DTfrom matplotlib import pyplot as pltfrom matplotlib.dates import date2numdata = [(DT.datetime.strptime('2010-02-05', "%Y-%m-%d"), 123),        (DT.datetime.strptime('2010-02-19', "%Y-%m-%d"), 678),        (DT.datetime.strptime('2010-03-05', "%Y-%m-%d"), 987),        (DT.datetime.strptime('2010-03-19', "%Y-%m-%d"), 345)]x = [date2num(date) for (date, value) in data]y = [value for (date, value) in data]fig = plt.figure()graph = fig.add_subplot(111)# Plot the data as a red line with round markersgraph.plot(x,y,'r-o')# Set the xtick locations to correspond to just the dates you entered.graph.set_xticks(x)# Set the xtick labels to correspond to just the dates you entered.graph.set_xticklabels(        [date.strftime("%Y-%m-%d") for (date, value) in data]        )plt.show()

如果您希望使用条形图,请使用

plt.bar()
。要了解如何设置线条和标记样式,请参见在标记位置使用日期标签进行绘图http://www.geology.wisc.edu/~jkington/matplotlib_date_labels.png
plt.plot()




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624099.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号