Search code examples
pythonpyqtpyqt5pyside2pyqtgraph

How to change the font of Axis label in Pyqtgraph


I have a custom font, I am able to set this font in title of the graph, I need help in setting the axis label font.(left, bottom axis labels)

I am able to set the font to the title of the graph like this

    graphWidget = pyqtgraph.PlotWidget()
    graph = graphWidget.getPlotItem()
    graph.titleLabel.item.setFont(font)

I would like to know if there's any similar way to set the font for axis labels.


Solution

  • To set custom QFont to axis label, you have to setFont for label of each axis.

    Here is a short example, which changes font family to Times for title, bottom and left axis.

    import sys
    
    import pyqtgraph
    from PyQt5.QtGui import QFont
    from PyQt5.QtWidgets import QApplication
    
    app = QApplication(sys.argv)
    
    # Define your font
    my_font = QFont("Times", 10, QFont.Bold)
    
    graphWidget = pyqtgraph.PlotWidget()
    graphWidget.setTitle("My plot")
    
    # Set label for both axes
    graphWidget.setLabel('bottom', "My x axis label")
    graphWidget.setLabel('left', "My y axis label")
    
    # Set your custom font for both axes
    graphWidget.getAxis("bottom").label.setFont(my_font)
    graphWidget.getAxis("left").label.setFont(my_font)
    
    graph = graphWidget.getPlotItem()
    # Set font for plot title
    graph.titleLabel.item.setFont(my_font)
    
    graphWidget.show()
    app.exec()