在matplotlib的一天对日期的绘制时间 [英] plot time of day vs date in matplotlib
问题描述
我想以特定的几种方式绘制在y轴上的x轴和日期的日期,然后使用线图或间隔(浮动条)绘图。
但是,与这个情节有一些区别,我无法让它工作。我实际上正在y轴上绘制DATES以及时间,所以在y轴上,当我需要大约一天的价值时,它会在几个月内花费数月的时间戳。该例子在评论中声称,由于信息在时间上,yaxis的基准日期可以是任何事情,但我不明白他是如何抛弃基准日期信息。
无论如何,我的需求是:
-
我想要24小时的选项时间(00:00至24:00)或y轴的am / pm风格时间,轴刻度看起来像下午3:00,上午11:00等。这将使用FuncFormatter完成,我猜。 p>
-
对于间隔(时间范围),我不想使用错误栏 - 线条太薄。我想使用(浮动)栏/列图。
我的数据是格式的日期时间字符串'2010-12-20 05:00:00'
感谢任何帮助。
我认为你对matplotlib处理时间和幕后的处理方式有点困惑。
matplotlib中的所有数据时间都表示为简单浮点数。 1天对应于1.0的差异,日期是自1900年以来的天数(如果我还记得正确的话)。
所以,为了画出时间在给定的日期,您需要使用%1
。
我要使用积分,但您可以轻松使用酒吧。如果您使用 plt.bar
,请使用底部
关键字参数,以便条形图底部开始在你的间隔的开始时间(请记住,第二个参数是酒吧的高度,而不是其顶部的y值)。
例如:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import datetime as dt
#一系列事件间隔1天
x = mpl.dates.drange(dt.datetime(2009,10,1),
dt.datetime(2010,1,15),
dt.timedelta(days = 1))
#改变数据时间,以便它们随机发生
#记住,1.0是等效的在这种情况下为1天...
x + = np.random.random(x.size)
#我们可以通过使用模1来提取时间,并添加任意基数日期
times = x%1 + int(x [0])#(int是所以y轴从午夜开始...)
#我只是绘图点在这里,但您可以轻松使用酒吧。
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot_date(x,times,'ro')
ax.yaxis_date()
fig.autofmt_xdate()
plt.show()
< img src =https://i.stack.imgur.com/MUXMz.pngalt =时间与日期>
希望这有帮助!
I want to plot, in a specific few ways, dates on the x axis and time of day on the y axis, and then have either line plots or interval (floating bar) plots.
But I have a few differences from that plot and I can't get it to work. I'm actually getting the y axis to plot both the DATES as well as the time, so it is scrunching months' worth of timestamps on the y axis, when I just need about one day's worth. That example claims in the comments, "base date for yaxis can be anything, since information is in the time", but I don't understand how he is "throwing away" the base date information.
In any case, my needs are:
I'd like the option for either 24 hour time (00:00 to 24:00) or am/pm style time for the y axis, with the axis ticks looking like 3:00pm, 11:00am, etc. This will be done with a FuncFormatter, I guess.
For the intervals (time ranges) I don't want to use the error bars--the lines are way too thin. I'd like to use a (floating) bar/column plot.
My data are datetime strings of the format '2010-12-20 05:00:00'
Thanks for any help.
I think you're slightly confused as to exactly how matplotlib handles times and dates behind the scenes.
All datetimes in matplotlib are represented as simple floats. 1 day corresponds to a difference of 1.0, and the dates are in days since 1900 (if I remember correctly, anyway).
So, in order to plot just the time of a given date, you need to use a % 1
.
I'm going to use points, but you can easily use bars. Look into using the bottom
keyword argument if you do use plt.bar
, so that the bottom of the bars will start at the start time of your intervals (and remember that the second argument is the height of the bar, not its top y-value).
For example:
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()
Hopefully that helps a bit!
这篇关于在matplotlib的一天对日期的绘制时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!