Matplotlib legend does not match seaborn bubble plot sizes
我使用seaborn创建了一个泡泡图,并使用matplotlib在我的seaborn图的右侧绘制了图例。我使用
1 2 3 4 5 6 7 8 9 10 11 12 | 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摄取。我确实了解到我的数据(代码1,代码2和代码3)具有不同的最小值和最大值,这些值应在seaborn的size参数下指定。对于代码1,size =(1,900);对于code2,size =(1,300);对于代码3,大小=(1,45)。因为我使用matplotlib在代码3的图的右边绘制图例,所以缩放比例特定于最右边的图,而不是所有3个图。最后,我最终使用了matplotlib的legend_elements,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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) |