원뿔형 플레인과 슬라이더 덕분에 평면 높이를 이동할 때 해당하는 볼륨이 2 개의 부 그림이있는 플롯을 작성하려고합니다. 슬라이더 값을 변경할 때마다 변경되는 텍스트를 구현하고 싶습니다.슬라이더 내에서 일부 plt.text를 인덱싱 할 수 없음
이제 문제가 하나 남았습니다. 텍스트가 서로 겹쳐져 완전히 쓸모없는 것을 만듭니다. 그래서 내 생각은 pyplot.text를 색인하고 슬라이더 값을 변경할 때마다 이전의 것을 제거하는 것이었다. 내 솔루션은 작동하지 않습니다하지만 난 문제를 우회 할 수있는 방법이 표시되지 않는 이유를 이해 : 내가 초기화하면 1 이상 가지 않을 것이다
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.widgets import Slider, Button, RadioButtons
plt.close('all') #close the previous figures
fig = plt.figure()
plt.subplots_adjust(bottom=0.25) #create a little space for the slider
X = np.arange(-50,50,2)
Y = np.arange(-50,50,2)
X,Y = np.meshgrid(X,Y)
""" --------- The Cone & The Plane ----------------------- """
Z = np.sqrt(X**2+Y**2) #Cone equation
h0 = 10
ax = fig.add_subplot(221, projection='3d') #create the space for the 2nd window (the plane)
zmax = 200
Z2 = 0*X+0*Y+h0 #Plane equation
l=ax.plot_surface(X,Y,Z2,color='#ff00ff',rstride=3, cstride=3,lw=0.1, alpha=0.2) #Plane plot
ax.plot_surface(X,Y,Z,color='#dd9900', rstride=2, cstride=2,lw=0.05,alpha=0.4) #Cone plot : wire frame
#plt.axis('scaled')
""" ----------- The Volume ------------------- """
ax2 = fig.add_subplot(122) #create the space for the 2nd window (the vol)
X2 = np.arange(0,250,1)
b= np.pi/3
Vol = b*X2**3 #equation of the cone volume
ax2.plot(X2,Vol,'k')
""" -------------- The slider ------------------------ """
axhauteur = plt.axes([0.2, 0.1, 0.65, 0.03]) #dimensions of the slider
shauteur = Slider(axhauteur, 'Hauteur', 0.5, zmax, valinit=h0) #caracteristics of the slider : from h0 to zmax with a pitch of 0.5
i=0
def update(val): # function defining the slider
h = shauteur.val
ax.clear() #the first plot is cleared and then redraw
l=ax.plot_surface(X,Y,0*X+0*Y+h,color='#ff00ff',rstride=3, cstride=3,lw=0.1,alpha=0.2)
ax.plot_surface(X,Y,Z,color='#dd9900', rstride=2, cstride=2,lw=0.05,alpha=0.4)
ax2.clear() #the 2nd plot is cleared and then redraw
ax2.plot(X2,Vol,'k')
ax2.axvline(x=h, linewidth=1, color = 'r')
ax2.axhline(y=b*h**3,linewidth=1, color = 'r')
#ax2.set_xlim(0,2*h)
#ax2.set_ylim(0,2*b*h**3)
fig.canvas.draw_idle()
V=b*h**3
#here, a reset for the plt.text is needed ! The indexing is not working yet
t_i=plt.text(13,10,'$ Volume = $\n' '$%s $'%(V), horizontalalignment='center', fontsize=30, bbox=dict(facecolor='k', alpha=0.25))
t_i.set_visible(False)
i=i+1
print i
shauteur.on_changed(update)
#plt.text(13,10,'$ Volume = $\n$0000000$', horizontalalignment='center', fontsize=30, bbox=dict(facecolor='k', alpha=0.25),visible=False)
plt.show()
: 여기
는 전체 코드입니다 def update(val):
에 있지만 내가 외부 경우 작동하지 않습니다 :
UnboundLocalError: local variable 'i' referenced before assignment which is also totally understandable.
'함수 내에서 정의되지 i''update', 당신이 원하는 경우 전역'i'를 사용하여 전역으로 정의하십시오. 'update' 함수 안에서'i'를 사용하기 전에'global i'를 추가하십시오. – hashmuke