Peter Chao
Peter Chao

Reputation: 443

Use matplotlib to set the x-axis

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?

Upvotes: 1

Views: 155

Answers (2)

Darius
Darius

Reputation: 12042

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

Upvotes: 0

cge
cge

Reputation: 9890

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.

Upvotes: 2

Related Questions