<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Python GUIs - ipython</title><link href="https://www.pythonguis.com/" rel="alternate"/><link href="https://www.pythonguis.com/feeds/ipython.tag.atom.xml" rel="self"/><id>https://www.pythonguis.com/</id><updated>2020-05-07T09:00:00+00:00</updated><subtitle>Create GUI applications with Python and Qt</subtitle><entry><title>PyQtGraph errors in Spyder/IPython — Fixing common PyQtGraph color, font size, and compatibility issues</title><link href="https://www.pythonguis.com/faq/pyqtgraph-errors-in-spyder-ipython/" rel="alternate"/><published>2020-05-07T09:00:00+00:00</published><updated>2020-05-07T09:00:00+00:00</updated><author><name>Martin Fitzpatrick</name></author><id>tag:www.pythonguis.com,2020-05-07:/faq/pyqtgraph-errors-in-spyder-ipython/</id><summary type="html">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 &lt;code&gt;'blue'&lt;/code&gt; causes a &lt;code&gt;ValueError&lt;/code&gt;, and passing &lt;code&gt;size=30&lt;/code&gt; as an integer gives a &lt;code&gt;TypeError&lt;/code&gt;. What's going wrong and how do I fix it?</summary><content type="html">
            &lt;blockquote&gt;
&lt;p&gt;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 &lt;code&gt;'blue'&lt;/code&gt; causes a &lt;code&gt;ValueError&lt;/code&gt;, and passing &lt;code&gt;size=30&lt;/code&gt; as an integer gives a &lt;code&gt;TypeError&lt;/code&gt;. What's going wrong and how do I fix it?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h2 id="the-color-name-error"&gt;The color name error&lt;/h2&gt;
&lt;p&gt;You might see this error when trying to set a title color using a full color name like &lt;code&gt;'blue'&lt;/code&gt;:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;self.graphWidget.setTitle("Your Title Here", color='blue', size='30pt')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;File "...\pyqtgraph\functions.py", line 182, in mkColor
    g = int(c[1]*2, 16)
ValueError: invalid literal for int() with base 16: 'll'
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Or, when using &lt;code&gt;'black'&lt;/code&gt;:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;UnboundLocalError: local variable 'r' referenced before assignment
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These errors occur because older versions of PyQtGraph's &lt;code&gt;mkColor&lt;/code&gt; function don't understand full color names like &lt;code&gt;'blue'&lt;/code&gt; or &lt;code&gt;'black'&lt;/code&gt;. When you pass &lt;code&gt;'blue'&lt;/code&gt;, PyQtGraph tries to interpret it as a hex color string, picks out individual characters (&lt;code&gt;'l'&lt;/code&gt;, &lt;code&gt;'u'&lt;/code&gt;, etc.), and fails to parse them as hexadecimal values.&lt;/p&gt;
&lt;h3&gt;The fix: use single-letter color codes or update PyQtGraph&lt;/h3&gt;
&lt;p&gt;The simplest immediate fix is to use PyQtGraph's single-letter color codes instead of full color names:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Letter&lt;/th&gt;
&lt;th&gt;Color&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'r'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Red&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'g'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Green&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'b'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blue&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'c'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Cyan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'m'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Magenta&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'y'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yellow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'k'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Black&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'w'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;White&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;So instead of &lt;code&gt;color='blue'&lt;/code&gt;, use &lt;code&gt;color='b'&lt;/code&gt;:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;self.graphWidget.setTitle("Your Title Here", color="b", size="30pt")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also pass colors as hex strings or RGB tuples:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;# 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")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The better long-term fix is to &lt;strong&gt;update PyQtGraph&lt;/strong&gt; to a newer version, where full color name support has been improved:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-sh"&gt;sh&lt;/span&gt;
&lt;pre&gt;&lt;code class="sh"&gt;pip install pyqtgraph --upgrade
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id="the-font-size-typeerror"&gt;The font size TypeError&lt;/h2&gt;
&lt;p&gt;Another common error appears when passing the &lt;code&gt;size&lt;/code&gt; parameter as an integer:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;self.graphWidget.setTitle("Your Title Here", color="b", size=30)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;File "...\pyqtgraph\graphicsItems\LabelItem.py", line 61, in setText
    optlist.append('font-size: ' + opts['size'])
TypeError: can only concatenate str (not "int") to str
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This happens because PyQtGraph's &lt;code&gt;LabelItem.setText&lt;/code&gt; method builds a CSS style string internally. It expects the &lt;code&gt;size&lt;/code&gt; value to already be a string with a unit suffix like &lt;code&gt;'30pt'&lt;/code&gt;. When you pass the integer &lt;code&gt;30&lt;/code&gt;, Python can't concatenate a string and an integer.&lt;/p&gt;
&lt;h3&gt;The fix: pass size as a string with units&lt;/h3&gt;
&lt;p&gt;Always pass the &lt;code&gt;size&lt;/code&gt; parameter as a string that includes the CSS unit:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;self.graphWidget.setTitle("Your Title Here", color="b", size="30pt")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;pt&lt;/code&gt; suffix stands for "points," which is a standard CSS font-size unit. You could also use &lt;code&gt;px&lt;/code&gt; (pixels) if you prefer:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;self.graphWidget.setTitle("Your Title Here", color="b", size="30px")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id="updating-pyqtgraph"&gt;Updating PyQtGraph&lt;/h2&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Check your current version:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-sh"&gt;sh&lt;/span&gt;
&lt;pre&gt;&lt;code class="sh"&gt;pip show pyqtgraph
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Upgrade to the latest version:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-sh"&gt;sh&lt;/span&gt;
&lt;pre&gt;&lt;code class="sh"&gt;pip install pyqtgraph --upgrade
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If you're using Anaconda (common with Spyder), you can also update through conda:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-sh"&gt;sh&lt;/span&gt;
&lt;pre&gt;&lt;code class="sh"&gt;conda install -c conda-forge pyqtgraph
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id="the-timeclock-error"&gt;The &lt;code&gt;time.clock&lt;/code&gt; error&lt;/h2&gt;
&lt;p&gt;If you're on Python 3.8 or later with an older PyQtGraph version, you may also see an error related to &lt;code&gt;time.clock&lt;/code&gt;. This function was removed in Python 3.8. Upgrading PyQtGraph to version 0.11.0 or later fixes this as well.&lt;/p&gt;
&lt;h2 id="a-complete-working-example"&gt;A complete working example&lt;/h2&gt;
&lt;p&gt;Here's a complete example that uses the correct color and size formats, and should work across PyQtGraph versions:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;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 (&amp;deg;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_())
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This example avoids all the common pitfalls:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Colors use single-letter codes (&lt;code&gt;"b"&lt;/code&gt;, &lt;code&gt;"r"&lt;/code&gt;) instead of full names&lt;/li&gt;
&lt;li&gt;The title &lt;code&gt;size&lt;/code&gt; is a string with a unit (&lt;code&gt;"20pt"&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Axis label styles use string values with units (&lt;code&gt;"18px"&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you run this and see your plot with a blue title, red axis labels, and a line chart, everything is working correctly.&lt;/p&gt;
            &lt;p&gt;For an in-depth guide to building Python GUIs with PyQt5 see my book, &lt;a href="https://www.martinfitzpatrick.com/pyqt5-book/"&gt;Create GUI Applications with Python &amp; Qt5.&lt;/a&gt;&lt;/p&gt;
            </content><category term="pyqt5"/><category term="pyqt"/><category term="pyqtgraph"/><category term="spyder"/><category term="ipython"/><category term="python"/><category term="qt"/><category term="qt5"/><category term="data-science"/><category term="pyqt5-data-science"/></entry></feed>