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

在matplotlib中绘制一天中的时间与日期的关系

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

在matplotlib中绘制一天中的时间与日期的关系

我认为您对matplotlib究竟如何在后台处理时间和日期感到有些困惑。

matplotlib中的所有日期时间都表示为简单浮点数。1天对应于1.0的差,日期是自1900年以来的天数(如果我没有记错的话,无论如何)。

因此,为了只绘制给定日期的时间,您需要使用

% 1

我将使用点,但是您可以轻松使用条形图。考虑使用

bottom
,如果你使用关键字参数
plt.bar
,使杆的底部将在您的时间间隔的开始时间开始(请记住第二个参数是
高度 了吧,不是其顶部的y值)。

例如:

import matplotlib.pyplot as pltimport matplotlib as mplimport numpy as npimport datetime as dt# Make a series of events 1 day apartx = mpl.dates.drange(dt.datetime(2009,10,1),dt.datetime(2010,1,15),dt.timedelta(days=1))# Vary the datetimes so that they occur at random times# Remember, 1.0 is equivalent to 1 day in this case...x += np.random.random(x.size)# We can extract the time by using a modulo 1, and adding an arbitrary base datetimes = x % 1 + int(x[0]) # (The int is so the y-axis starts at midnight...)# I'm just plotting points here, but you could just as easily use a bar.fig = plt.figure()ax = fig.add_subplot(111)ax.plot_date(x, times, 'ro')ax.yaxis_date()fig.autofmt_xdate()plt.show()

希望能有所帮助!



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

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

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