我有一个csv文件,如下所示:
Axis [m],Channel 1 [],Channel 2 [],Channel 3 [],Channel 4 []
0,11.87772978,65.2269997,7.103221875,6.324708559
1.34E-08,17.65605321,75.09093444,8.309697828,14.87524308
2.69E-08,15.19155521,77.12878487,12.31291774,9.457125362
4.03E-08,23.85118853,88.76138941,20.10571063,8.041540228
5.38E-08,18.77440037,87.15681445,14.53884458,13.36609689
6.72E-08,19.54841939,117.9766076,16.87197928,18.50902666
8.06E-08,33.37595782,102.2086995,40.59474863,9.451430137
我想使用matplotlib绘制前两列中的值
我有以下代码:
import matplotlib.pyplot as plt
import pylab, csv
x=[]
y=[]
with open("test.csv","rU") as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
if re.search("\d",row[0]):
x.append(float(row[0]))
y.append(float(row[1]))
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.scatter(x,y,color='blue',s=5,edgecolor='none')
ax1.set_aspect(1./ax1.get_data_ratio()) # make axes square
pylab.savefig('test.jpg')
但是,这将绘制我的所有x值(122个总值),作为一条围绕0的线(而不是像我期望的那样作为一条曲线),请参见here以获取示例我认为这是因为X轴的尺度将所有数据聚在一起0左右。
我想我需要改变X轴的尺度来处理小数字吗?
任何帮助,一如既往,都将受到高度赞赏!
最佳答案
你可以像这样改变PyPoT轴
plt.axis([min(x), max(x), min(y), max(y)])
下面是一个剪切粘贴示例
from StringIO import StringIO
import matplotlib.pyplot as plt
import pylab, csv, re
data = '''0,11.87772978,65.2269997,7.103221875,6.324708559
1.34E-08,17.65605321,75.09093444,8.309697828,14.87524308
2.69E-08,15.19155521,77.12878487,12.31291774,9.457125362
4.03E-08,23.85118853,88.76138941,20.10571063,8.041540228
5.38E-08,18.77440037,87.15681445,14.53884458,13.36609689
6.72E-08,19.54841939,117.9766076,16.87197928,18.50902666
8.06E-08,33.37595782,102.2086995,40.59474863,9.451430137'''
x=[]
y=[]
file_ = StringIO(data)
reader = csv.reader(file_, delimiter=',')
for row in reader:
if re.search("\d",row[0]):
x.append(float(row[0]))
y.append(float(row[1]))
print x
print y
plt.plot(x, y)
plt.axis([min(x), max(x), min(y), max(y)])
plt.show()
这里是输出