matplotlib:轴的比例不正确

时间:2013-07-02 03:17:12

标签: python matplotlib

我有一个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轴的比例来处理小数字?
一如既往,任何帮助都将受到高度赞赏!

2 个答案:

答案 0 :(得分:5)

您已经为轴ax1set_aspect使用了非常强大的手柄。

您还可以使用此手柄设置轴的限制:

ax1.set_xlim(0,3)
ax1.set_ylim(0,3)

Axes handle具有更多属性。我建议安装IPython,尤其是IPython笔记本。然后,您只需键入以下内容即可始终查看句柄的属性:

ax1.

(ax1点),然后按TAB键。

答案 1 :(得分:4)

您可以像这样更改pyplots轴

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()

这是输出

enter image description here