繁体   English   中英

Matplotlib 图例与 seaborn 气泡图大小不匹配

[英]Matplotlib legend does not match seaborn bubble plot sizes

我使用seaborn创建了一个气泡图,并使用matplotlib在我的seaborn图右侧绘制了图例。 我在我的 seaborn 代码中使用 size sizes=(1,900)指定了气泡的大小,但是我的 matplotlib 图例上的缩放并没有反映绘图显示的内容。 图例从 0 到 45,但我图中的实际数据范围从 0 到 900

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(11,4))
sns.scatterplot(y="Min", x="Max",
                size="Count", sizes=(1,900), alpha=0.5,
                color='r', data=code1, ax=ax1, legend=False)
sns.scatterplot(y="Min", x="Max", alpha=0.5,
                color='b', size="Count", sizes=(1,900),
                data=code2, ax=ax2, legend=False)
sns.scatterplot(y="Min", x="Max", alpha=0.5,
                color='g', size="Count", sizes=(1,900),
                data=code3, ax=ax3)
ax3.legend(loc='upper right', bbox_to_anchor=(1.7,1), labelspacing=2,
           fontsize=14, frameon=False, markerscale=1)

这是我的情节

这是我的情节的截图

我无法弄清楚 seaborn 如何构建图例输出以供 matplotlib 摄取。 我确实了解到我的数据(code1、code2 和 code3)具有不同的最小值和最大值,这些值应该在 seaborn 的大小参数下指定。 对于代码1,大小=(1,900); 对于代码2,大小=(1,300); 对于代码3,大小=(1,45)。 因为我使用 matplotlib 在 code3 的图右侧绘制图例,所以缩放特定于最右边的图,而不是所有 3 个图。 最后,我最终使用了 matplotlib 的 legend_elements,如下所示:

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12,4))
scatter = ax1.scatter(y=code1["Min"], x=code1["Max"],
                s=code1["Count"], 
                color='r', alpha=0.5)
ax2.scatter(y=code2["Min"], x=code2["Max"], 
                     color='b', s=code2["Count"], alpha=0.5)
ax3.scatter(y=code3["Min"], x=code3["Max"], 
                     color='g', s=code3["Count"], alpha=0.5)

kw = dict(prop="sizes", num=[10,100,500,900])
legend = ax3.legend(*scatter.legend_elements(**kw), title="Count", fontsize=12,
                    loc='upper right', bbox_to_anchor=(1.5,1), labelspacing=2,
                   frameon=False)

使用 matplotlib 而不是 seaborn 更新绘图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM