matplotlib条形图与日期 [英] matplotlib bar chart with dates
问题描述
我知道 plot_date()
,但是有没有一个 bar_date()
在那里?
一般的方法是使用 set_xticks
和 set_xticklabels
d喜欢可以处理从几个小时到几年的时间尺度的东西(这意味着涉及主要和次要刻度,以使我可以理解的东西)。
编辑:我意识到我正在绘制与特定时间间隔相关联的值(该栏横跨)。我在下面更新了我使用的基本解决方案:
import matplotlib.pyplot as plt
import datetime
t = [datetime.datetime(2010,12,2,22,0),datetime.datetime(2010,12,2,23,0),datetime.datetime(2010,12,10,0,0),datetime.datetime (2010,12,10,6,0)]
y = [4,6,9,3]
interval = 1.0 / 24.0#1hr间隔,但maplotlib日期的基数为1天
ax = plt.subplot(111)
ax.bar(t,y,width = interval)
ax.xaxis_date()
plt.show()
所有 plot_date
是绘制函数和调用 ax.xaxis_date()
。
您应该需要做的是:
import numpy as np
pre>
import matplotlib.pyplot一个s plt
import datetime
x = [datetime.datetime(2010,12,1,10,0),
datetime.datetime(2011,1,4,9,0 )
datetime.datetime(2011,5,5,9,0)]
y = [4,9,2]
ax = plt.subplot(111)
ax.bar(x,y,width = 10)
ax.xaxis_date()
plt.show()
I know about
plot_date()
but is there abar_date()
out there?The general method would be to use
set_xticks
andset_xticklabels
, but I'd like something that can handle time scales from a few hours out to a few years (this means involving the major and minor ticks to make things readable I think).Edit: I realized that I am plotting values associated with a specific time interval (that the bar spans). I updated below with the basic solution I used:
import matplotlib.pyplot as plt import datetime t=[datetime.datetime(2010, 12, 2, 22, 0),datetime.datetime(2010, 12, 2, 23, 0), datetime.datetime(2010, 12, 10, 0, 0),datetime.datetime(2010, 12, 10, 6, 0)] y=[4,6,9,3] interval=1.0/24.0 #1hr intervals, but maplotlib dates have base of 1 day ax = plt.subplot(111) ax.bar(t, y, width=interval) ax.xaxis_date() plt.show()
解决方案All
plot_date
does is plot the function and the callax.xaxis_date()
.All you should need to do is this:
import numpy as np import matplotlib.pyplot as plt import datetime x = [datetime.datetime(2010, 12, 1, 10, 0), datetime.datetime(2011, 1, 4, 9, 0), datetime.datetime(2011, 5, 5, 9, 0)] y = [4, 9, 2] ax = plt.subplot(111) ax.bar(x, y, width=10) ax.xaxis_date() plt.show()
这篇关于matplotlib条形图与日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!