How do I add padding around a PyQtGraph PlotWidget?

I am trying to add padding around a PlotWidget as the default settings are extremely tight. However, the strategies I am trying to take are not working. The default padding is seen below.

default padding for a PlotWidget in PyQt

This is generated using the following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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_())
</code>
<code>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_()) </code>
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_())

I tried to set the style sheet of the PlotWidget object to specify a padding of 20px, as shown in the code below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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)
</code>
<code>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) </code>
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)

When I run the code, the padding is indeed added, but the graph becomes a scrollable object where the bottom and right of the graph are cut off. Resizing the window does not fix this issue either.

PlotWidget with padding added

I then decided to replace padding with margins to the style sheet instead, however that also cuts off the graph. Furthermore, I want the color of the margins to match the color of the graph (i.e. white), which it is not doing.
PlotWidget with margins added

How can I get padding around my graph in PyQtGraph?

New contributor

user22960591 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật