1

I have the following masterResult.csv file:

DATE,    DATASET,       BUILD        Col1   Col2 Col3
5/3/16,  Amazon,        1001         1113   344  169
5/3/16,  Amazon,        1002         1113   344  169
5/3/16,  Amazon,        1005         1113   344  169
5/3/16,  Amazon,        1006         1113   344  169

I would like to draw a graph using matplotlib.

My code is:

per_data=genfromtxt('masterResult.csv',delimiter=',',names=True)
for name in per_data.dtype.names[2:]:    
    plt.plot(per_data['BUILD'], per_data[name], label=name)

But that gave me: graph for masterResult.csv

And the x-axis range is not right. How to make it so x-axis range is from 1001 ~ 1006?

cge
  • 9,552
  • 3
  • 32
  • 51
Peter Chao
  • 413
  • 3
  • 8
  • 19

2 Answers2

2

The simple way to set the xaxis, if your data were right, would be plt.xlim(1001,1006), but that's not the problem.

The reason why your x axis is wrong in the first place is because your data file is not a correct csv file. The BUILD and subsequent columns aren't separated by commas, which confuses numpy. If you take a look at per_data directly, you'll see BUILD isn't set to a number at all. You either need to add commas consistently, or have it as tab/space separated data. And in general, if things look very wrong, it can be useful to look at your array directly instead of just plotting it.

cge
  • 9,552
  • 3
  • 32
  • 51
0

Import:

You can use different parameters to optimize the import:

data = np.genfromtxt('masterResult.csv', skip_header=1, usecols=(2,3,4,5), dtype="i8", autostrip=True)

print data
# [[1001 1113  344  169]
#  [1002 1113  344  169]
#  [1005 1113  344  169]
#  [1006 1113  344  169]]

Plot:

Finally a simple plot of the data:

draw = plt.plot(data[:,0], data[:,range(1,4)], '--o')
plt.legend(draw, ('Col1', 'Col2', 'Col3'))
plt.xlabel("Build")
plt.show()

enter image description here

Darius
  • 10,762
  • 2
  • 29
  • 50
  • Thanks so much! But for my x-axis, how do I add 1001,~ 1005 as x label? and there will be more attached to the file, we plan to run this many times, and I need to show the actual build number. – Peter Chao May 04 '16 at 04:50
  • OH I get it, the +1.001e3 is the floating number. Thanks! – Peter Chao May 04 '16 at 04:51
  • Exactly, otherwise you can [format the labels](http://stackoverflow.com/questions/3677368/matplotlib-format-axis-offset-values-to-whole-numbers-or-specific-number). – Darius May 04 '16 at 10:53