当我从事与工作相关的项目时,我了解到这个可怕的库中许多与GUI相关的主题都没有很好的文档来支持交互程序。
经过几个小时的收集和调试,我决定分享以下发现:
我相信这会对其他程序员有所帮助!
发布于 2019-09-10 16:33:40
1.如何将按钮放置在绘图区域的下面.
如例句所示。其思想是在图形坐标中创建一个轴,即在主轴之下。fig.subplotpars.bottom给出了主轴的y坐标。所以在
ax_button = plt.axes([x0, y0, width, height])只要y0 + height < fig.subplotpars.bottom按钮在轴下。
2.如何为按钮和鼠标单击声明处理程序.
按钮可以通过.on_clicked注册回调。其中的任何函数只有在单击按钮时才会被调用。
一般的'button_press_event'需要确保只有在选择的轴内单击时才能做一些事情。
def on_mouse_click(event):
if event.inaxes == ax:
# do something
cid = fig.canvas.mpl_connect('button_press_event', on_mouse_click)3.如何知道在上、下图或其中一个按钮中是否发生了单击
非常类似于2:如果您有4个轴,ax1、ax2、ax_button1、ax_button2,只需检查event.inaxes的值,就可以知道事件发生在哪个轴上。
def on_mouse_click(event):
if event.inaxes == ax1:
# do something
elif event.inaxes == ax2:
# do something else4.如何对多个按钮使用相同的按钮单击处理程序.
与上面类似,您可以检查事件发生在哪个轴中。
def on_button_click(event):
if event.inaxes == btn1.ax:
# do something
elif event.inaxes == btn2.ax:
# do something else
btn1.on_clicked(on_button_click)
btn2.on_clicked(on_button_click)或者,您可以将标识符传递给函数。
def on_button_click(event, id):
if id == "button1":
# do something
elif id == "button2":
# do something else
btn1.on_clicked(lambda e: on_button_click(e, "button1")
btn2.on_clicked(lambda e: on_button_click(e, "button2")5.如何定位绘图窗口.
这是不相关的,在如何用matplotlib设置图形窗口的绝对位置?中回答,它将取决于正在使用的后端。
完整的例子:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
def on_mouse_click(event):
if event.inaxes == ax1:
client = 'upper'
if event.inaxes == ax2:
client = 'lower'
else:
client = 'none'
print(client)
def on_button_click(btn):
print(btn)
if btn == 'Upper':
ax1.clear()
elif btn == 'Lower':
ax2.clear()
fig.canvas.draw_idle()
# First create some toy data:
x = np.linspace(0, 2*np.pi, 400)
y1 = np.sin(x**2)
y2 = np.cos(x**2)
# Creates two subplots and unpacks the output array immediately
fig, (ax1, ax2) = plt.subplots(num=1, ncols=1, nrows=2, figsize=(5,8))
ax1.plot(x, y1)
ax2.plot(x, y2)
ax1.set_title('Upper')
ax2.set_title('Lower')
cid = fig.canvas.mpl_connect('button_press_event', on_mouse_click)
ax_button1 = fig.add_axes([0.69, 0.01, 0.1, 0.055])
ax_button2 = fig.add_axes([0.80, 0.01, 0.1, 0.055])
btn1 = Button(ax_button1, "Upper")
btn1.on_clicked(lambda e: on_button_click("Upper"))
btn2 = Button(ax_button2, "Lower")
btn2.on_clicked(lambda e: on_button_click("Lower"))
plt.show()发布于 2019-09-10 15:41:35
以下代码基于这里提供的示例:https://matplotlib.org/3.1.0/gallery/widgets/buttons.html
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
ax1 = ax2 = 0
ax1_first_y = ax2_first_y = 0
def on_mouse_click(event):
if event.xdata: # either in graph or in button
if event.y >= ax1_first_y:
client = 'upper'
elif event.y >= ax2_first_y:
client = 'lower'
else:
client = 'none' # probably a button
print('client=%s, x=%d, y=%d, xdata=%f, ydata=%f' %
(client,
event.x, event.y, event.xdata, event.ydata))
else: # no xdata, meaning clicked out of any widget
wid = ax2.get_window_extent().width
print('%s click: button=%d, x=%d, y=%d, p1=%.1f' %
('double' if event.dblclick else 'single', event.button,
event.x, event.y, event.x/wid))
def on_button_click(event):
txt = event.inaxes.properties()['children'][0].get_text()
if txt == 'Upper':
ax1.clear()
plt.show()
elif txt == 'Lower':
ax2.clear()
plt.show()
else:
print('unknown text: '+txt)
# First create some toy data:
x = np.linspace(0, 2*np.pi, 400)
y1 = np.sin(x**2)
y2 = np.cos(x**2)
# Creates two subplots and unpacks the output array immediately
fig, (ax1, ax2) = plt.subplots(num=1,ncols=1, nrows=2, figsize=(5,8))
ax1_first_y = ax1.transAxes.frozen().get_matrix()[1][2]
ax2_first_y = ax2.transAxes.frozen().get_matrix()[1][2]
ax1.plot(x, y1)
ax2.plot(x, y2)
ax1.set_title('Upper')
ax2.set_title('Lower')
mngr = plt.get_current_fig_manager()
mngr.set_window_title('Auto-focus Scan Results')
mngr.window.wm_geometry('+500+0')
cid = fig.canvas.mpl_connect('button_press_event', on_mouse_click)
plot_buttons = [[plt.axes([0.7, 0.01, 0.1, 0.055]), 'Upper'],
[plt.axes([0.81, 0.01, 0.1, 0.055]), 'Lower']
]
all_buttons = list() # we must keep a pointer to buttons or else they will not function correctly
for b in plot_buttons:
btn = Button(b[0], b[1])
btn.on_clicked(on_button_click)
all_buttons.append(btn)
plt.show()https://stackoverflow.com/questions/57874157
复制相似问题