Matplotlib Python:如何添加面板按钮 [英] Matplotlib Python: How to add panel button

查看:190
本文介绍了Matplotlib Python:如何添加面板按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib创建一个简单的交互式图,用户可以在该图上放置标记.为此,一切正常.

I am using matplotlib to create a simple interactive plot where the user will be able to place markers on the plot. For that matter everything works fine.

现在我想添加一个按钮,当按下某个功能时将执行该按钮.为此,我遵循了 example.但是使用该按钮会导致意外行为.由于包含了按钮,而不是能够添加标记,所以所有标记都放置在按钮区域内,并且根本不显示在图形中.这没有多大意义.

Now I want to add a button that when pressed a certain function will be executed. For that I followed that example. But using the button causes unexpected behavior. With the button included instead of being able to add markers all markers are placed inside the button area and are not displayed at all in the graph. Which doesn't make much sense.

我正在寻找一种方法来添加面板按钮,就像每个 matplotlib 窗口中默认存在的那些按钮一样.你有什么建议吗?还有其他我可以看的例子吗?我看过很多示例,但我发现很难浏览文档以准确找到我需要的内容.提前致谢.

I am looking for a way to add a panel button like those that exist by default in every matplotlib window. Do you have any suggestion? Any other example that I could take a look into? I have seen a lot of examples but I find to it hard to navigate through the documentation to find exactly what I need. Thanks in advance.

更新

我现在使用的代码如下:

The code I use right now looks like this:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

dataX = np.array([1,2,3,4,5,6,7,8,9,10])
dataY = np.array([1193,1225,1125,1644,1255,13676,2007,2008,12359,1210])

def on_click(event):
    if event.dblclick:
        plt.plot((event.xdata, event.xdata),(mean-standardDeviation, mean+standardDeviation), 'r-')
        plt.show()

def _yes(event):
    print "yolo"

global mean, standardDeviation

# mean and standard deviation
mean = np.mean(dataY)
standardDeviation = np.std(dataY)

# plot data
plt.plot(dataX, dataY, linewidth=0.5)

plt.connect('button_press_event', on_click)

# button
axcut = plt.axes([0.9, 0.0, 0.1, 0.075])
bcut = Button(axcut, 'YES', color='red', hovercolor='green')
bcut.on_clicked(_yes)

plt.show()

不添加按钮时,一切正常.使用按钮我只能在按钮区域内放置标记.有什么想法吗?

When the button is not added everything works as expected. With the button I can only place markers inside the button's area. Any idea?

推荐答案

您需要将两者分开.

让我们尝试使用子图:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

dataX = np.array([1,2,3,4,5,6,7,8,9,10])
dataY = np.array([1193,1225,1125,1644,1255,13676,2007,2008,12359,1210])

ax = plt.subplot(111)
def on_click(event):
    if event.dblclick:
       ax.plot((event.xdata, event.xdata), (mean-standardDeviation, mean+standardDeviation), 'r-')
       plt.show()

def _yes(event):
    print("yolo")

mean = np.mean(dataY)
standardDeviation = np.std(dataY)

ax.plot(dataX, dataY, linewidth=0.5)
plt.connect('button_press_event', on_click)

axcut = plt.axes([0.9, 0.0, 0.1, 0.075])
bcut = Button(axcut, 'YES', color='red', hovercolor='green')
bcut.on_clicked(_yes)

plt.show()

现在应该可以了.

但是,如果您偶然双击,它将在图形上绘制一条线.因此,如果将按钮操作更改为右键单击:

But, if you accidentally double click on yes it will draw a line on a graph. So, if you change button action to right-click:

def _yes(event):
    if event.button == 3:
        print("yolo")

现在好了:)

这篇关于Matplotlib Python:如何添加面板按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆