2016-11-05 59 views
0

我使用Python 2.7.10與matplotlib 1.4.3matplotlib.patches.Arc()無法更新運行時

我想畫一個包含

matplotlib.patches.Arc(xy, width, height, angle=0.0, theta1=0.0, theta2=360.0, **kwargs) 
實例的情節theta2參數

運行期間,我允許用戶更改某些影響繪圖的參數。在這種情況下,我正在嘗試更改已存在的Arc實例的theta1和theta2參數。

但是,theta1和theta2參數不更新。我能夠更新其他參數:中心,寬度,高度和角度,但不是theta1和theta2。

我通過

Arc1 = matplotlib.patches.Arc(....) 

然後實例電弧以後在一個子程序中,我改變一些參數與

Arc1.center = new_center_position #Changes parameter 
Arc1.width = new_width   #Changes parameter 
Arc1.height = new_height   #Changes parameter 
Arc1.angle = new_angle   #Changes parameter 
Arc1.theta1 = new_start_angle  #Does not change parameter 
Arc1.theta2 = new_end_angle  #Does not change parameter 
fig.canvas.draw_idle()   #Redraw canvas to reflect changes 

其次,是存在其中我可以更新數的某種其它方式一次參數,而不必在新行中做每個參數?

回答

0

我個人認爲這是一個錯誤,你可以在matplotlib中提出問題。

問題是當設置matplotlib.patches.Arc.theta1時,包含圓弧的Path未更新。由於matplotlib.patches.Arc沒有任何setter方法,解決方法是通過

matplotlib.patches.Arc._path = matplotlib.patches.Path.arc(theta1 , theta2) 

創建一個新的Path每次下面是如何使用它的一個完整的例子。

import matplotlib.pyplot as plt 
import matplotlib.patches 
from matplotlib.widgets import Slider 


fig=plt.figure() 
ax=fig.add_subplot(121, aspect="equal") 

ax_x = plt.axes([0.58, 0.75, 0.33, 0.03], axisbg="w") 
ax_y = plt.axes([0.58, 0.65, 0.33, 0.03], axisbg="w") 
ax_w = plt.axes([0.58, 0.55, 0.33, 0.03], axisbg="w") 
ax_h = plt.axes([0.58, 0.45, 0.33, 0.03], axisbg="w") 
ax_a = plt.axes([0.58, 0.35, 0.33, 0.03], axisbg="w") 
ax_t1 = plt.axes([0.58, 0.25, 0.33, 0.03], axisbg="w") 
ax_t2 = plt.axes([0.58, 0.15, 0.33, 0.03], axisbg="w") 

s_x = Slider(ax_x, 'x', -1, 1, valinit=0) 
s_y = Slider(ax_y, 'y', -1, 1, valinit=0) 
s_w = Slider(ax_w, 'width', 0, 2, valinit=0.5) 
s_h = Slider(ax_h, 'height', 0, 2, valinit=0.5) 
s_a = Slider(ax_a, 'angle', 0, 360, valinit=0) 
s_t1 = Slider(ax_t1, 'theta1', 0, 360, valinit=0) 
s_t2 = Slider(ax_t2, 'theta2', 0, 360, valinit=360) 

arc = matplotlib.patches.Arc((0,0), width=0.5, height=0.5, linewidth=4, color="#bd3270") 
ax.add_patch(arc) 

def update(val): 
    arc.center = (s_x.val, s_y.val) 
    arc.width = s_w.val   
    arc.height = s_h.val    
    arc.angle = s_a.val    
    arc.theta1 = s_t1.val   
    arc.theta2 = s_t2.val 
    ## this is the line to add 
    arc._path = matplotlib.patches.Path.arc(s_t1.val , s_t2.val)  
    fig.canvas.draw_idle() 

for s in [s_x,s_y, s_w, s_h, s_a, s_t1, s_t2]: 
    s.on_changed(update) 

ax.set_xlim([-1,1]) 
ax.set_ylim([-1,1]) 
plt.show() 

enter image description here