Warm tip: This article is reproduced from stackoverflow.com, please click
font-size plot pyqtgraph python

How to change ticks fontsize of a plot using PyQtgraph

发布于 2021-01-16 22:11:49

everything is in the title, how to change the fontsize of the ticks using pyqtgraph ?

Thx

Questioner
Gregos38
Viewed
0
ImportanceOfBeingErnest 2017-01-21 04:37

I think the only way to change the font size of the ticklabels in pyqtgraph is to first create a new font within PyQt and set the fontsize to it. Then this font can be applied to the ticks.

font=QtGui.QFont()
font.setPixelSize(20)
plot.getAxis("bottom").tickFont = font

Initially I would have thought that something like
plot.getAxis("bottom").setStyle(tickFont = font)
should work as well, but for some reason it doesn't.

Once the font size has increased, it may make sense to adapt the tickOffset as well. Find a complete running code is below.

import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg


app = QtGui.QApplication([])

x = np.linspace(-50, 50, 1000)
y = np.sin(x) / x

win = pg.GraphicsWindow()
plot = win.addPlot(x=x, y=y, title="Plot")
plot.setLabel('bottom', "some x axis label")

font=QtGui.QFont()
font.setPixelSize(20)
plot.getAxis("bottom").tickFont = font
plot.getAxis("bottom").setStyle(tickTextOffset = 20)


if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

enter image description here