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

如何使用matplotlib在日期时间轴上绘制矩形?

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

如何使用matplotlib在日期时间轴上绘制矩形?

问题在于,matplotlib使用其自己的日期/时间表示形式(浮动天数),因此您必须先进行转换。此外,您将不得不告诉xaxis它应该具有日期/时间刻度和标签。下面的代码可以做到这一点:

from datetime import datetime, timedeltafrom matplotlib.patches import Rectangleimport matplotlib.pyplot as pltimport matplotlib.dates as mdates# Create new plotfig = plt.figure()ax = fig.add_subplot(111)# Create rectangle x coordinatesstartTime = datetime.now()endTime = startTime + timedelta(seconds = 1)# convert to matplotlib date representationstart = mdates.date2num(startTime)end = mdates.date2num(endTime)width = end - start# Plot rectanglerect = Rectangle((start, 0), width, 1, color='yellow')ax.add_patch(rect)# assign date locator / formatter to the x-axis to get proper labelslocator = mdates.AutoDateLocator(minticks=3)formatter = mdates.AutoDateFormatter(locator)ax.xaxis.set_major_locator(locator)ax.xaxis.set_major_formatter(formatter)# set the limitsplt.xlim([start-width, end+width])plt.ylim([-.5, 1.5])# goplt.show()

结果:

注意 :Matplotlib 1.0.1 非常 旧。我不能保证我的例子会起作用。您应该尝试更新!



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

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

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