首页 > 解决方案 > 结合图例图标 matplotlib

问题描述

我正在使用matplotlib在图例中显示一个带有单个图标的图和置信区间。过去我常写:

fig, ax1 = plt.subplots(1, 1)
curve = ax1.plot(...)
curve_fil = ax.fill_between(...)
fig.legend([(curve[0], curve_fill[0])], ["label here"], ...)

这会起作用,并允许我制作如下图: 在此处输入图像描述

但是现在我收到以下错误:

TypeError: 'PolyCollection' object is not subscriptable

如果我删除[0]并只提供情节作为参数,我会得到

AttributeError: 'NoneType' object has no attribute 'create_artists'

如果我尝试创建自定义补丁,例如:

patch = mpatches.Patch(color='r')
fig.legend([(curve[0], patch)], ["label here"])

然后我得到:

TypeError: 'Polygon' object is not subscriptable

这是什么意思呢?我开始在这个问题上扯掉我的头发......我知道已经发布了类似的问题,但是由于所有被标记为有效答案的建议对我都不起作用,所以我决定发布这个问题。

标签: pythonmatplotlib

解决方案


推荐阅读