Building data from CSV files using matplotlib

My CSV file looks

0.0 1 0.1 2 0.2 3 0.3 4 0.5 7 1.0 9 0.0 6 0.1 10 0.2 11 0.3 12 0.5 13 1.0 14 ... 

and I want to draw the first column along the X axis, the second column along the Y axis. So, my code

 import matplotlib.pyplot as plt from numpy import genfromtxt data=genfromtxt("test",names=['x','y']) ax=plt.subplot(111) ax.plot(data['x'],data['y']) plt.show() 

But this connects the end point of the graph, showing a straight line, graph
(source: tistory.com )

What I want is this graph. graph
(source: tistory.com )

Then how do I read a data file or are there options for disconnecting a line in matplotlib?

+5
source share
1 answer

As mentioned in the comments, each call to the chart will display all the pairs of points that it receives, so you must slice the data for each column. If all lines are 6 points in size, you can do something like this:

 import matplotlib.pyplot as plt from numpy import genfromtxt data=genfromtxt("test",names=['x','y']) x=data['x'] y=data['y'] columnsize = int(len(x)/6) ax=plt.subplot(111) for i in range(columnsize): ax.plot(x[i*6:(i+1)*6],y[i*6:(i+1)*6]) plt.show() 

this code works when x and y are of type numpy.ndarray . numpy arrays support indexing and slicing as standard python syntax.

+1
source

All Articles