Python Matplotlib "ginput" 可以独立于 "zoom to rectangle" 吗?
can Python Matplotlib "ginput" be independent from "zoom to rectangle"?
我正在使用 ginput
沿着时间信号对几个点进行图形选择。有时,当信号太密集时,在选择点之前放大一个区域可能很有用。我的问题是 zoom to rectangle
选项似乎在 ginput
中被考虑在内。
例如,使用此示例代码:
from __future__ import print_function
from pylab import arange, plot, sin, ginput, show
import numpy as np
t = np.linspace(0,25,500)
plot(t, sin(t))
x = ginput(3)
print("clicked",x)
show()
如果我放大信号的一部分,为缩放区域选择所做的点击被计入 ginput
...有没有办法避免这种情况并使缩放区域选择独立于ginput
?
由于我没有得到任何答案,而且我找不到太多关于该主题的信息,所以我是这样解决这个问题的:我添加了一个允许用户放大所选区域的前置程序。当该区域适合点选时,用户只需按"Enter"并继续ginput
。
from __future__ import print_function
from pylab import arange, plot, sin, ginput, show
import matplotlib as plt
from pylab import *
import numpy as np
def closest_point(vec,val):
ind = np.where(vec==min(vec,key=lambda x:abs(x-val)))
return ind[0][0]
t = np.linspace(0,250,5000)
y = sin(t)
fig = plt.figure()
plt.suptitle('chosing points over a possibly dense signal...',fontsize=14)
zoom_ok = False
while not zoom_ok:
plt.plot(t,y)
plt.title('1 - click on the two corners of the area to enlarge',fontsize=12)
zoom = ginput(2,timeout=-1)
xzoom = np.zeros([0])
for i in range(2):
xzoom = np.concatenate((xzoom,[int(closest_point(t,zoom[i][0]))]))
temps_zoom = t[xzoom[0]:xzoom[1]]
dep_zoom = y[xzoom[0]:xzoom[1]]
plt.clf()
plt.plot(temps_zoom,dep_zoom,'b')
plt.title('2 - if zoom ok -> press Enter, otherwise -> mouse click',fontsize=12)
zoom_ok = plt.waitforbuttonpress()
plt.title('3 - you may now select the points ',fontsize=16)
x = ginput(2,timeout=-1)
plt.show()
我遇到了同样的问题,我的解决方法是编写一个使用鼠标滚轮进行缩放的函数,这样我就可以在不单击的情况下进行缩放。我从这个页面获得了功能:
Matplotlib plot zooming with scroll wheel
并修改了函数,使其不会缩放到超过原始 x 和 y 限制:
def zoom_factory(ax, max_xlim, max_ylim, base_scale = 2.):
def zoom_fun(event):
# get the current x and y limits
cur_xlim = ax.get_xlim()
cur_ylim = ax.get_ylim()
xdata = event.xdata # get event x location
ydata = event.ydata # get event y location
if event.button == 'up':
# deal with zoom in
scale_factor = 1/base_scale
x_scale = scale_factor / 2
elif event.button == 'down':
# deal with zoom out
scale_factor = base_scale
x_scale = scale_factor * 2
else:
# deal with something that should never happen
scale_factor = 1
print(event.button)
# set new limits
new_width = (cur_xlim[1] - cur_xlim[0]) * x_scale
new_height = (cur_ylim[1] - cur_ylim[0]) * scale_factor
relx = (cur_xlim[1] - xdata) / (cur_xlim[1] - cur_xlim[0])
rely = (cur_ylim[1] - ydata) / (cur_ylim[1] - cur_ylim[0])
if xdata - new_width * (1 - relx) > max_xlim[0]:
x_min = xdata - new_width * (1 - relx)
else:
x_min = max_xlim[0]
if xdata + new_width * (relx) < max_xlim[1]:
x_max = xdata + new_width * (relx)
else:
x_max = max_xlim[1]
if ydata - new_height * (1 - rely) > max_ylim[0]:
y_min = ydata - new_height * (1 - rely)
else:
y_min = max_ylim[0]
if ydata + new_height * (rely) < max_ylim[1]:
y_max = ydata + new_height * (rely)
else:
y_max = max_ylim[1]
ax.set_xlim([x_min, x_max])
ax.set_ylim([y_min, y_max])
ax.figure.canvas.draw()
fig = ax.get_figure() # get the figure of interest
# attach the call back
fig.canvas.mpl_connect('scroll_event',zoom_fun)
#return the function
return zoom_fun
所以对于给定的数字:
t = np.linspace(0,250,5000)
y = sin(t)
fig = figure()
ax = fig.add_subplot(111)
plt.plot(t,y)
max_xlim = ax.get_xlim() # get current x_limits to set max zoom out
max_ylim = ax.get_ylim() # get current y_limits to set max zoom out
f = zoom_factory(ax, max_xlim, max_ylim, base_scale=1.1)
show()
x = ginput(3)
我使用的解决方法是将鼠标按钮参数设置为 None
:
plt.ginput(n=0, timeout=0, mouse_add=None, mouse_pop=None, mouse_stop=None)
然后我使用键盘到add/pop/stop。 (要添加一个点,请将鼠标移动到正确的位置并按 Spacebar
或 j
等。使用 Delete
删除最后一个点并使用 Enter
停止。)这样,当我用鼠标使用矩形缩放时,我没有得到不需要的点。
当然,您尝试使用的某些键盘按键可能会产生其他 window/zoom-related 副作用。 (例如 Backspace
会弹出并重置缩放),因此您必须小心。
我正在使用 ginput
沿着时间信号对几个点进行图形选择。有时,当信号太密集时,在选择点之前放大一个区域可能很有用。我的问题是 zoom to rectangle
选项似乎在 ginput
中被考虑在内。
例如,使用此示例代码:
from __future__ import print_function
from pylab import arange, plot, sin, ginput, show
import numpy as np
t = np.linspace(0,25,500)
plot(t, sin(t))
x = ginput(3)
print("clicked",x)
show()
如果我放大信号的一部分,为缩放区域选择所做的点击被计入 ginput
...有没有办法避免这种情况并使缩放区域选择独立于ginput
?
由于我没有得到任何答案,而且我找不到太多关于该主题的信息,所以我是这样解决这个问题的:我添加了一个允许用户放大所选区域的前置程序。当该区域适合点选时,用户只需按"Enter"并继续ginput
。
from __future__ import print_function
from pylab import arange, plot, sin, ginput, show
import matplotlib as plt
from pylab import *
import numpy as np
def closest_point(vec,val):
ind = np.where(vec==min(vec,key=lambda x:abs(x-val)))
return ind[0][0]
t = np.linspace(0,250,5000)
y = sin(t)
fig = plt.figure()
plt.suptitle('chosing points over a possibly dense signal...',fontsize=14)
zoom_ok = False
while not zoom_ok:
plt.plot(t,y)
plt.title('1 - click on the two corners of the area to enlarge',fontsize=12)
zoom = ginput(2,timeout=-1)
xzoom = np.zeros([0])
for i in range(2):
xzoom = np.concatenate((xzoom,[int(closest_point(t,zoom[i][0]))]))
temps_zoom = t[xzoom[0]:xzoom[1]]
dep_zoom = y[xzoom[0]:xzoom[1]]
plt.clf()
plt.plot(temps_zoom,dep_zoom,'b')
plt.title('2 - if zoom ok -> press Enter, otherwise -> mouse click',fontsize=12)
zoom_ok = plt.waitforbuttonpress()
plt.title('3 - you may now select the points ',fontsize=16)
x = ginput(2,timeout=-1)
plt.show()
我遇到了同样的问题,我的解决方法是编写一个使用鼠标滚轮进行缩放的函数,这样我就可以在不单击的情况下进行缩放。我从这个页面获得了功能:
Matplotlib plot zooming with scroll wheel
并修改了函数,使其不会缩放到超过原始 x 和 y 限制:
def zoom_factory(ax, max_xlim, max_ylim, base_scale = 2.):
def zoom_fun(event):
# get the current x and y limits
cur_xlim = ax.get_xlim()
cur_ylim = ax.get_ylim()
xdata = event.xdata # get event x location
ydata = event.ydata # get event y location
if event.button == 'up':
# deal with zoom in
scale_factor = 1/base_scale
x_scale = scale_factor / 2
elif event.button == 'down':
# deal with zoom out
scale_factor = base_scale
x_scale = scale_factor * 2
else:
# deal with something that should never happen
scale_factor = 1
print(event.button)
# set new limits
new_width = (cur_xlim[1] - cur_xlim[0]) * x_scale
new_height = (cur_ylim[1] - cur_ylim[0]) * scale_factor
relx = (cur_xlim[1] - xdata) / (cur_xlim[1] - cur_xlim[0])
rely = (cur_ylim[1] - ydata) / (cur_ylim[1] - cur_ylim[0])
if xdata - new_width * (1 - relx) > max_xlim[0]:
x_min = xdata - new_width * (1 - relx)
else:
x_min = max_xlim[0]
if xdata + new_width * (relx) < max_xlim[1]:
x_max = xdata + new_width * (relx)
else:
x_max = max_xlim[1]
if ydata - new_height * (1 - rely) > max_ylim[0]:
y_min = ydata - new_height * (1 - rely)
else:
y_min = max_ylim[0]
if ydata + new_height * (rely) < max_ylim[1]:
y_max = ydata + new_height * (rely)
else:
y_max = max_ylim[1]
ax.set_xlim([x_min, x_max])
ax.set_ylim([y_min, y_max])
ax.figure.canvas.draw()
fig = ax.get_figure() # get the figure of interest
# attach the call back
fig.canvas.mpl_connect('scroll_event',zoom_fun)
#return the function
return zoom_fun
所以对于给定的数字:
t = np.linspace(0,250,5000)
y = sin(t)
fig = figure()
ax = fig.add_subplot(111)
plt.plot(t,y)
max_xlim = ax.get_xlim() # get current x_limits to set max zoom out
max_ylim = ax.get_ylim() # get current y_limits to set max zoom out
f = zoom_factory(ax, max_xlim, max_ylim, base_scale=1.1)
show()
x = ginput(3)
我使用的解决方法是将鼠标按钮参数设置为 None
:
plt.ginput(n=0, timeout=0, mouse_add=None, mouse_pop=None, mouse_stop=None)
然后我使用键盘到add/pop/stop。 (要添加一个点,请将鼠标移动到正确的位置并按 Spacebar
或 j
等。使用 Delete
删除最后一个点并使用 Enter
停止。)这样,当我用鼠标使用矩形缩放时,我没有得到不需要的点。
当然,您尝试使用的某些键盘按键可能会产生其他 window/zoom-related 副作用。 (例如 Backspace
会弹出并重置缩放),因此您必须小心。