Jyo*_*hsa 7 python matplotlib legend seaborn
使用“tips”数据集作为玩具模型,我生成了以下图:
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
cmap = sns.cubehelix_palette(dark=.3, light=.8, as_cmap=True)
g = sns.scatterplot(x="total_bill", y="sex", hue="smoker", size = 'tip',sizes=(320, 600), data=tips)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., fontsize=13)
plt.show(g)
Run Code Online (Sandbox Code Playgroud)
这张图片正是我所需要的。但是,我想从图例中删除size = 'tip'并只保留吸烟者。本质上,删除那些标记为 0.0 到 12.0 的黑色圆圈。如何确保我的图例只有我选择的一个变量?
我能够通过对图例中的标签进行索引来找到修复方法。
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
cmap = sns.cubehelix_palette(dark=.3, light=.8, as_cmap=True)
ax = sns.scatterplot(x="total_bill", y="sex", hue="smoker", size='tip', sizes=(320, 600), data=tips)
# extract the existing handles and labels
h, l = ax.get_legend_handles_labels()
# slice the appropriate section of l and h to include in the legend
ax.legend(h[0:3], l[0:3], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., fontsize=13)
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3550 次 |
| 最近记录: |