首页 文章

matplotlib条形图与日期

提问于
浏览
30

我知道 plot_date() 但那里有 bar_date()

一般的方法是使用 set_xticksset_xticklabels ,但我想要能够处理从几个小时到几年的时间尺度(这意味着涉及主要和次要的刻度以使我认为可读的东西) .

Edit: 我意识到我正在绘制与特定时间间隔(条形 Span )相关的值 . 我在下面用我使用的基本解决方案更新:

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()

1 回答

  • 39

    所有plot_date都是绘制函数和调用ax.xaxis_date() .

    您需要做的就是:

    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()
    

    bar graph with x dates

相关问题