2012-12-10 41 views
3

我刚开始尝试使用matplotlib,因为我经常遇到需要绘制一些数据的实例,因此matplotlib似乎是一个很好的工具。我试图修改主站点中的椭圆示例,以便画出两个圆圈,代码运行后,我发现没有显示任何修补程序,我无法弄清楚究竟是什么错误。这里是代码。提前致谢。MatPlotlib:修补程序未显示

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib 
import matplotlib.patches as mpatches 

plt.axis([-3,3,-3,3]) 
ax = plt.axes([-3,3,-3,3]) 
# add a circle 
art = mpatches.Circle([0,0], radius = 1, color = 'r', axes = ax) 

ax.add_artist(art) 

#add another circle 
art = mpatches.Circle([0,0], radius = 0.1, color = 'b', axes = ax) 

ax.add_artist(art) 

print ax.patches 

plt.show() 

回答

3

您正在使用哪个版本的matplotlib?我无法复制你的结果,我可以很好地看到这两个省略号。我打算通过一个远投,但我想你的意思是做这样的事情:

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib 
import matplotlib.patches as mpatches 

# create the figure and the axis in one shot 
fig, ax = plt.subplots(1,figsize=(6,6)) 

art = mpatches.Circle([0,0], radius = 1, color = 'r') 
#use add_patch instead, it's more clear what you are doing 
ax.add_patch(art) 

art = mpatches.Circle([0,0], radius = 0.1, color = 'b') 
ax.add_patch(art) 

print ax.patches 

#set the limit of the axes to -3,3 both on x and y 
ax.set_xlim(-3,3) 
ax.set_ylim(-3,3) 

plt.show() 
+0

感谢所做的更改使其工作,我使用Matplotlib v 1.2.0,从源代码编译。只是一个问题ax.set_xlim方法限制什么?再次非常感谢 – Jodgod

+0

'set_xlim'方法根据数据坐标强制绘图的极限。因此,将-3,3作为参数告诉matplotlib只绘制那些包含在该间隔中的对象 – EnricoGiampieri

+0

啊,我明白了,谢谢! – Jodgod

相关问题