Changing ticks in pyqtgraph objects to strings

Is there a way to change the tics in a line graph to dates or names of months?

Hey, here you go.

from PyQt5 import QtWidgets
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import sys  # We need sys so that we can pass argv to QApplication
import os
import datetime

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.graphWidget = pg.PlotWidget()

        months = [1,2,3,4,5,6,7,8,9,10]
        temperature = [30,32,34,32,33,31,29,32,35,45]


        pen = pg.mkPen(color=(255, 0, 0))
        month_labels = [
            # Generate a list of tuples (x_value, x_label)
            (m,, m, 1).strftime('%B'))
            for m in months
        self.graphWidget.plot(months, temperature, pen=pen, labels=month_labels)
        # Pass the list in, *in* a list.

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()

if __name__ == '__main__':

The trick is the labels need to be a list of 2-tuples containing the tick value (x value) and the label for that tick. In the example above we’re using month numbers 1-10 for the x-axis, so the month labels becomes [(1, "January"), (2, "February"), (3, "March") ... etc]

To set the labels you need to get the axis object, and then use .setTicks() …since this can be used to set ticks for both axes, you need to pass it in in a list i.e. [month_labels] where month_labels is our list of tick labels.

The result is …

Hope that helps?