如何在 PyQtGraph PlotWidget 周围添加填充?

问题描述 投票:0回答:1

我正在尝试在 PlotWidget 周围添加填充,因为默认设置非常严格。然而,我尝试采取的策略并没有奏效。默认填充如下所示。

PyQt 中 PlotWidget 的默认填充

这是使用以下代码生成的:

import sys
from PySide6.QtWidgets import QApplication, QMainWindow
from  PySide6.QtGui import QFont
import pyqtgraph as pg
import numpy as np

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # Create a PlotWidget
        self.graph_widget = pg.PlotWidget()
        self.graph_widget.setBackground('w')

        self.graph_widget.plotItem.setLabel('bottom', "Time(s)")
        self.graph_widget.plotItem.setLabel('left', "Speed (m/s)")

        # Setting graph styles for readability
        self.set_axis_styles('bottom')
        self.set_axis_styles('left')

        # Create a generic curve
        self.create_parabola()

        # Set the central widget of the main window
        self.setCentralWidget(self.graph_widget)

    def set_axis_styles(self, axis_name:str):
        axis_font = QFont()
        axis_font.setPointSize(12)

        axis_item = self.graph_widget.getAxis(axis_name)
        axis_item.setStyle(tickFont=axis_font, tickLength=-10)
        axis_item.label.setFont(axis_font)
        axis_item.setTextPen('k')
        axis_item.setTickPen('k')
        axis_item.setPen('k')

    def create_parabola(self):
        x = np.linspace(-10, 10, 100)
        y = x**2
        mask = y >= 0
        x = x[mask]
        y = y[mask]
        self.graph_widget.plot(x, y, pen='r')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

我尝试设置PlotWidget对象的样式表指定20px的padding,如下代码所示:

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # Create a PlotWidget
        self.graph_widget = pg.PlotWidget()
        self.graph_widget.setBackground('w')

        self.graph_widget.plotItem.setLabel('bottom', "Time(s)")
        self.graph_widget.plotItem.setLabel('left', "Speed (m/s)")
        
        # Set the style sheet for the graphing widget
        self.graph_widget.setStyleSheet("""
            QWidget {
                padding: 20px;
                background-color: white;
            }"""
        )

        # Setting graph styles for readability
        self.set_axis_styles('bottom')
        self.set_axis_styles('left')

        # Create a generic curve
        self.create_parabola()

        # Set the central widget of the main window
        self.setCentralWidget(self.graph_widget)

当我运行代码时,确实添加了填充,但图形变成了可滚动对象,其中图形的底部和右侧被切断。调整窗口大小也无法解决此问题。

添加了填充的 PlotWidget

然后我决定用样式表的边距替换填充,但这也会切断图形。此外,我希望边距的颜色与图表的颜色(即白色)相匹配,但它没有这样做。
添加边距的 PlotWidget

如何在 PyQtGraph 中的图形周围填充?

pyqt5 padding margin pyside pyqtgraph
1个回答
0
投票

我使用@musicamante的评论来解决这个问题。通过使用通用

QWidget
作为我的图表的容器,然后使用另一个小部件围绕它(为了更好地说明最终结果,我能够在不切断任何内容的情况下设置图表周围的边距。

但是,由于我想要填充而不是边距,因此我使用

SetStyleSheet
方法将图形容器的背景设置为白色,这现在给出了图形周围 20px 填充的错觉。

“填充”为 20px 的 PlotWidget

from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout
from  PySide6.QtGui import QFont, Qt
import pyqtgraph as pg
import numpy as np

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.graph_container = QWidget() # Container to hold the PlotWidget
        self.main_container = QWidget() # MainWindow container

        # Create a PlotWidget
        self.graph_widget = pg.PlotWidget()
        self.graph_widget.setBackground('w')

        self.graph_widget.plotItem.setLabel('bottom', "Time(s)")
        self.graph_widget.plotItem.setLabel('left', "Speed (m/s)")

        # Setting graph styles for readability
        self.set_axis_styles('bottom')
        self.set_axis_styles('left')

        # Create a generic curve
        self.create_parabola()

          # Style Sheet for the PlotWidget container
        self.graph_container.setStyleSheet(
        '''QWidget {
            background-color: white
        }
        ''')

        # Graph Widget Layout
        graph_layout = QVBoxLayout()
        graph_layout.addWidget(self.graph_widget)
        graph_layout.setContentsMargins(20,20,20,20)
        self.graph_container.setLayout(graph_layout)

        # Central Widget Layout
        main_layout = QVBoxLayout()
        main_layout.addWidget(self.graph_container)
        self.main_container.setLayout(main_layout)
    
        # Set the central widget of the main window
        self.setCentralWidget(self.main_container)

    def set_axis_styles(self, axis_name:str):
        axis_font = QFont()
        axis_font.setPointSize(12)

        axis_item = self.graph_widget.getAxis(axis_name)
        axis_item.setStyle(tickFont=axis_font, tickLength=-10)
        axis_item.label.setFont(axis_font)
        axis_item.setTextPen('k')
        axis_item.setTickPen('k')
        axis_item.setPen('k')

    def create_parabola(self):
        x = np.linspace(-10, 10, 100)
        y = x**2
        mask = y >= 0
        x = x[mask]
        y = y[mask]
        self.graph_widget.plot(x, y, pen='r')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.