绘制多面网格的图例

时间:2018-07-06 08:43:26

标签: python seaborn

我在Seaborn中使用面网格绘制了约10张图。如何在每个图中绘制图例?这是我当前的代码:

plt.legend()

如果我包含#using <System.dll> using namespace System; using namespace System::IO::Ports; int main() { for each (String^ s in SerialPort::GetPortNames()) { } } ,则图例仅出现在最后一个图形中。如何在构面网格图中的每个图形中绘制图例?还是有办法在第一个图形而不是最后一个图形中绘制图例?预先感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

如果在每个轴上进行迭代,则可以分别向每个轴添加图例。使用来自seaborn文档的示例:

import seaborn as sns

tips = sns.load_dataset("tips")

g = sns.FacetGrid(tips, col="time",  hue="smoker")
g.map(plt.scatter, "total_bill", "tip", edgecolor="w")

for ax in g.axes.ravel():
    ax.legend()

我们必须使用.ravel()的原因是因为轴存储在numpy数组中。这会让您:

Facetgrid with individual legends

因此,您需要这样做

g = sns.FacetGrid(masterdata,row="departmentid",col = "coursename",hue="resulttype",size=5, aspect=1)
g.map(plt.scatter, "totalscore", "semesterPercentage")

for ax in g.axes.ravel():
    ax.legend()

要仅在左上图中显示图例,您要访问axes数组中的第一个numpy,该数组将位于索引[0, 0]处。您可以通过例如

g = sns.FacetGrid(tips, col="time",  hue="smoker")
g.map(plt.scatter, "total_bill", "tip", edgecolor="w")

g.axes[0, 0].legend()

将为您提供: Facetgrid with legend in first axes