- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我运行 this示例并创建一个矩形选区,如果我缩放或移动选区周围的绘图窗口消失,直到我取消选择移动或缩放工具并再次单击绘图窗口。
我在 IPython 笔记本中使用 %matplotlib tkinter
。
我已经尝试了解缩放窗口时发生的限制变化并将矩形选区设置为可见:
def persist_rect(newlims):
rs = toggle_selector.RS
print(rs.visible)
rs.set_visible(True)
rs.update()
current_ax.callbacks.connect('xlim_changed', persist_rect)
current_ax.callbacks.connect('ylim_changed', persist_rect)
但这似乎并没有做任何事情。它甚至没有将 toggle_selector.RS.visible
设置为 false。
我也一直在查看 source for RectangleSelector ,但我还没有看到任何有启发性的东西。
我还发现,当我使用 RectangleSelector.extents = new_extents
修改所选区域的范围时,我也遇到了这个问题。当 .extents
被修改时,例如使用 slider 小部件,所选区域会消失,直到我再次单击绘图。
如果按照@ImportanceOfBeingErnest 的建议用useblit=False
初始化RectangleSelector
,所有这些问题都会消失,但是,正如他们所说,这不是一个非常高效的解决方案.
最佳答案
为 draw_event
添加回调:
def mycallback(event):
if RS.active:
RS.update()
plt.connect('draw_event', mycallback)
使 RectangleSelector
在缩放或平移后保持不变,并与 useblit=True
兼容。
例如,使用 code from the docs作为基础:
from __future__ import print_function
from matplotlib.widgets import RectangleSelector
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.widgets as widgets
import threading
import datetime as DT
def line_select_callback(eclick, erelease):
'eclick and erelease are the press and release events'
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
print(" The button you used were: %s %s" % (eclick.button, erelease.button))
def toggle_selector(event):
print(' Key pressed: {}'.format(event.key))
if event.key in ['D', 'd'] and RS.active:
print(' RectangleSelector deactivated.')
RS.set_active(False)
RS.set_visible(False)
RS.update()
if event.key in ['A', 'a'] and not RS.active:
print(' RectangleSelector activated.')
RS.set_active(True)
RS.set_visible(True)
RS.update()
def mycallback(event):
if RS.active:
# print('mycallback')
RS.update()
# def persist_rect(newlims):
# print('persist_rect')
# RS.set_visible(True)
# RS.update()
fig, ax = plt.subplots()
# figtype = type(fig)
# figtype._draw = figtype.draw
# def mydraw(self, renderer):
# print('figure.draw')
# self._draw(renderer)
# figtype.draw = mydraw
N = 100000
x = np.linspace(0.0, 10.0, N)
RS = RectangleSelector(ax, line_select_callback,
drawtype='box', useblit=True,
button=[1, 3], # don't use middle button
minspanx=5, minspany=5,
spancoords='pixels',
interactive=True)
plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7)
plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5)
plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3)
plt.connect('key_press_event', toggle_selector)
plt.connect('draw_event', mycallback)
# ax.callbacks.connect('xlim_changed', persist_rect)
# ax.callbacks.connect('ylim_changed', persist_rect)
plt.show()
为什么 mycallback
有效但 persist_rect
无效?
如果您取消注释上面注释掉的语句,您将得到一些打印输出,看起来像这样:
figure.draw
mycallback
figure.draw
mycallback
(4.09, -0.53) --> (8.15, 0.38)
The button you used were: 1 1
persist_rect
persist_rect
figure.draw
mycallback
Key pressed: q
注意 persist_rect
在 figure.draw
之前被调用,而 mycallback
在之后被调用。 figure.draw
不绘制 RectangleSelection
,但绘制用于背景的 Rectangle
。所以 figure.draw
遮盖了 RectangleSelection
。因此 persist_rect
会暂时显示 RectangleSelection
,但它无法持久化。mycallback
有效,因为它是在 figure.draw
之后调用的。
关于python - RectangleSelector 在缩放时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45531164/
当我运行 this示例并创建一个矩形选区,如果我缩放或移动选区周围的绘图窗口消失,直到我取消选择移动或缩放工具并再次单击绘图窗口。 我在 IPython 笔记本中使用 %matplotlib tkin
矩形选区概述 矩形选区是一种常见的对象选择方式,这个名词最常见于photoshop中,用于在一个子图选择鼠标拖动的矩形区域中的元素,在matplotlib中的矩形选区属于部件(widgets),m
我正在做一个项目,我想在图像上选择一个区域,以便对该区域进行一些处理。所以我找到了matplotlib这个神奇的工具RectangleSelector。但我想要的是将矩形设置在图像的初始位置,而不是等
我是一名优秀的程序员,十分优秀!