matplotlib 中的条件按钮按下事件
Conditional Button-press-event in matplotlib
我正在尝试编写一段代码,如果选中一个复选框,它只会 运行 一个按钮单击事件。
更详细地说,按钮按下事件记录了鼠标点击坐标,并在点击坐标处的绘图上绘制了一条垂直的红线。但是,如果复选框 'On' 被选中,我只希望它 运行。
谁能帮我修复下面的代码?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
import ipywidgets as widgets
from ipywidgets import interactive
#mouse click function to store coordinates and plot vertical red line
def onclick(event):
if event == 'On':
global ix,iy
ix, iy = event.xdata, event.ydata
global click_count
click_count.append((ix,iy))
#plotting lines where clicks occur
if len(click_count) > 1:
ax1.axvline(x=ix, ymin=0, ymax=1, color = "red")
# assign global variable to access outside of function
global coords3
coords3.append((ix, iy))
# Disconnect after 12 clicks
if len(coords3) == 12:
fig.canvas.mpl_disconnect(cid)
plt.close(1)
return
#check box function
def func(label):
if label == 'On':
return 'On'
#plots the graph
x = range(0,10)
y = range(0,10)
fig = plt.figure(1)
ax1 = fig.add_subplot(111)
ax1.plot(x,y, color = "blue")
ax1.set_title('This is my graph')
#create the check box
ax2 = plt.axes([0.7, 0.05, 0.1, 0.075])
check_box= CheckButtons(ax2, ('On', 'Off'), (False, False))
#define check box function
check = check_box.on_clicked(func)
# calling out the click coordinates to a variable
coords3 = []
click_count = []
# Call click func
cid = fig.canvas.mpl_connect('button_press_event', onclick(check))
plt.show(1)
我想大概就是这个问题问的内容了。这个问题的代码有很多问题,我想我不能对所有问题发表评论,但一些主要问题是:
- 注册时不能调用回调函数。而且您不能简单地使用一些自定义参数。 matplotlib 事件的参数是
Event
s.
- 您需要在回调函数中查询复选框是否被选中。
- 如果要显示一条新的红线,您需要绘制 canvas。
- 对于二进制状态切换,单个复选框就足够了。否则就变得很复杂了。
- 您需要检查点击是否确实发生在坐标区内。这也要求复选框不在轴内,否则单击复选框也会产生一条线。
完整代码。
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
#mouse click function to store coordinates and plot vertical red line
def onclick(event):
# Only use event within the axes.
if not event.inaxes == ax1:
return
# Check if Checkbox is "on"
if check_box.get_status()[0]:
ix, iy = event.xdata, event.ydata
coords3.append((ix,iy))
#plotting lines where clicks occur
if len(coords3) >= 1:
ax1.axvline(x=ix, ymin=0, ymax=1, color = "red")
fig.canvas.draw_idle()
# Disconnect after 12 clicks
if len(coords3) >= 12:
fig.canvas.mpl_disconnect(cid)
plt.close(fig)
#plots the graph
x = range(0,10)
y = range(0,10)
fig, ax1 = plt.subplots()
ax1.plot(x,y, color = "blue")
ax1.set_title('This is my graph')
#create the check box
ax2 = plt.axes([0.7, 0.05, 0.1, 0.075])
check_box = CheckButtons(ax2, ['On',], [False,])
# List to store coordinates
coords3 = []
# Callback to click func
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
print(coords3)
我正在尝试编写一段代码,如果选中一个复选框,它只会 运行 一个按钮单击事件。
更详细地说,按钮按下事件记录了鼠标点击坐标,并在点击坐标处的绘图上绘制了一条垂直的红线。但是,如果复选框 'On' 被选中,我只希望它 运行。
谁能帮我修复下面的代码?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
import ipywidgets as widgets
from ipywidgets import interactive
#mouse click function to store coordinates and plot vertical red line
def onclick(event):
if event == 'On':
global ix,iy
ix, iy = event.xdata, event.ydata
global click_count
click_count.append((ix,iy))
#plotting lines where clicks occur
if len(click_count) > 1:
ax1.axvline(x=ix, ymin=0, ymax=1, color = "red")
# assign global variable to access outside of function
global coords3
coords3.append((ix, iy))
# Disconnect after 12 clicks
if len(coords3) == 12:
fig.canvas.mpl_disconnect(cid)
plt.close(1)
return
#check box function
def func(label):
if label == 'On':
return 'On'
#plots the graph
x = range(0,10)
y = range(0,10)
fig = plt.figure(1)
ax1 = fig.add_subplot(111)
ax1.plot(x,y, color = "blue")
ax1.set_title('This is my graph')
#create the check box
ax2 = plt.axes([0.7, 0.05, 0.1, 0.075])
check_box= CheckButtons(ax2, ('On', 'Off'), (False, False))
#define check box function
check = check_box.on_clicked(func)
# calling out the click coordinates to a variable
coords3 = []
click_count = []
# Call click func
cid = fig.canvas.mpl_connect('button_press_event', onclick(check))
plt.show(1)
我想大概就是这个问题问的内容了。这个问题的代码有很多问题,我想我不能对所有问题发表评论,但一些主要问题是:
- 注册时不能调用回调函数。而且您不能简单地使用一些自定义参数。 matplotlib 事件的参数是
Event
s. - 您需要在回调函数中查询复选框是否被选中。
- 如果要显示一条新的红线,您需要绘制 canvas。
- 对于二进制状态切换,单个复选框就足够了。否则就变得很复杂了。
- 您需要检查点击是否确实发生在坐标区内。这也要求复选框不在轴内,否则单击复选框也会产生一条线。
完整代码。
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
#mouse click function to store coordinates and plot vertical red line
def onclick(event):
# Only use event within the axes.
if not event.inaxes == ax1:
return
# Check if Checkbox is "on"
if check_box.get_status()[0]:
ix, iy = event.xdata, event.ydata
coords3.append((ix,iy))
#plotting lines where clicks occur
if len(coords3) >= 1:
ax1.axvline(x=ix, ymin=0, ymax=1, color = "red")
fig.canvas.draw_idle()
# Disconnect after 12 clicks
if len(coords3) >= 12:
fig.canvas.mpl_disconnect(cid)
plt.close(fig)
#plots the graph
x = range(0,10)
y = range(0,10)
fig, ax1 = plt.subplots()
ax1.plot(x,y, color = "blue")
ax1.set_title('This is my graph')
#create the check box
ax2 = plt.axes([0.7, 0.05, 0.1, 0.075])
check_box = CheckButtons(ax2, ['On',], [False,])
# List to store coordinates
coords3 = []
# Callback to click func
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
print(coords3)