I'm following a PyQtGraph tutorial and getting errors when I try to set colors and font sizes on my plot titles and labels. Using color names like
'blue'causes aValueError, and passingsize=30as an integer gives aTypeError. What's going wrong and how do I fix it?
If you're working through PyQtGraph tutorials and running into errors when setting colors, titles, or font sizes, you're not alone. These issues come up frequently, especially when using older versions of PyQtGraph in environments like Spyder or IPython. The good news is that most of these problems have straightforward fixes.
In this article, we'll walk through the most common PyQtGraph errors related to colors and label formatting, explain why they happen, and show you how to solve them.
The color name error
You might see this error when trying to set a title color using a full color name like 'blue':
self.graphWidget.setTitle("Your Title Here", color='blue', size='30pt')
File "...\pyqtgraph\functions.py", line 182, in mkColor
g = int(c[1]*2, 16)
ValueError: invalid literal for int() with base 16: 'll'
Or, when using 'black':
UnboundLocalError: local variable 'r' referenced before assignment
These errors occur because older versions of PyQtGraph's mkColor function don't understand full color names like 'blue' or 'black'. When you pass 'blue', PyQtGraph tries to interpret it as a hex color string, picks out individual characters ('l', 'u', etc.), and fails to parse them as hexadecimal values.
The fix: use single-letter color codes or update PyQtGraph
The simplest immediate fix is to use PyQtGraph's single-letter color codes instead of full color names:
| Letter | Color |
|---|---|
'r' |
Red |
'g' |
Green |
'b' |
Blue |
'c' |
Cyan |
'm' |
Magenta |
'y' |
Yellow |
'k' |
Black |
'w' |
White |
So instead of color='blue', use color='b':
self.graphWidget.setTitle("Your Title Here", color="b", size="30pt")
You can also pass colors as hex strings or RGB tuples:
# Hex color
self.graphWidget.setTitle("Your Title Here", color="#0000FF", size="30pt")
# RGB tuple (values 0-255)
self.graphWidget.setTitle("Your Title Here", color=(0, 0, 255), size="30pt")
The better long-term fix is to update PyQtGraph to a newer version, where full color name support has been improved:
pip install pyqtgraph --upgrade
The font size TypeError
Another common error appears when passing the size parameter as an integer:
self.graphWidget.setTitle("Your Title Here", color="b", size=30)
File "...\pyqtgraph\graphicsItems\LabelItem.py", line 61, in setText
optlist.append('font-size: ' + opts['size'])
TypeError: can only concatenate str (not "int") to str
This happens because PyQtGraph's LabelItem.setText method builds a CSS style string internally. It expects the size value to already be a string with a unit suffix like '30pt'. When you pass the integer 30, Python can't concatenate a string and an integer.
The fix: pass size as a string with units
Always pass the size parameter as a string that includes the CSS unit:
self.graphWidget.setTitle("Your Title Here", color="b", size="30pt")
The pt suffix stands for "points," which is a standard CSS font-size unit. You could also use px (pixels) if you prefer:
self.graphWidget.setTitle("Your Title Here", color="b", size="30px")
Updating PyQtGraph
Many of these issues were fixed in later releases of PyQtGraph. If you're using an older version (especially anything before 0.11), upgrading will resolve most of these problems and give you access to better features and performance.
Check your current version:
pip show pyqtgraph
Upgrade to the latest version:
pip install pyqtgraph --upgrade
If you're using Anaconda (common with Spyder), you can also update through conda:
conda install -c conda-forge pyqtgraph
The time.clock error
If you're on Python 3.8 or later with an older PyQtGraph version, you may also see an error related to time.clock. This function was removed in Python 3.8. Upgrading PyQtGraph to version 0.11.0 or later fixes this as well.
A complete working example
Here's a complete example that uses the correct color and size formats, and should work across PyQtGraph versions:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import pyqtgraph as pg
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.graphWidget = pg.PlotWidget()
self.setCentralWidget(self.graphWidget)
# Set background color
self.graphWidget.setBackground("w")
# Set title with single-letter color code and string size
self.graphWidget.setTitle(
"Temperature Over Time", color="b", size="20pt"
)
# Set axis labels
styles = {"color": "r", "font-size": "18px"}
self.graphWidget.setLabel("left", "Temperature (°C)", **styles)
self.graphWidget.setLabel("bottom", "Time (min)", **styles)
# Add grid
self.graphWidget.showGrid(x=True, y=True)
# Sample data
hour = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
temperature = [30, 32, 34, 32, 33, 31, 29, 32, 35, 30]
# Plot with a blue pen
pen = pg.mkPen(color="b", width=2)
self.graphWidget.plot(hour, temperature, pen=pen)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
This example avoids all the common pitfalls:
- Colors use single-letter codes (
"b","r") instead of full names - The title
sizeis a string with a unit ("20pt") - Axis label styles use string values with units (
"18px")
If you run this and see your plot with a blue title, red axis labels, and a line chart, everything is working correctly.
PyQt/PySide Development Services — Stuck in development hell? I'll help you get your project focused, finished and released. Benefit from years of practical experience releasing software with Python.