python-3.x - 使用 Matplotlib 创建简单的时间表

标签 python-3.x matplotlib

我正在尝试使用 Matplotlib 创建一个简单的时间表。我有以下开始。我想删除 y 轴数字并显示“a=1”、“a=2”、“a=3”。事实上,我的函数中定义的 y1 和 y2 有点假,因为盒子的厚度不一定是某个数字。只要每个“a”的厚度相等,就可以了。我想绘制一个带有阴影的框,其中 a=1 在 370 到 560 之间,a=2 在 550 到 980 之间,依此类推。 x 轴显示时间(以分钟为单位)。我检查了水平条形图,但它们都从 0 开始,我无法找到将它们转换为调度类型的方法。有什么建议吗?

import matplotlib.pyplot as plt

x =  [(370, 560), (550,980), (380,440)]

def activity_filler(x,y1,y2):
    # Shade the area between y1 and y2
    plt.fill_between(x, y1, y2,
                     facecolor="grey", # The fill color
                     color='grey',       # The outline color
                     alpha=0.4, hatch = 'X\/|-')          # Transparency of the fill
activity_filler(x[0],[1],[2])
activity_filler(x[1],[2],[3])
activity_filler(x[2],[3],[4])
plt.show()

最佳答案

x = [(370, 560), (550,980), (380,440)]

fig, ax = plt.subplots()
for i,evt in enumerate(x):
    ax.barh(i,width=evt[1]-evt[0],left=evt[0])

ax.set_yticks(range(len(x)))
ax.set_yticklabels([f'a={i+1}' for i in range(len(x))])

enter image description here

关于python-3.x - 使用 Matplotlib 创建简单的时间表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57282775/

相关文章:

python - 在实践中,为什么比较整数比比较字符串更好?

python - 我怎样才能简化我的代码(Python登录系统)?

python - Matplotlib 渲染所有内部体素(带 alpha)

pandas - Statsmodels 马赛克图 ValueError : cannot convert float NaN to integer

python - 如何在没有qrc系统的情况下在pyqt6中设置背景图像

python - pandas 系列从十六进制转换为 ascii 时出现错误

python - argparse 参数依赖

python - 风玫瑰图的自定义缩放

python - 使用带有secondary_y的Seaborn + Pandas绘图时如何摆脱网格线

python - 如何使用 pyplot.jl 中的颜色图作为 julia-lang 中的函数