猿问

从seaborn情节中删除图例标题

我只是想删除用 seaborn 制作的散点图的标题。标题由色调参数给出。在这种情况下,标题是“冥王星”

x = sns.scatterplot(x="Al total", y="Fe/Fe+Mg", data=df, hue="Pluton", alpha=1)

sns.set_style("ticks")


plt.legend(ncol=3, loc='upper center', 

           bbox_to_anchor=[0.5, 1.25], 

           columnspacing=1.3, labelspacing=0.0,

           handletextpad=0.0, handlelength=1.5,

           fancybox=True, shadow=True)



plt.ylim(0.2 ,1.1)


慕雪6442864
浏览 81回答 2
2回答

慕丝7291255

当您scatterplot()使用hue=、 或style=等创建 a 时,seaborn 会自动在图例列表中添加一个条目以充当“节标题”。由于您正在重新创建图例以将其放入所需的格式,因此要求 matplotlib 排除图例列表中的第一个条目以摆脱该“标题”是非常简单的tips = sns.load_dataset('tips')ax = sns.scatterplot(x="total_bill", y="tip", hue="day",                     data=tips)h,l = ax.get_legend_handles_labels()plt.legend(h[1:],l[1:],ncol=3, loc='upper center',            bbox_to_anchor=[0.5, 1.25],            columnspacing=1.3, labelspacing=0.0,           handletextpad=0.0, handlelength=1.5,           fancybox=True, shadow=True)

哆啦的时光机

您可以找到句柄和标签 - 并从它们中删除图例标题。它们将是列表,其中包含您的图例作为第一项。例如,labels您的示例如下所示:labels = ['Pluton', 'Desemborque', 'Desemb. (hidrot. I)' ... and all others]handles将包含类似的项目,但它们表示为matplotlib object:handles = [<matplotlib.lines.Line2D object at 0x7f114408bf98>, ... and many others]代码:import matplotlib.pyplot as pltimport seaborn as sns# Set style for seabornsns.set_style("ticks")x = sns.scatterplot(x="Al total", y="Fe/Fe+Mg", data=df, hue="Pluton", alpha=1)# Found handles and labels for legendax = x.axes[0][0]handles, labels = ax.get_legend_handles_labels()# When set legend in matplotlib use our modified handles and labelsplt.legend(ncol=3, loc='upper center',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bbox_to_anchor=[0.5, 1.25],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;columnspacing=1.3, labelspacing=0.0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;handletextpad=0.0, handlelength=1.5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fancybox=True, shadow=True,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;handles=handles[1:], labels=labels[1:],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )# Plotplt.ylim(0.2, 1.1)plt.show()可以建议也阅读此以了解其他可能的方法
随时随地看视频慕课网APP

相关分类

Python
我要回答