gpt4 book ai didi

python - 如何将 matplotlib 编辑选项卡添加到 Canvas 中显示的 seaborn 图中?

转载 作者:行者123 更新时间:2023-12-01 01:46:17 27 4
gpt4 key购买 nike

当在 gui 中集成绘图并使用 Canvas 时,我的绘图在我只有“plt.show()”时得到的绘图底部没有编辑选项卡。如何将左侧图中的底部编辑选项卡添加到右侧图中?

编辑(一旦实现了工具栏,但仍然存在一些问题):

我使用堆叠布局在窗口之间切换。下面完整显示的两个函数是负责窗口的下拉菜单和绘图,并在选择新的下拉选项时更新绘图。

工具栏基于 https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html (感谢欧内斯特的重要性)

简化代码(可以单独运行): 导入系统

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()

主窗口:

enter image description here

点击“Graph DropDow......”:

enter image description here

点击“返回主页”(应该与第一步中的主窗口相同):

enter image description here

最佳答案

回答最初的问题:

您忘记将导航工具栏添加到 GUI 中。例如。对于 PyQt

matplotlib.backends.backend_qt5agg.NavigationToolbar2QT

或 Tk

matplotlib.backends.backend_tkagg.NavigationToolbar2Tk

等等。您可以引用the examples on the matplotlib page适用于所有可能的 GUI 后端。

添加导航栏的方式取决于所使用的GUI, 这是问题中的 secret ,但您将从相应的示例中找到答案。

回答已编辑的问题:

您正在向窗口添加导航工具栏,而不是 StackedLayout。因此,您需要将其添加到正在使用的布局中,而不是 self.addToolBar(Qt.BottomToolBarArea, NavToolbar(self.canvas, self))

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

关于python - 如何将 matplotlib 编辑选项卡添加到 Canvas 中显示的 seaborn 图中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51296949/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com