![](/img/trans.png)
[英]Create a single legend for multiple plot in 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.