Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change ticks fontsize of a plot using PyQtgraph

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

Thx

like image 889
Gregos38 Avatar asked Oct 28 '25 17:10

Gregos38


1 Answers

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

like image 77
ImportanceOfBeingErnest Avatar answered Oct 31 '25 08:10

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!