带日期的 matplotlib 条形图 [英] matplotlib bar chart with dates
本文介绍了带日期的 matplotlib 条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道 plot_date()
但有没有 bar_date()
?
I know about plot_date()
but is there a bar_date()
out there?
一般的方法是使用 set_xticks
和 set_xticklabels
,但我想要一些可以处理从几个小时到几年的时间尺度的东西(这个意味着涉及主要和次要刻度以使我认为可读).
The general method would be to use set_xticks
and set_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).
我意识到我正在绘制与特定时间间隔(条形跨度)相关的值.我在下面更新了我使用的基本解决方案:
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()
推荐答案
所有 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()
这篇关于带日期的 matplotlib 条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文