Python 使用matplotlib,如何使用滑块动作更新部分地物?
我需要与下图交互。我想添加两个滑块来更改下图中红点的x、y位置。虚线和圆也应该随着红点的动作而更改,而蓝点应该保持在那里 我在matplotlib下找到了一些滑块的演示,它们都更新了图形的曲线。我的图形比它们复杂一些,我不知道如何更新部分 将numpy作为np导入 从matplotlib.lines导入Line2D 将matplotlib.pyplot作为plt导入 将matplotlib.animation导入为动画 从matplotlib导入rcParams 从matplotlib.widgets导入滑块 图,ax=plt子批次 轴线[-5,15,-5,15] ax.set_aspect1 x y轴等比例显示 锚定=4 主播=[0,10,0,10] anchorY=[0,0,10,10] 主播名称=['A0','A1','A2','A3'] colorArray=['蓝色'、'绿色'、'洋红'、'绿色'] ax.scatteranchorX,anchorY,s=200绘图稳定蓝点 对于i,enumerateanchorName中的txt: ax.TXT,anchorX[i],anchorY[i],fontsize=20 initTagX=np.random.random_样本*10 初始值=np.random.random_样本*10 tagPos=[initTagX,initTagY] ax.scattertagPos[0],tagPos[1],c='red',s=300绘制红点 画虚线 对于我在兰奇奥纳姆: 主播 diffVector=[tagPos[0]-anchorX[i],tagPos[1]-anchorY[i]] 距离=np.linalg.normdiffVector,ord=2计算二阶范数 平方和开根号 圆形=plt.CircleanchorPos,距离,颜色=颜色数组[i],填充=假,线型='-',线宽=4 ax.add\u艺术圈 ax.plot[anchorX[i],tagPos[0]],[anchorY[i],tagPos[1]],linestyle='-',color=colorArray[i],linewidth=4 节目Python 使用matplotlib,如何使用滑块动作更新部分地物?,python,matplotlib,Python,Matplotlib,我需要与下图交互。我想添加两个滑块来更改下图中红点的x、y位置。虚线和圆也应该随着红点的动作而更改,而蓝点应该保持在那里 我在matplotlib下找到了一些滑块的演示,它们都更新了图形的曲线。我的图形比它们复杂一些,我不知道如何更新部分 将numpy作为np导入 从matplotlib.lines导入Line2D 将matplotlib.pyplot作为plt导入 将matplotlib.animation导入为动画 从matplotlib导入rcParams 从matplotlib.wid
您的图形与您可能看到的示例之间的唯一区别是,当滑块发生更改时,您需要同时更新多个内容 更改x或y位置时,需要更新红点的x或y位置、4个圆的半径以及4条虚线的位置 下面的代码完成了所有这些。移动滑块时,将调用update_rp_x或update_rp_y。然后调用函数update_plot和更新的坐标,然后移动所有相关的绘图特征。对于红点,我从使用ax.scatter切换到使用ax.plot,因为更新其位置(例如rp.set_扩展数据)要简单一些。线和圆在第一次打印时存储在列表中,因此当我们更新它们时,我们可以循环这些列表并依次更新每个列表。这些圆只需要改变它们的半径,我们可以使用Circle.set_radius和set_xdata和set_ydata来改变它们的半径
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
fig, ax = plt.subplots()
ax.axis([-5,15,-5,15])
ax.set_aspect(1) # x y
# Make room at the bottom for sliders
fig.subplots_adjust(bottom=0.3)
# Initial tag location
initTagX = np.random.random_sample()*10
initTagY = np.random.random_sample()*10
tagPos = [initTagX, initTagY]
# Set up the anchors
anchorNum = 4
anchorX = [0,10,0,10]
anchorY = [0,0,10,10]
anchorName = ['A0', 'A1', 'A2', 'A3']
colorArray = ['blue', 'green', 'magenta', 'green']
# Plot the anchors
ax.scatter(anchorX, anchorY, s=100) # plot stable blue point
# Label the anchors
for i, txt in enumerate(anchorName):
ax.annotate(txt, (anchorX[i], anchorY[i]), fontsize = 10)
# Plot initial location of red point
rp, = ax.plot(tagPos[0], tagPos[1], c='red', marker='o', ms=10) # plot the red point
# Store circles and lines for update later
circles = []
lines = []
# Plot initial circles
for i in range(anchorNum):
anchorPos = (anchorX[i], anchorY[i])
diffVector = [(tagPos[0] - anchorX[i]), (tagPos[1] - anchorY[i])]
dist = np.linalg.norm(diffVector,ord = 2)
circle = plt.Circle(anchorPos, dist, color = colorArray[i], fill = False, linestyle='--', linewidth=2)
circles.append(ax.add_artist(circle))
lines.append(ax.plot([anchorX[i], tagPos[0]], [anchorY[i], tagPos[1]], linestyle = '--',
color = colorArray[i], linewidth=2)[0])
def update_plot(xpos, ypos):
'''
This function updates the radius of the circles
and the position of the dashed lines
'''
# Update the tag position
tagPos = [xpos, ypos]
rp.set_xdata(xpos)
rp.set_ydata(ypos)
for i in range(anchorNum):
anchorPos = (anchorX[i], anchorY[i])
diffVector = [(tagPos[0] - anchorX[i]), (tagPos[1] - anchorY[i])]
dist = np.linalg.norm(diffVector,ord = 2)
# Now we actually update the circles and dashed lines
circles[i].set_radius(dist)
lines[i].set_xdata([anchorX[i], tagPos[0]])
lines[i].set_ydata([anchorY[i], tagPos[1]])
return tagPos
def update_rp_x(xpos):
''' This function updates the x position of the red point '''
global tagPos
tagPos = update_plot(xpos, tagPos[1])
fig.canvas.draw_idle()
def update_rp_y(ypos):
''' This function updates the y position of the red point '''
global tagPos
tagPos = update_plot(tagPos[0], ypos)
fig.canvas.draw_idle()
# Create Axes for the sliders
axcolor = '#909090'
sax_x = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
sax_y = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)
# Create sliders
sx = Slider(sax_x, 'x position', -5, 15, valinit=initTagX)
sy = Slider(sax_y, 'y position', -5, 15, valinit=initTagY)
# Tell sliders which function to call when changed
sx.on_changed(update_rp_x)
sy.on_changed(update_rp_y)
plt.show()