简体   繁体   English

使用matplotlib从CSV文件绘制数据

[英]Plotting data from CSV files using matplotlib

My csv file looks 我的csv文件看起来

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 in x axis, second column as y axis. 我想在x轴上绘制第一列,在第二列绘制y轴。 So my code is 所以我的代码是

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 connect the end point of graph, showing straight line, graph http://cfile7.uf.tistory.com/image/24776F37554AD3670CECF8 What I want is this graph. 但这连接图的终点,显示直线, 图http://cfile7.uf.tistory.com/image/24776F37554AD3670CECF8我想要的就是这张图。 graph http://cfile7.uf.tistory.com/image/27372B49554AD4422EBD0F 图http://cfile7.uf.tistory.com/image/27372B49554AD4422EBD0F

Then how do I read data file or are there any options in matplotlib disconnecting the line? 然后我如何读取数据文件或matplotlib中有任何选项断开线路?

As others mentioned in the comments, every call to plot will plot all the point-pairs it gets so you should slice the data for every column. 正如评论中提到的其他人一样,每次调用绘图都会绘制它所获得的所有点对,因此您应该为每列切片数据。 If all the lines are of size 6 points you can do something like this: 如果所有线条的大小均为6磅,您可以执行以下操作:

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 . xy的类型为numpy.ndarray时,此代码有效。 numpy arrays support indexing and slicing as python standard syntax. numpy数组支持索引和切片为python标准语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM