簡體   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