首页 > 解决方案 > matplotlib,ipywidgets - 如何更改箭袋对象的位置?

问题描述

我想根据来自 ipywidgets 滚动的交互输入来更改箭袋(或箭头)对象的位置。

这是我尝试过的:

%matplotlib notebook
from ipywidgets import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Arrow

fig = plt.figure(figsize=[6,6])
ax = fig.add_subplot(1, 1, 1)

x_ar1 = -5 #object distance
dy_ar1 = 2 #boject heightX

object1 = plt.quiver(x_ar1, 0, 0, dy_ar1, angles='xy', scale_units='xy', scale=1)
ax.add_artist(object1)

#mark axis
x_axis, = ax.plot([-10, 10], [0, 0], lw=1, c='k')

plt.xlim([-10,10])
plt.ylim([-5,5])

def update(x = -5, y = 2):
    
    x_ar1 = x
    dy_ar1 = y
    
    object1.X = x_ar1
    object1.V = dy_ar1
    
    fig.canvas.draw_idle()
    

interact(update, x=(-6,6,0.1), y = (-2,2,0.1))

即使代码适用于箭袋的高度 ( object1.V),它也不适用于对象的放置 ( object1.X)。我也看不到任何set_xy可用的功能。有谁知道我在这里想念什么?

标签: pythonmatplotlibplotjupyter-notebookipywidgets

解决方案


我认为 matplotlib 目前不允许这样做。见https://matplotlib.org/devdocs/api/_as_gen/matplotlib.quiver.Quiver.html?highlight=quiver#matplotlib.quiver.Quiver

唯一的 API 方法是 set_UVC(),可以用来改变箭头的大小、方向和颜色;当类被实例化时,它们的位置是固定的。这种方法可能在动画中很有用。

Matplotlib 上有一个开放的功能请求:https ://github.com/matplotlib/matplotlib/issues/11790


推荐阅读