问题描述
我正在尝试在 Seaborn 中创建一个 FacetGrid
I am trying to create a FacetGrid in Seaborn
我的代码目前是:
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)
这给了我的图
现在,我想要标题内部"和外部",而不是ActualExternal =0.0"和ActualExternal =1.0"
Now, instead of "ActualExternal =0.0" and "ActualExternal =1.0" I would like the titles "Internal" and "External"
而且,我希望xlabel代替"ActualDepth"说"Percentage Depth"
And, instead of "ActualDepth" I would like the xlabel to say "Percentage Depth"
最后,我想添加一个缺陷数"的 ylabel.
Finally, I would like to add a ylabel of "Number of Defects".
我尝试了谷歌搜索,并尝试了一些方法,但到目前为止没有成功.请问你能帮我吗?
I've tried Googling and have tried a few things but so far no success. Please can you help me?
谢谢
推荐答案
您可以通过 g 访问 FacetGrid (
.有了它,您可以自由使用任何您喜欢的 matplotlib 方法来调整绘图.g = sns.FacetGrid(...)
) 的轴.轴
You can access the axes of a FacetGrid (g = sns.FacetGrid(...)
) via g.axes
. With that you are free to use any matplotlib method you like to tweak the plot.
更改标题:
axes = g.axes.flatten()
axes[0].set_title("Internal")
axes[1].set_title("External")
更改标签:
axes = g.axes.flatten()
axes[0].set_ylabel("Number of Defects")
for ax in axes:
ax.set_xlabel("Percentage Depth")
请注意,我更喜欢 FacetGrid
的内部 g.set_axis_labels
和 set_titles
方法之上的那些,因为它使哪些轴更加明显是要贴标签的.
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.
这篇关于Python、Seaborn FacetGrid 更改标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!