繁体   English   中英

按钮在轴上的位置(matplotlib)

[英]Button positioning in axes (matplotlib)

我有一个带有许多补丁的现有图(轴)。 我希望在现有轴上添加一些按钮。 如果我编写以下代码,它将使整个轴成为按钮,即在轴上的任何位置都检测到单击。

# ax is the reference to axes containing many patches
bSend = Button(ax, 'send')
bSend.on_clicked(fu)

matplotlib给出的示例不使用现有轴,而是使用新轴(?)

# Create axes
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])

# Make Buttons of those axes.
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)

有没有一种方法可以将Button定位在现有轴上

matplotlib.widgets.Button位于其自己的轴中,​​您需要通过第一个参数来提供。 因此,您需要在某处创建轴。

根据您要实现的目标,您可以简单地选择轴内的坐标,

button_ax = plt.axes([0.4, 0.5, 0.2, 0.075])  #posx, posy, width, height
Button(button_ax, 'Click me')

此处的坐标以图形的宽度和高度为单位。 因此,将以图形宽度的40%,图形高度的50%创建按钮,宽度为20%,高度为7.5%。

在此处输入图片说明

或者,您可以使用InsetPosition将按钮轴相对于子图轴InsetPosition

import matplotlib.pyplot as plt
from  matplotlib.widgets import Button
from mpl_toolkits.axes_grid1.inset_locator import InsetPosition

fig, ax= plt.subplots()

button_ax = plt.axes([0, 0, 1, 1])
ip = InsetPosition(ax, [0.4, 0.5, 0.2, 0.1]) #posx, posy, width, height
button_ax.set_axes_locator(ip)
Button(button_ax, 'Click me')

plt.show()

在此,按钮的位置为轴宽度的40%,高度的50%,轴宽度的20%和高度8%。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM