Skip to content

Instantly share code, notes, and snippets.

@flutefreak7
Created February 25, 2019 07:16
Show Gist options
  • Save flutefreak7/3ffbaa0e0ad72694cd03085d0266d8fd to your computer and use it in GitHub Desktop.
Save flutefreak7/3ffbaa0e0ad72694cd03085d0266d8fd to your computer and use it in GitHub Desktop.
This adds a PlotWidget element to the QDarkStyle stylesheet so that axes labels in PyQtGraph plots aren't covered by unnecessary padding.
from PyQt5 import QtCore, QtWidgets
import qdarkstyle
import pyqtgraph as pg
qss_plot_widget = r'''
PlotWidget {
padding: 0px;
}
'''
app = QtWidgets.QApplication([])
class Plotter(QtWidgets.QWidget):
def __init__(self, dark=False, fix_padding=False):
super().__init__()
self._layout = QtWidgets.QVBoxLayout()
self._layout.setSpacing(0)
self._layout.setContentsMargins(0, 0, 0, 0)
self.setLayout(self._layout)
self._plotter = pg.PlotWidget()
self._plotter.plot(x=[1, 2, 3, 4], y=[1, 3, 2, 4])
self._plotter.setLabels(title='Window 1', bottom='X Axis', left='Y Axis', right='Y2 Axis')
self._plotter.setContentsMargins(0, 0, 0, 0)
self._layout.addWidget(self._plotter)
if dark:
qss = qdarkstyle.load_stylesheet_pyqt5()
if fix_padding:
qss += qss_plot_widget
self.setWindowTitle('QDarkStyle + PyQtGraph Fix')
else:
self.setWindowTitle('QDarkStyle')
self.setStyleSheet(qss)
else:
self.setWindowTitle('No Style Sheet')
plot_light = Plotter(dark=False)
plot_dark = Plotter(dark=True)
plot_dark_fixed = Plotter(dark=True, fix_padding=True)
plot_light.show()
plot_dark.show()
plot_dark_fixed.show()
plot_light.move(QtCore.QPoint(10, 100))
plot_dark.move(QtCore.QPoint(400, 100))
plot_dark_fixed.move(QtCore.QPoint(800, 100))
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment