PyPlot删除弧

时间:2014-04-03 13:11:36

标签: python matplotlib

下面给出的代码应该绘制2个弧,然后删除弧。

import numpy as np
from matplotlib import patches
import matplotlib.pyplot as plt
import math
import matplotlib

xcenter=300
ycenter=300
angle=0
k=0
p=[100,155]
theta=[1.0471975512, 1.0471975512]
fig=plt.figure()
ax=fig.add_subplot(111, aspect='equal')
plt.axis([0,600,0,600])
ax.grid('on')
plt.ion()
#plt.ion()
S=2
num_ellipse=[]

while k<S:
    if k==0:
        e=patches.Arc((xcenter, ycenter), p[k], p[k], 0.0, 0, theta[k]*(180/math.pi))
        ax.add_patch(e)
        num_ellipse.append(e)
        plt.show()
    elif k!=0 and k<S-1:
        e=patches.Arc((xcenter, ycenter), p[k], p[k], 0.0, theta[k-1]*(180/math.pi), theta[k]*(180/math.pi))
        ax.add_patch(e)
        num_ellipse.append(e)
    elif k==S-1:
        e=patches.Arc((xcenter, ycenter), p[k], p[k], 0.0, theta[k-1]*(180/math.pi), (2*math.pi/3)*(180/math.pi))
        ax.add_patch(e)
        num_ellipse.append(e)
    k=k+1

print num_ellipse
num_ellipse.remove(num_ellipse[0])
print num_ellipse
ax.add_patch(num_ellipse[0])
plt.show(block="True")

运行它会产生以下输出:

enter image description here

这里的问题是它应该删除第一个弧,但它没有这样做。我哪里错了?

1 个答案:

答案 0 :(得分:1)

从列表中删除它,不会将其从轴中删除。您需要在arc对象上调用.removehttp://matplotlib.org/1.3.1/api/artist_api.html

num_ellipse[0].remove()

您可以通过调用

来检查轴的补丁
print ax.patches
num_ellipse.remove(num_ellipse[0])
print ax.patches #Still the same. 
ax.patches[0].remove()
print ax.patches #now it's gone