【问题标题】:Lines in a bar chart Matplotlib条形图中的线条 Matplotlib
【发布时间】:2017-11-09 00:56:54
【问题描述】:

我的目标是用条形图表示分配的时间。分配的小时数应表示为 sched_hrs 上方的 tot_hrs。因此,7 处的线应该在 'Blairstone FL' 处的 1 条的正上方。然而,情节出现在“Blairstone FL”、“Bonifay FL”和“Calhoun FL”之上的三行 7、8 和 9。主要问题存在于 tt in range(len(service_areas)): 中的以下代码。非常感谢任何帮助。

sched_hrs=[1, 2, 3]
tot_hrs=[7, 8, 9]
columnstart = -0.5
columnend = 0.5
service_areas = ['Blairstone FL', 'Bonifay FL', 'Calhoun FL']

plt.figure(figsize=(len(service_areas) + 4.5, 4)).canvas.set_window_title('Click')
ind = np.arange(len(service_areas))
width = 0.18
plt.bar(ind, sched_hrs,width, label='Scheduled Hours', color='green', align="center")
for tt in range(len(service_areas)):
    plt.plot([columnstart, columnend], [tot_hrs, tot_hrs], color='#228B22', linestyle='-', linewidth=2)
    columnstart +=1
    columnend +=1
plt.xticks(ind, service_areas, rotation=10)
plt.yticks(np.arange(0, max(1, max(tot_hrs) + 2), 1))
plt.title("Allocated Hours")
plt.xlabel("Areas")
plt.ylabel("Hours")
plt.legend()
plt.show() 

【问题讨论】:

  • 所以基本上你想创建一个堆积条形图,每个条形图由 sched_hrs 和 tot_hrs 组成?
  • 不是堆积图。我想在条形图上方有一个折线图。不过谢谢。

标签: python python-3.x matplotlib bar-chart


【解决方案1】:

假设这是您想要实现的目标:

那么你应该像这样画线:

plt.bar(ind, 0.1, width, bottom=tot_hrs, color='green', align="center")

替换这段代码:

for tt in range(len(service_areas)):
    plt.plot([columnstart, columnend], [tot_hrs, tot_hrs], color='#228B22', linestyle='-', linewidth=2)
    columnstart +=1
    columnend +=1

【讨论】:

  • 感谢您添加此答案。我们的目标是让直线之间有断点,而不是增加线性投影。
  • 您可能需要绘制一张图来表明您的期望。使用 ms paint 或任何可用的绘图工具。
  • 图片已添加。
猜你喜欢
  • 1970-01-01
  • 2016-06-16
  • 2018-08-22
  • 2017-03-27
  • 1970-01-01
  • 2015-12-25
  • 2016-01-02
  • 1970-01-01
相关资源
最近更新 更多