社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何在python中使用seaborn relplot创建的子块中关闭y轴标题、图例?[复制品]

Mithun C D • 7 年前 • 2293 次点击  

我想在Seaborn创建一个FacetGrid

我的代码当前为:

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)

这给了我数字

My FacetGrid

现在,我要的不是“actualexternal=0.0”和“actualexternal=1.0”,而是“internal”和“external”

而且,我希望xlabel不是“actualDepth”,而是“percentage depth”

最后,我想加上一个ylabel“缺陷数”。

我试过谷歌搜索,也试过一些东西,但至今没有成功。你能帮我吗?

谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/42933
文章 [ 2 ]  |  最新文章 7 年前
cbrnr
Reply   •   1 楼
cbrnr    8 年前

尽管可以使用matplotlib命令遍历轴并单独设置标题,但使用seaborn的内置工具来控制标题更为简洁。例如:

# 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")

这样做的好处是不需要修改,而不管您是拥有1d面还是2d面。

ImportanceOfBeingErnest
Reply   •   2 楼
ImportanceOfBeingErnest    8 年前

您可以访问facetgrid的轴( g = sns.FacetGrid(...) 通过 g.axes . 这样,您就可以自由使用任何想要调整绘图的matplotlib方法。

更改标题 :

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 方法,因为它使标记哪些轴更加明显。