浮动条形图
Floating Bar Chart
我正在尝试制作一个图表,其中 x 轴是时间,y 轴是一个条形图,其中的条形图将覆盖特定时间段,如下所示:
______________
|_____________|
_____________________
|___________________|
----------------------------------------------------->
time
我有 2 个日期时间值列表,用于我希望涵盖的这些时间的开始和结束时间。到目前为止我有
x = np.array([dt.datetime(2010, 1, 8, i,0) for i in range(24)])
涵盖 24 小时。我的问题是如何设置和绘制我的 y 值看起来像这样?
您可以使用 plt.barh
:
import datetime as DT
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
start = [DT.datetime(2000,1,1)+DT.timedelta(days=i) for i in (2,0,3)]
end = [s+DT.timedelta(days=i) for s,i in zip(start, [15,7,10])]
start = mdates.date2num(start)
end = mdates.date2num(end)
yval = [1,2,3]
width = end-start
fig, ax = plt.subplots()
ax.barh(bottom=yval, width=width, left=start, height=0.3)
xfmt = mdates.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(xfmt)
# autorotate the dates
fig.autofmt_xdate()
plt.show()
产量
我正在尝试制作一个图表,其中 x 轴是时间,y 轴是一个条形图,其中的条形图将覆盖特定时间段,如下所示:
______________
|_____________|
_____________________
|___________________|
----------------------------------------------------->
time
我有 2 个日期时间值列表,用于我希望涵盖的这些时间的开始和结束时间。到目前为止我有
x = np.array([dt.datetime(2010, 1, 8, i,0) for i in range(24)])
涵盖 24 小时。我的问题是如何设置和绘制我的 y 值看起来像这样?
您可以使用 plt.barh
:
import datetime as DT
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
start = [DT.datetime(2000,1,1)+DT.timedelta(days=i) for i in (2,0,3)]
end = [s+DT.timedelta(days=i) for s,i in zip(start, [15,7,10])]
start = mdates.date2num(start)
end = mdates.date2num(end)
yval = [1,2,3]
width = end-start
fig, ax = plt.subplots()
ax.barh(bottom=yval, width=width, left=start, height=0.3)
xfmt = mdates.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_formatter(xfmt)
# autorotate the dates
fig.autofmt_xdate()
plt.show()
产量