You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQtGraph图例项无法显示对应符号,报AttributeError错误求助

Fixing PyQtGraph Legend Symbol Issue with AttributeError on QPen

Hey there! Let's break down this legend symbol problem you're hitting. That error AttributeError:'QPen' object has no attribute 'opts' tells us exactly what's going wrong here.

Why This Happens

PyQtGraph's LegendItem relies on PyQtGraph-native plot items (like PlotDataItem) to pull styling details—things like line symbols, colors, and widths. These items store their configuration in an opts dictionary that the legend knows how to read. If you're directly passing a raw Qt QPen object to your plot lines, the legend can't parse it: QPen is a Qt native object, and it doesn't have that opts attribute the legend is looking for. That's why you see the legend description but no symbol, plus the error.

How to Fix It

There are two simple ways to resolve this, depending on how you're building your plot:

1. Use PyQtGraph's PlotDataItem with Style Dictionaries

Instead of creating a QPen directly, use PyQtGraph's built-in style syntax to define your line properties. This ensures the plot item has the opts dictionary the legend needs:

import pyqtgraph as pg
from PyQt5.QtGui import Qt

# Assume you have a PlotWidget set up already
plot_widget = pg.PlotWidget()

# Your node size value derived from IMAP mail data
node_size = 3  # Example value

# Create a PlotDataItem with your line style (width tied to node size)
mail_connection_line = pg.PlotDataItem(
    x=[1, 2, 3],  # Replace with your actual x-axis data
    y=[5, 3, 7],  # Replace with your actual y-axis data
    pen={'color': 'darkslategray', 'width': node_size},
    symbol='circle',  # Pick the symbol you want to show in the legend
    symbolSize=8,
    name="IMAP Mail Connection"
)

plot_widget.addItem(mail_connection_line)
plot_widget.addLegend()  # Now the legend will display both symbol and description

2. Manually Add Custom Legend Entries

If you need to keep using QPen for your actual plot lines, create a "dummy" PlotDataItem just for the legend. This dummy won't be drawn on the plot, but it gives the legend the opts data it needs:

import pyqtgraph as pg
from PyQt5.QtGui import QPen, Qt

plot_widget = pg.PlotWidget()
node_size = 3

# Your original line using QPen
pen = QPen(Qt.darkslategray)
pen.setWidth(node_size)
actual_line = pg.plot([1,2,3], [5,3,7], pen=pen)
plot_widget.addItem(actual_line)

# Create a dummy PlotDataItem to feed the legend
legend_dummy = pg.PlotDataItem(
    pen={'color': 'darkslategray', 'width': node_size},
    symbol='circle',
    symbolSize=8
)

# Add the legend and your custom entry
legend = plot_widget.addLegend()
legend.addItem(legend_dummy, "IMAP Mail Connection")

Key Takeaway

PyQtGraph's legend system is built to work with its own plot item types, not raw Qt drawing objects like QPen. By using PlotDataItem—either for your actual plot line or as a legend dummy—you provide the opts structure the legend needs to render symbols correctly, and that AttributeError will disappear.

内容的提问来源于stack exchange,提问作者Rishik Mani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:51:07