Che*_*e M 6 python timestamp date matplotlib
我想以特定的几种方式绘制x轴上的日期和y轴上的时间,然后绘制线图或间隔(浮动条)图.
但是我与那个情节有一些区别,我无法让它发挥作用.我实际上是让y轴绘制DATES和时间,所以当我需要大约一天的价值时,它会在y轴上淹没几个月的时间戳.该例子在评论中声称,"yaxis的基准日期可以是任何东西,因为信息在时间中",但我不明白他是如何"丢弃"基准日期信息.
无论如何,我的需求是:
我希望选择24小时时间(00:00到24:00)或y轴的上午/下午样式时间,轴刻度看起来像下午3:00,上午11:00等.这将是我想是用FuncFormatter完成的.
对于间隔(时间范围),我不想使用误差线 - 线条太薄.我想使用(浮动)条形/柱形图.
我的数据是'2010-12-20 05:00:00'格式的日期时间字符串
谢谢你的帮助.
Joe*_*ton 10
我认为你对matplotlib如何处理幕后的时间和日期感到有些困惑.
matplotlib中的所有日期时间都表示为简单浮点数.1天对应差异为1.0,日期是自1900年以来的天数(如果我没记错的话,无论如何).
因此,为了仅绘制给定日期的时间,您需要使用a % 1.
我要使用积分,但你可以轻松使用吧.bottom如果使用关键字参数,请查看使用关键字参数plt.bar,以便条形图的底部将在间隔的开始时间开始(并记住第二个参数是条形的高度,而不是其顶部的y值).
例如:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import datetime as dt
# Make a series of events 1 day apart
x = 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 date
times = 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()
Run Code Online (Sandbox Code Playgroud)

希望这有点帮助!
| 归档时间: |
|
| 查看次数: |
10524 次 |
| 最近记录: |