浮动条形图 [英] Floating Bar Chart

查看:103
本文介绍了浮动条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一个图,其中x轴是时间,而y轴是条形图,其中的条形会覆盖特定时间段,如下所示:

I'm trying to make a plot where the x-axis is time and the y-axis is a bar chart that will have the bars covering a certain time period like this:

                         ______________
                         |_____________|

    _____________________
    |___________________|
----------------------------------------------------->
             time

我想要列出这些时间的开始和结束时间,其中有2个datetime值列表.到目前为止,我有

I have 2 lists of datetime values for the start and end of these times I'd like to have covered. So far I have

x = np.array([dt.datetime(2010, 1, 8, i,0) for i in range(24)])

涵盖24小时.我的问题是然后如何设置和绘制我的y值,使其看起来像这样?

to cover a 24-hour period. My question is then how do I set and plot my y-values to look like this?

推荐答案

您可以使用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()

收益

这篇关于浮动条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆