我正在尝试使用 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))])
关于python-3.x - 使用 Matplotlib 创建简单的时间表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57282775/