简体   繁体   English

如何在 QScrollArea 中绘制图形而不缩小它们?

[英]How to plot graphs in QScrollArea without shrinking them?

I'm creating an interface where I want to plot graphs in another tab by clicking in a button.我正在创建一个界面,我想通过单击按钮在另一个选项卡中绘制图形。 I want the graphs to be plotted side-by-side in a scroll area.我希望在滚动区域中并排绘制图形。 The problem is that when I plot the graphs, instead of the Scroll Bar working on the visualization, the graphs actually shrink to fit the widget问题是,当我绘制图形时,而不是滚动条在可视化上工作,图形实际上缩小以适合小部件

Here's a minimal reproducible example这是一个最小的可重现示例

import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QPushButton,
                             QLabel, QVBoxLayout, QHBoxLayout, QMessageBox, 
                             QLineEdit,QComboBox, QAction, QTabWidget, QScrollArea)

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

from matplotlib.figure import Figure
import matplotlib.pyplot as plt
                             
from PyQt5.QtCore import Qt
from PyQt5 import QtGui

class MainWindow(QMainWindow):    

    #view (GUI)
    def __init__(self, parent = None):
        #initalizer
        super(MainWindow, self).__init__(parent)
        
        #Window
        self.setWindowTitle("DataVisualizationPrototype")
        self.setGeometry(400, 200, 900, 800)
        self.activateWindow()
        self.raise_()
        
        
        self.tab_widget = TabWidget(self)
        self.setCentralWidget(self.tab_widget)
        
class TabWidget(QWidget):
    
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout =QVBoxLayout(self)
        
        self.tabs = QTabWidget()
        self.tab1 = QWidget()
        self.tab2 = QWidget()
        self.tabs.addTab(self.tab1, "Home")
        self.tabs.addTab(self.tab2, "Comparison")
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)
        
        
        #HOME PAGE
        #==========================================
        #GeneralLayout
        self.tab1layout = QVBoxLayout()
        self.tab1.setLayout(self.tab1layout)
        self.tab1layout.sizeHint()
        
        plotButton = QPushButton()
        self.tab1layout.addWidget(plotButton)
        plotbutton.clicked.connect(onclick2)

       
      
        #COMPARISON TAB
        #==========================================
        #GeneralLayout
        self.tab2layout = QVBoxLayout()
        self.tab2.setLayout(self.tab2layout)
        self.tab2layout.sizeHint()
        
        self.compGraphLayout = QHBoxLayout()
        self.compGraphLayout.addStretch()
        
        self.compScroll = QScrollArea()
        self.compScroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.compScroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.scrollWidget = QWidget()
        
        self.compScroll.setWidget(self.scrollWidget)
        self.compScroll.setWidgetResizable(True)
        
        self.tab2layout.addWidget(self.compScroll)
        self.scrollWidget.setLayout(self.compGraphLayout)
        
   def onclick2(self, event):
        self.plotLayout = QVBoxLayout()
        self.fig = plt.figure()
        self.plotLayout.addWidget(FigureCanvas(self.fig))
        
   
   
   
        self.plotLayout.addWidget(NavigationToolbar
                                 (FigureCanvas(self.fig),
                                  self.scrollWidget))
        self.compGraphLayout.addLayout(self.plotLayout)
        

#Plot MRE
        self.fig.plt.plot([1, 2, 3, 4])
        self.fig.plt.ylabel('some numbers')
        
        FigureCanvas(self.fig).adjustSize()
        
        
 
        
def main():

    pt1 = QApplication(sys.argv)
    view = MainWindow()
    view.show()
    
    sys.exit(pt1.exec())

if __name__ == '__main__':
    main()

The code provided by the OP is messy, redundant and has some typos so I will avoid pointing out the cause of the error. OP 提供的代码凌乱、冗余并且有一些拼写错误,因此我将避免指出错误的原因。

My solution propose a simple and clear way to achieve the goal of displaying the plots horizontally.我的解决方案提出了一种简单明了的方法来实现水平显示图的目标。 On the other hand, the canvas does not have a suitable sizeHint, so the widgets will be compressed making the scrollbar not visible, so a possible option is to set a minimum width.另一方面,画布没有合适的 sizeHint,因此小部件将被压缩,使滚动条不可见,因此可能的选项是设置最小宽度。

import sys

from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    QWidget,
    QPushButton,
    QVBoxLayout,
    QHBoxLayout,
    QTabWidget,
    QScrollArea,
)

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

from matplotlib.figure import Figure


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

        # Window
        self.setWindowTitle("DataVisualizationPrototype")
        self.setGeometry(400, 200, 900, 800)
        self.activateWindow()
        self.raise_()

        self.tab_widget = TabWidget()
        self.setCentralWidget(self.tab_widget)


class TabWidget(QTabWidget):
    def __init__(self, parent=None):
        super(TabWidget, self).__init__(parent)

        self.tab1 = QWidget()
        self.plot_button = QPushButton("Add plot")
        lay = QVBoxLayout(self.tab1)
        lay.addWidget(self.plot_button)

        self.tab2 = QWidget()
        self.scroll_area = QScrollArea()
        self.scroll_container = QWidget()
        self.scroll_area.setWidgetResizable(True)
        self.scroll_area.setWidget(self.scroll_container)
        self.scroll_layout = QHBoxLayout(self.scroll_container)
        lay = QVBoxLayout(self.tab2)
        lay.addWidget(self.scroll_area)

        self.addTab(self.tab1, "Home")
        self.addTab(self.tab2, "Comparison")

        self.plot_button.clicked.connect(self.plot)

    def plot(self):
        canvas = FigureCanvas(Figure())
        ax = canvas.figure.add_subplot(111)
        toolbar = NavigationToolbar(canvas, self)

        container = QWidget()
        lay = QVBoxLayout(container)
        lay.addWidget(canvas)
        lay.addWidget(toolbar)

        self.scroll_layout.addWidget(container)
        container.setMinimumWidth(400)

        ax.plot([1, 2, 3, 4])
        ax.set_ylabel("some numbers")


def main():

    app = QApplication(sys.argv)
    view = MainWindow()
    view.show()

    sys.exit(app.exec())


if __name__ == "__main__":
    main()

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

相关问题 Matplotlib:使用相同的图形绘制多个图形,没有它们重叠 - Matplotlib: plot multiple graphs using same figure, without them overlapping 如何将绘图图列表传递给javascript并动态绘制它们 - How to pass a list of plotly graphs to javascript and plot them dynamically 如何在 QScrollArea 中动态添加和删除绘图小部件? - How to dinamically add and delete plot widgets in a QScrollArea? 如何在不失去交互性的情况下将 seaborn 图实现到 tkinter 中? - How to implement seaborn graphs into tkinter without them losing their interactivity? 如何使用迭代方法绘制折线图并为每个折线图分配适当的标签 - how to plot line graphs with an iterate method and assign proper labels at each of them 如何根据方程式绘制图形 - How to plot graphs from equations 如何知道绘图的方程? - How to know the equation of the plot graphs? 如何 plot 两个条形图 - how to plot two bar graphs 如何在不影响滚动条颜色的情况下更改 QScrollArea 的背景颜色? - How to change the background color of a QScrollArea without affecting the scrollbar color? 如何在不缩小的情况下均匀分布在matplotlib中的图? - How to space plots in matplotlib equally without shrinking?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM