0

I am trying to plot a Gantt chart. I am new to python and not familiar with advanced programming concepts. On running the code I get the error shown.

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()

Error:

IndexError: index 2000 is out of bounds for axis 0 with size 2000

I think what I need is to be able to vectorize (jobnum%10) the array used to access the dictionary.

plt.hlines(macnum,procstart,procfinish,colors = colormapd[jobnum%10])

I am able to run:

plt.hlines(macnum,procstart,procfinish)

But I wish to change colors of lines as per the jobnumbers. I have 2500+ jobs. Any other method to create gantt chart, if better could be suggested too.

kosmos
  • 359
  • 5
  • 13

2 Answers2

0

Your arrays have indexes 0-1999, not 500-2499. So when the for loop hits 2000, the index is beyond the end of the arrays.

To set the individual colors, pass in an array with the colors corresponding to the data. Like so:

colors = np.array([colormapd[i%10] for i in therange])

for i in range(50,250):
    plt.hlines(macnum,procstart,procfinish,colors=colors)
    plt.show()
RootTwo
  • 4,288
  • 1
  • 11
  • 15
  • color_mapper = np.vectorize(lambda x: {1:"r", 2:"g", 3:"b", 4:"y", 5:"m", 6:"k", 7:"r", 8:"g", 9:"b", 0:"y",}.get(x)) plt.hlines(macnum,procstart,procstart+proctime,colors = color_mapper(jobnum%10)) This worked for me. – kosmos Mar 05 '16 at 07:02
  • http://stackoverflow.com/questions/18066781/create-gantt-plot-with-python-matplotlib – kosmos Mar 05 '16 at 07:11
0

could you include the full traceback in your question it is unclear at which line your code is failing.

also could you post a snippet of your csv file and a portion of the array.

That being said, if you have 2000 items in your csv file then your index of your first item would be 0 and your last 1999

your 2000th item in your array is really at index 1999 since array indexing starts at 0.

also use numpy.arange(start,end) not python native range. and remember arange and range do not include the endpoint.

here is some example code

macnum = np.arange(5,26)
procstart = np.linspace(0.0,23.5,21)
procend = procstart + .5

print macnum, len(macnum)
print procstart, len(procstart)
print procend, len(procend)

the first three arguments to plt.hlines can take arrays so just pass your arrays directly to the function.

plt.hlines(macnum, procstart, procend)
plt.show()

matplotlib has builtin color maps but i am not entirely sure how to use them yet.

Drafter250
  • 138
  • 9
  • Thanks. Me being new to python I forgot that the 'endpoint' is not included. It works now.I indeed passed them as arrays and it worked. But I wanted to give a different color to each job and was therefore struggling. – kosmos Mar 05 '16 at 07:09