简体   繁体   English

如何将matplotlib编辑选项卡添加到画布中显示的海洋图中?

[英]How to add matplotlib editing tab to seaborn plots displayed within a canvas?

When integrating plots within a gui and using the canvas my plots come without the editing tab at the bottom of plots I get when I simply have "plt.show()". 当在gui中集成图并使用画布时,我的图在图的底部没有“编辑”选项卡,而我只有“ plt.show()”时得到。 How can I add the bottom editing tab in the plot on the left to the plot on the right? 如何在左侧图中添加底部编辑选项卡到右侧图中?

Edit (once toolbar is implemented but still have some problems): 编辑(实现了工具栏,但仍然存在一些问题):

I use stacked layout to switch in between windows. 我使用堆叠式布局在窗口之间切换。 the two functions fully displayed below are the one's in charge of the window with a drop down menu and plot and updating the plot when a new dropdown option is chosen. 下面完全显示的两个功能是负责下拉菜单的窗口,并在选择新的下拉选项时绘图并更新绘图。

Toolbar is based on https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html (thanks to ImportanceofBeingErnest) 工具栏基于https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html (感谢ImportanceofBeingErnest)

Simplified Code (can run on its own): import sys 简化代码(可以单独运行):import sys

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavToolbar
import seaborn as sns

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.setWindowTitle("Title Window (not sure)")

        self.stacked_layout = QStackedLayout()
        self.stacked_counter = -1

        self.central_widget = QWidget()
        self.central_widget.setLayout(self.stacked_layout)
        self.setCentralWidget(self.central_widget)

        self.main_window_layout()

    def main_window_layout(self):

        self.bargraph_dropdown_menu_button = QPushButton("Go To Bar Graph DropDown Menu")

        self.main_menu_button_layout = QHBoxLayout()
        self.main_menu_button_layout.addWidget(self.bargraph_dropdown_menu_button)

        self.main_menu_layout = QVBoxLayout()
        self.main_menu_layout.addLayout(self.main_menu_button_layout)

        self.view_main_menu_widget = QWidget()
        self.view_main_menu_widget.setLayout(self.main_menu_layout)

        #connections
        self.bargraph_dropdown_menu_button.clicked.connect(self.dropdown_menu_layout)

        self.stacked_layout.addWidget(self.view_main_menu_widget)
        self.stacked_counter += 1
        self.stacked_layout.setCurrentIndex(self.stacked_counter)


    def dropdown_menu_layout(self):
        self.setMinimumSize(550,350)
        self.setMaximumSize(100000,100000)

        self.homebutton = QPushButton('Back Home')
        self.homebutton.setFixedSize(70,22)

        self.figure = plt.figure(figsize=(15,5))
        self.canvas = FigureCanvas(self.figure)
        def on_resize(event):
            plt.tight_layout()
            self.canvas.draw()
        cid = self.canvas.mpl_connect('resize_event', on_resize)

        self.addToolBar(Qt.BottomToolBarArea, NavToolbar(self.canvas, self))

        sns.set(style="whitegrid")
        plt.cla()
        ax = self.figure.add_subplot(111)
        ax.set_title('Droops and Power System Parameters to Bus Sensitivity', fontweight=1)
        ax.title.set_position([.5, 1.025])
        ax.set_ylabel("Sensitivity", fontweight=1)
        ax.yaxis.labelpad = 15
        plt.tight_layout()
        sns.despine(bottom=False,top=False,left=False,right=False)
        self.canvas.draw()

        self.button_hbox_layout = QHBoxLayout()
        self.button_hbox_layout.addWidget(self.homebutton)
        self.button_hbox_layout.addStretch(1)

        self.dropdown_vbox_layout = QVBoxLayout()
        self.dropdown_vbox_layout.addLayout(self.button_hbox_layout)
        self.dropdown_vbox_layout.addWidget(self.canvas)

        self.view_dropdown_menu_widget = QWidget()
        self.view_dropdown_menu_widget.setLayout(self.dropdown_vbox_layout)

        #connections
        self.homebutton.clicked.connect(self.change_to_main_menu_window)

        self.stacked_layout.addWidget(self.view_dropdown_menu_widget)
        self.stacked_counter += 1
        self.stacked_layout.setCurrentIndex(self.stacked_counter)

    def change_to_main_menu_window(self):
        self.setFixedSize(300,75)
        self.stacked_layout.setCurrentIndex(0)


def main():
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    win.raise_()
    app.exec_()   #sys.exit(app.exec_())

if __name__ == "__main__":
    main()

Main Window: 主视窗:

在此处输入图片说明

Click "Graph DropDow......": 点击“ Graph DropDow……”:

在此处输入图片说明

Click "Back Home" (Supposed to be the same as the Main Window in step one): 单击“返回首页”(假定与第一步中的主窗口相同):

在此处输入图片说明

Answer to initial question: 最初问题的答案:

You forgot to add the navigation toolbar to your GUI. 您忘记了将导航工具栏添加到GUI。 Eg for PyQt 例如PyQt

matplotlib.backends.backend_qt5agg.NavigationToolbar2QT

or for Tk 或Tk

matplotlib.backends.backend_tkagg.NavigationToolbar2Tk

etc. You may refer to the examples on the matplotlib page for all possible GUI backends. 等等。有关所有可能的GUI后端,您可以参考matplotlib页面上的示例

The way how to add the navigation bar depends on the GUI in use, which is a secret in the question, but you will find out from the respective example. 添加导航栏的方式取决于所使用的GUI, 这在问题中是个秘密,但是您可以从相应的示例中找到。

Answer to edited question: 已编辑问题的答案:

You are adding the navigation toolbar to the window, not the StackedLayout. 您正在将导航工具栏添加到窗口,而不是StackedLayout。 So instead of self.addToolBar(Qt.BottomToolBarArea, NavToolbar(self.canvas, self)) you need to add it to the layout in use, 因此self.addToolBar(Qt.BottomToolBarArea, NavToolbar(self.canvas, self))您需要将其添加到使用的布局中,而不是self.addToolBar(Qt.BottomToolBarArea, NavToolbar(self.canvas, self))

self.dropdown_vbox_layout.addWidget(NavToolbar(self.canvas, self))

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

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