Matplotlib – 套索选择器小工具
Matplotlib 为我们提供了各种小部件。在本文中,我们将学习Lasso Selector Widget Demo 。 Lasso Selector Widget 是一种帮助我们制作任意空间选择曲线的工具。
方法#1
我们将手动将轴添加到我们的绘图中,然后使用 lasso-selector-widget 工具。
执行:
Python3
# importing matplotlib package
import matplotlib.pyplot as plt
# importing LassoSelector from
# Matplotlib.widgets
from matplotlib.widgets import LassoSelector
# Creating a figure of the plot
fig = plt.figure()
# Add set of axes to figure(Manually)
# left, bottom, width, height (ranging in between 0 and 1)
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
# Set the label of X-Axis
axes.set_xlabel('X-axis')
# Set the label of Y-Axis
axes.set_ylabel('Y-Axis')
# Set the title of the plot
axes.set_title('LassoSelector-Demo-Widget')
# OnSelect function:Gets triggered
# as soon as the mouse is pressed
# in the plot
def onSelect(geeksforgeeks):
print(geeksforgeeks)
# line defines the color, width and opacity
# of the line to be drawn
line = {'color': 'green',
'linewidth': 8, 'alpha': 1}
# Three parameters are passed inside the lasso
# Selector class defining the axis, line
# property and on select function
lsso = LassoSelector(ax=axes, onselect=onSelect,
lineprops=line, button=2)
# Show the above plot
plt.show()
Python3
# importing matplotlib package
import matplotlib.pyplot as plt
# importing LassoSelector from
# Matplotlib.widgets
from matplotlib.widgets import LassoSelector
# Creating a Subplot in matplotlib
fig, axes = plt.subplots()
# Set the label of X-Axis
axes.set_xlabel('X-axis')
# Set the label of Y-Axis
axes.set_ylabel('Y-Axis')
# Set the title of the plot
axes.set_title(
'LassoSelector-Demo-Widget with axes created automatically with subplots')
# onSelect function gets triggered
# as soon as the mouse is pressed
# in the plot
def onSelect(geeksforgeeks):
print(geeksforgeeks)
# line defines the color, width and opacity
# of the line to be drawn
line = {'color': 'green',
'linewidth': 8, 'alpha': 1}
# Three parameters are passed inside the lasso
# Selector class defining the axis, line
# property and on select function
lsso = LassoSelector(ax=axes, onselect=onSelect,
lineprops=line, button=2)
# If you want to print x and y while pressing
# and releasing mouse, then use mpl_connect
# and replace pressed and released with event
# Shows the above plot
plt.show()
输出:
解释:
在上面的代码中,我们将matplotlib包与来自matplotlib.widgets模块的LassoSelector工具一起导入到我们的Python项目中。导入包后,我们正在创建一个图形(即一个空画布)并手动为其添加轴。然后,我们定义了一个onSelect()函数,只要在绘图中按下鼠标,它就会被触发。然后,我们正在创建定义线属性的线,然后是LassoSelector ,它帮助我们在图中绘制。现在LassoSelector内部有四个参数,第一个定义我们创建的轴,第二个定义onSelect()函数,第三个参数定义 line(line) 的属性,最后一个参数定义点击哪个鼠标将用于绘制plot(left, right, middle) 。
方法#2
在这里,我们也可以使用plt.subplots自动创建轴,而不是手动添加轴。
蟒蛇3
# importing matplotlib package
import matplotlib.pyplot as plt
# importing LassoSelector from
# Matplotlib.widgets
from matplotlib.widgets import LassoSelector
# Creating a Subplot in matplotlib
fig, axes = plt.subplots()
# Set the label of X-Axis
axes.set_xlabel('X-axis')
# Set the label of Y-Axis
axes.set_ylabel('Y-Axis')
# Set the title of the plot
axes.set_title(
'LassoSelector-Demo-Widget with axes created automatically with subplots')
# onSelect function gets triggered
# as soon as the mouse is pressed
# in the plot
def onSelect(geeksforgeeks):
print(geeksforgeeks)
# line defines the color, width and opacity
# of the line to be drawn
line = {'color': 'green',
'linewidth': 8, 'alpha': 1}
# Three parameters are passed inside the lasso
# Selector class defining the axis, line
# property and on select function
lsso = LassoSelector(ax=axes, onselect=onSelect,
lineprops=line, button=2)
# If you want to print x and y while pressing
# and releasing mouse, then use mpl_connect
# and replace pressed and released with event
# Shows the above plot
plt.show()
输出:
解释:
与我们之前的方法一样,正在发生一系列相同的事件。唯一的区别是这里我们在plt.subplots()的帮助下自动创建轴。如果您通过终端,您可以看到大量坐标,这些坐标是我们在图形中绘制的点。