简体   繁体   English

Python,Seaborn FacetGrid更改标题

[英]Python, Seaborn FacetGrid change titles

I am trying to create a FacetGrid in Seaborn 我正在尝试在Seaborn中创建FacetGrid

My code is currently: 我的代码当前为:

g = sns.FacetGrid(df_reduced, col="ActualExternal", margin_titles=True)
bins = np.linspace(0, 100, 20)
g.map(plt.hist, "ActualDepth", color="steelblue", bins=bins, width=4.5)

This gives my the Figure 这给了我图

我的FacetGrid

Now, instead of "ActualExternal =0.0" and "ActualExternal =1.0" I would like the titles "Internal" and "External" 现在,我希望使用标题“ Internal”和“ External”代替“ ActualExternal = 0.0”和“ ActualExternal = 1.0”

And, instead of "ActualDepth" I would like the xlabel to say "Percentage Depth" 而且,我希望xlabel代替“ ActualDepth”说“ Percentage Depth”

Finally, I would like to add a ylabel of "Number of Defects". 最后,我想添加一个“缺陷数”的ylabel。

I've tried Googling and have tried a few things but so far no success. 我尝试了Googling,并尝试了一些方法,但到目前为止没有成功。 Please can you help me? 请你能帮我吗?

Thanks 谢谢

You can access the axes of a FacetGrid ( g = sns.FacetGrid(...) ) via g.axes . 您可以通过g.axes访问FacetGrid( g = sns.FacetGrid(...) )的g.axes With that you are free to use any matplotlib method you like to tweak the plot. 这样,您就可以随意使用任何您想要调整曲线的matplotlib方法。

Change titles : 更改标题

axes = g.axes.flatten()
axes[0].set_title("Internal")
axes[1].set_title("External")

Change labels : 更改标签

axes = g.axes.flatten()
axes[0].set_ylabel("Number of Defects")
for ax in axes:
    ax.set_xlabel("Percentage Depth")

Note that I prefer those above the FacetGrid 's internal g.set_axis_labels and set_titles methods, because it makes it more obvious which axes is to be labelled. 请注意,我更喜欢FacetGrid内部g.set_axis_labelsset_titles方法中的set_titles方法,因为它使要标记哪些轴更加明显。

Although you can iterate through the axes and set the titles individually using matplotlib commands, it is cleaner to use seaborn's built-in tools to control the title. 尽管您可以遍历轴并使用matplotlib命令分别设置标题,但使用seaborn的内置工具控制标题更干净。 For example: 例如:

# Add a column of appropriate labels
df_reduced['measure'] = df_reduced['ActualExternal'].replace({0: 'Internal',
                                                              1: 'External'}

g = sns.FacetGrid(df_reduced, col="measure", margin_titles=True)
g.map(plt.hist, "ActualDepth", color="steelblue", bins=bins, width=4.5)

# Adjust title and axis labels directly
g.set_titles("{col_name}")  # use this argument literally
g.set_axis_labels(x_var="Percentage Depth", y_var="Number of Defects")

This has the benefit of not needing modification regardless of whether you have 1D or 2D facets. 这样做的好处是,无论您具有1D还是2D构面,都无需进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM