2012-12-10 74 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

相關問題