Gregos38
Gregos38

Reputation: 53

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

Upvotes: 4

Views: 10057

Answers (3)

Kurtresponse
Kurtresponse

Reputation: 63

For Pyqtgraph 0.11 the syntax has changed to:

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

or

plot.getAxis("bottom").setTickFont(font)

Upvotes: 6

Elliot Young
Elliot Young

Reputation: 351

There's not much documentation on this it seems like, but anybody else that was struggling with this like I did might find the source code for AxisItem useful.

Upvotes: 2

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339200

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

Upvotes: 7

Related Questions