【发布时间】:2016-03-05 05:22:10
【问题描述】:
我正在尝试绘制甘特图。我是 python 新手,不熟悉高级编程概念。在运行代码时,我得到了显示的错误。
import numpy as np
import matplotlib as mpl
import pylab as plt
arr=np.loadtxt('gantt.csv',dtype=float,delimiter=",")
colormapd = {
1:"r",
2:"g",
3:"b",
4:"y",
5:"m",
6:"k",
7:"r",
8:"g",
9:"b",
0:"y",
}
therange=range(500,2500)
jobnum= arr[therange,0].astype(int)
macnum= arr[therange,2].astype(int)
procstart = arr[therange,3]
procfinish = arr[therange,4]
for i in range(500,2500):
plt.hlines(macnum[i],procstart[i],procfinish[i],colors = colormapd[1])
plt.show()
错误:
IndexError: index 2000 is out of bounds for axis 0 with size 2000
我认为我需要的是能够对用于访问字典的数组进行矢量化 (jobnum%10)。
plt.hlines(macnum,procstart,procfinish,colors = colormapd[jobnum%10])
我能跑:
plt.hlines(macnum,procstart,procfinish)
但我希望根据工作编号更改线条的颜色。我有 2500 多个工作岗位。任何其他创建甘特图的方法,如果更好,也可以建议。
【问题讨论】:
-
你试过用
for i in range(2000):替换for i in range(500,2500):吗?
标签: python numpy matplotlib vectorization gantt-chart