2010-04-23 121 views
1

我想在这里做一个基本的线图,但我似乎无法弄清楚如何调整我的x轴。matplotlib:使用范围在x轴

这里是我尝试调整范围时得到的错误。

from pylab import * 

plot (range(0,11),[9,4,5,2,3,5,7,12,2,3],'.-',label='sample1') 
plot (range(0,11),[12,5,33,2,4,5,3,3,22,10],'o-',label='sample2') 
xlabel('x axis') 
ylabel('y axis') 
title('my sample graphs') 
legend(('sample1','sample2')) 
savefig("sampleg.png",dpi=(640/8)) 

show() 


File "C:\Python26\lib\site-packages\matplotlib\axes.py", line 228, in _xy_from_xy 
raise ValueError("x and y must have same first dimension") 
ValueError: x and y must have same first dimension 

我希望我的范围是字符串列表:“1/12/2007”,“2008年12月1日”,“12/1/2009”,“2010年12月1日” ]

有什么建议吗?


老实说,我发现代码在线,并试图重写它,以正确理解它。我想我会从头开始,这样我就知道自己在做什么,但是我需要从哪里开始。

我张贴另一个问题这说明了什么我想在这里做的:

Using PyLab to create a 2D graph from two separate lists

+0

你的matplotlib调用中的日期在哪里?也许你可以重新格式化这个。 – 2010-04-23 16:01:33

回答

3

range(0,11)应该是range(0,10)

+1

......或者只是“范围(10)”。 – 2010-04-27 14:55:10

2

除了史蒂夫的观察:如果你的点总是一些y值在同一个连续的整数x's,matplotlib使得范围甚至是隐含的。

plot([9,4,5,2,3,5,7,12,2,3],'.-',label='sample1') 
+0

是的,更容易。 – 2010-04-23 16:03:27