matplotlib中条形图的日期格式

时间:2012-08-10 12:52:37

标签: python numpy matplotlib

  

可能重复:
  How to plot data against specific dates on the x-axis using matplotlib
  Plot with non-numerical data on x axis (for ex., dates)

我试图在matplotlib中生成条形图,其中x轴和y轴值是(日期,整数)的元组。数据具有以下形式:

data = [('2012-02-26', 17), ('2012-03-07', 16), ('2012-06-30', 16), 
        ('2012-03-16', 15), ('2012-06-24', 15), ('2012-05-13', 14), 
        ('2012-03-19', 12), ('2012-02-18', 9), ('2012-04-07', 9), 
        ('2012-04-09', 8)]

如果我只使用整数作为x值,我可以生成一个简单的条形图:

import scipy as sp
from matplotlib import pyplot as pl
x = sp.arange(100)
y = sp.random.randint(1,100, 100)
fig = figure()
pl.bar(x,y)

enter image description here

如何在x轴上绘制带有字符串日期的类似条形图?理想情况下,我还希望将x轴标签显示为日期。我在matplotlib recipe page上找到了一个类似的例子,但是我无法使用我的例子。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

使用plot_date函数代替plot()。

plot_date

答案 1 :(得分:0)

我用过

ax.xaxis.set_major_formatter(dates.DateFormatter('%d/%m'))
相关问题