【问题标题】:How to set the min and max value for SpanSelector widget programmatically如何以编程方式设置 SpanSelector 小部件的最小值和最大值
【发布时间】:2021-04-09 15:18:05
【问题描述】:

我想根据按预期工作的 SpanSelector 更新子图。但另一个要求是我需要将选定的跨度初始化为一些给定的值。我曾尝试调用_press_release 函数,但它们只是返回False,但没有任何反应。如何以编程方式设置跨度,以便所选跨度在 SpanSelector 中也可见?

from matplotlib.widgets import SpanSelector
from collections import namedtuple

widgets.append(
    SpanSelector(range_ax, onselect=self._plot_subplots, direction='horizontal', rectprops=dict(alpha=0.5, facecolor='red'), span_stays=True)
)

# fake some events
Event = namedtuple('Event', ['xdata', 'ydata'])
widgets[0]._press(Event(0, 119))
widgets[0]._release(Event(500, 120))

【问题讨论】:

    标签: python matplotlib matplotlib-widget


    【解决方案1】:

    设置选择矩形stay_rect 并使用初始xminxmax 值调用onselect 处理程序。

    示例:来自docs 的示例的初始视图,在plt.show() 之前插入了以下内容:

    xmin, xmax = 3, 4 
    span.stay_rect.set_bounds(xmin, 0, xmax-xmin, 1)
    span.stay_rect.set_visible(True)
    span.onselect(xmin, xmax)
    

    【讨论】:

    • 这显示所需图表为真,但未显示所选范围span_stays=True
    【解决方案2】:

    我认为使用extents 更干净。根据文档中的示例,您可以执行以下操作:

    span = SpanSelector(
        ax1,
        onselect,
        "horizontal",
        useblit=True,
        props=dict(alpha=0.5, facecolor="tab:blue"),
        interactive=True,
        drag_from_anywhere=True,
        span_stays=True,  # Add this to make SpanSelector persistent
    )
    
    # Set default values
    xmin, xmax = 1, 4
    span.extents = (xmin, xmax)
    span.onselect(xmin, xmax)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 2015-05-13
      相关资源
      最近更新 更多