0

我正在尝试在 Matplotlib 中使用交互式滑块实现绘图。我使用Interactive matplotlib plot 中的建议和两个滑块来实现滑块。现在我试图在滑块改变时移动一个点的注释。似乎有一些我无法弄清楚的奇怪行为。

下面是一个工作示例代码。代码在第三个子图中产生一个点,当滑块改变时,点移动。点上有一个随点移动的注释。但是,在当前示例中,旧注释没有被删除,这是不需要的。

我尝试了一些方法来解决这个问题。例如,我本来期望带有“!!”的注释代码 完成这项工作:删除旧注释,然后添加一个新注释。但是,如果未注释,则更改滑块时注释根本不会发生任何变化。

我已阅读删除注释,同时保留绘图 matplotlibPython 和 Remove annotation from figure以及Remove and Re-Add Object in matplotlib | 切换对象外观 matplotlib。我尝试从那里实施建议,即更改注释的坐标或将注释添加为艺术家对象。两人都没有做任何事情。

我在这里有什么基本的错误吗?我觉得我不明白注释的对象变量是如何在 python 中处理的。

还有一条评论:我宁愿不要过多地改变代码的结构(尤其是 sympy 的使用)。我删除了很多东西来创建这个最小的例子来重现错误,但我需要其他情节的结构。

import numpy as np
import sympy as sp
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.widgets import Slider, Button, RadioButtons

## Define variable symbols ##
var1 = sp.Symbol('var1')

## Define initial value ##
var1_init = 5

## Marker position functions ##
def positionY(var1):
    return var1

def positionX(var1):
    return var1

## plot ##
axis_color = 'lightgoldenrodyellow'

fig = plt.figure()
ax = fig.add_subplot(131)
ax2 = fig.add_subplot(132)
ax3 = fig.add_subplot(133)

# Adjust the subplots region to leave some space for the slider
fig.subplots_adjust(left=0.25, bottom=0.3)

# Draw the initial plot
marker_coord = (positionX(var1_init),positionY(var1_init))
[qMarker1] = ax3.plot(marker_coord[0],marker_coord[1],'ro')
ax3.set_xlim([-1.0, 11.0])
ax3.set_ylim([-1.0, 11.0])
qAnnotation = ax3.annotate('(%.2f, %.2f)' % marker_coord, xy=marker_coord, textcoords='data')

## Add sliders ##

# Define an axes area and draw sliders in it
var1_slider_ax  = fig.add_axes([0.25, 0.2, 0.65, 0.03], facecolor=axis_color)
var1_slider = Slider(var1_slider_ax, 'var1', 0, 10.0, valinit=var1_init)

# Define an action for modifying the plot1 when any slider's value changes
def sliders_on_changed(val):
    qMarker1.set_ydata(positionY(var1_slider.val))
    qMarker1.set_xdata(positionX(var1_slider.val))
    #qAnnotation.remove() ## <--------------------------- !!
    marker_coord = (positionX(var1_slider.val),positionY(var1_slider.val))
    qAnnotation = ax3.annotate('(%.2f, %.2f)' % marker_coord, xy=marker_coord, textcoords='data')
    fig.canvas.draw_idle()
var1_slider.on_changed(sliders_on_changed)

# Add a button for resetting the parameters
reset_button_ax = fig.add_axes([0.05, 0.1, 0.1, 0.04])
reset_button = Button(reset_button_ax, 'Reset', color=axis_color, hovercolor='0.975')
def reset_button_on_clicked(mouse_event):
    var1_slider.reset()
reset_button.on_clicked(reset_button_on_clicked)

plt.show()
4

1 回答 1

1

取消注释该行qAnnotation.remove()会产生一个错误 UnboundLocalError: local variable 'qAnnotation' referenced before assignment,这可以很好地解释问题。在函数的qAnnotation本地范围内重新定义。所以它实际上是关于理解 python 中的局部和全局范围,与特定的 matplotlib 对象无关。

在类似的情况下可以很容易地重现该错误

a = 0
def f():
    a += 1
    a=100
f()

这也抛出,UnboundLocalError: local variable 'a' referenced before assignment.

最简单的解决方案:qAnnotation在全局范围内提供 global qAnnotation.

def sliders_on_changed(val):
    global qAnnotation
    # ..
    qAnnotation.remove() 
    qAnnotation = ax3.annotate(...)
    fig.canvas.draw_idle()

避免显式global语句的不同解决方案是使注释成为列表的一部分并在本地访问该列表。

qAnnotation = [ax3.annotate( ... )]

def sliders_on_changed(val):
    # ..
    qAnnotation[0].remove()
    qAnnotation[0] = ax3.annotate( ... )
    fig.canvas.draw_idle()
于 2017-10-14T13:07:58.720 回答