python - matplotlib 图形的选项卡式窗口,这可能吗?

标签 python matplotlib plot multipage tabbed

我有一个 python 项目,输出几个 Matplotlib 图形;每个图包含几个图表。项目每次运行启动大约15个数字(窗口)的问题,我无法减少。

是否可以将所有这些图形(窗口)连接到一个选项卡式窗口,以便每个选项卡代表一个图形?

非常感谢任何帮助。

提前致谢


解决方法

感谢@mobiusklein 下面的评论,他提出了一个解决方法,将图形导出为 myltipage pdf 文件,如图所示 here .

关于上述多页 pdf 示例的重要说明

我试过了,但在 matplotlib 中使用 LaTeX 时遇到错误。因为修复这个错误超出了这个问题的范围,所以我建议如果有人遇到这种情况,设置 plt.rc('text', usetex=False) 而不是 usetex=True

我仍然希望如果有人有其他解决方案或解决方法来发布它以造福他人。

最佳答案

我为 matplotlib 编写了一个简单的包装器,它可以执行您描述的操作。不过,您需要 pyqt5 才能正常工作。

这是代码,您构建一个 plotWindow 对象并为其提供图形句柄。它将为每个图创建一个新选项卡。

import matplotlib
# prevent NoneType error for versions of matplotlib 3.1.0rc1+ by calling matplotlib.use()
# For more on why it's nececessary, see
# https://stackoverflow.com/questions/59656632/using-qt5agg-backend-with-matplotlib-3-1-2-get-backend-changes-behavior
matplotlib.use('qt5agg')

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QTabWidget, QVBoxLayout
import matplotlib.pyplot as plt
import sys

class plotWindow():
    def __init__(self, parent=None):
        self.app = QApplication(sys.argv)
        self.MainWindow = QMainWindow()
        self.MainWindow.__init__()
        self.MainWindow.setWindowTitle("plot window")
        self.canvases = []
        self.figure_handles = []
        self.toolbar_handles = []
        self.tab_handles = []
        self.current_window = -1
        self.tabs = QTabWidget()
        self.MainWindow.setCentralWidget(self.tabs)
        self.MainWindow.resize(1280, 900)
        self.MainWindow.show()

    def addPlot(self, title, figure):
        new_tab = QWidget()
        layout = QVBoxLayout()
        new_tab.setLayout(layout)

        figure.subplots_adjust(left=0.05, right=0.99, bottom=0.05, top=0.91, wspace=0.2, hspace=0.2)
        new_canvas = FigureCanvas(figure)
        new_toolbar = NavigationToolbar(new_canvas, new_tab)

        layout.addWidget(new_canvas)
        layout.addWidget(new_toolbar)
        self.tabs.addTab(new_tab, title)

        self.toolbar_handles.append(new_toolbar)
        self.canvases.append(new_canvas)
        self.figure_handles.append(figure)
        self.tab_handles.append(new_tab)

    def show(self):
        self.app.exec_()

if __name__ == '__main__':
    import numpy as np

    pw = plotWindow()

    x = np.arange(0, 10, 0.001)

    f = plt.figure()
    ysin = np.sin(x)
    plt.plot(x, ysin, '--')
    pw.addPlot("sin", f)

    f = plt.figure()
    ycos = np.cos(x)
    plt.plot(x, ycos, '--')
    pw.addPlot("cos", f)
    pw.show()

这也发布在:https://github.com/superjax/plotWindow

希望这对您的应用程序来说是一个很好的起点。

关于python - matplotlib 图形的选项卡式窗口,这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37346845/

相关文章:

python - 使用 psycopg2 将数据从 python 写入 postgreSQL 时遇到问题

python - Django 1.5 : Display foreignkey values in both admin models

python - 更新 matplotlib 中的图形坐标

python - 如何在 Python 中对 Matplotlib 进行线程化?

python - pandas 版本中的索引错误 >= 0.20

python - Pandas - DataFrame 将列或旋转列转换为新行

python - Databricks 笔记本挂着 pytorch

r - 我可以将 r 中的基本图转换为 ggplot 对象吗?

python - 绘制 pandas 列的直方图

python - 将 matplotlib 3D 图形的框架更改为 x、y 和 z 箭头