October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Replace Numeric Axis Ticks with Strings in PyQtGraph

Use setTicks() for fixed category labels, tickStrings() for calculated text, and DateAxisItem for Unix timestamp coordinates in PyQtGraph.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use AxisItem.setTicks() for a fixed set of labels, override tickStrings() when labels should be calculated from tick values, and use DateAxisItem when your x coordinates are Unix timestamps. In all cases, keep the plotted coordinates numeric and make sure the tick positions match them.

Choose the right method for your x values

What your x values represent Use How it works
Known numeric positions for categories, such as 0, 1, 2 AxisItem.setTicks() Provide explicit pairs of numeric positions and display labels. This sets which ticks appear and overrides the normal automatic tick calculation and formatting. PyQtGraph AxisItem API.
Numeric values whose labels need to be computed Override AxisItem.tickStrings() Return one display string for each tick value PyQtGraph supplies. PyQtGraph AxisItem API.
Actual dates stored as Unix timestamps DateAxisItem Formats timestamp coordinates as dates and adapts the displayed format to the time density visible on the axis. PyQtGraph DateAxisItem API.

Set fixed labels at known numeric positions

For a short, fixed sequence such as month names on ordinal x positions, get the bottom axis and pass tick levels to setTicks(). Each tick is a (numeric_position, label) pair:

As an Amazon Associate I earn from qualifying purchases.

import pyqtgraph as pg

plot = pg.PlotWidget()
axis = plot.getAxis('bottom')
axis.setTicks([[(0, 'Jan'), (1, 'Feb'), (2, 'Mar')]])

The numbers in those pairs must be the x coordinates used by the plotted data. For example, if the data points are at 0, 1, and 2, the labels above align with them. A label pair changes the tick location and its displayed text; it does not convert string categories into plotting coordinates. Keep the data numeric and map each label to the corresponding numeric position.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

setTicks() takes nested tick levels: the inner list contains the position-label pairs for a level. The documented format supports major and minor levels, with a third optional level. Because explicit ticks take precedence over the usual tickSpacing(), tickValues(), and tickStrings() behavior, use this method when you intend to control the ticks directly—not when you want PyQtGraph to keep selecting them automatically.

Format real date and time coordinates

If x data contains Unix timestamps, use DateAxisItem rather than maintaining a fixed label list. It chooses date formatting according to the visible time density, which is useful as the plotted range changes.

import pyqtgraph as pg

plot = pg.PlotWidget()
axis = pg.DateAxisItem(orientation='bottom')
plot.setAxisItems({'bottom': axis})

Plot timestamp-valued x coordinates with this axis. The axis constructor also accepts an optional utcOffset; decide the timezone semantics for both the data and the display instead of assuming the offset is handled in a particular way. The concise API reference documents the parameter but does not detail its conversion rules. The API reference currently identifies itself as PyQtGraph 0.15.0.dev0, so confirm behavior against the version installed in your application.

Do not pass ordinal month positions such as 0, 1, and 2 to DateAxisItem as if they were timestamps. Use explicit labels or custom formatting for ordinal values.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Generate labels from tick values

For labels that should be calculated rather than listed as fixed pairs, subclass AxisItem and override tickStrings(values, scale, spacing). Return a string for every supplied value:

import pyqtgraph as pg

class MonthAxis(pg.AxisItem):
    def tickStrings(self, values, scale, spacing):
        months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                  'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        return [months[int(value) % 12] for value in values]

This example assumes integer-like values used as month indices. It does not represent years or distinguish, for example, January in different years. For timestamps or ranges that cross year boundaries, format the actual date values with DateAxisItem or write a formatter that interprets the data representation correctly.

In logarithmic mode, PyQtGraph routes tick-string generation through logTickStrings(), which is also an override point. See the AxisItem API reference for the tick-formatting methods.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Prevent labels from being clipped or overlapping

Custom labels may need more room than numeric ticks, especially on a narrow plot. AxisItem.setStyle() documents controls including autoExpandTextSpace, autoReduceTextSpace, hideOverlappingLabels, and tick-text width and height limits. Horizontal axes hide labels that extend beyond the axis geometry by default; vertical axes permit extension by default. Check the result at the target window size and font, and adjust axis style if labels overlap or are cut off. See the AxisItem style and tick API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.