繁体   English   中英

如何在Seaborn FacetGrid中更改子图标题和轴标签的位置?

[英]How to change the positions of subplot titles and axis labels in Seaborn FacetGrid?

我正在尝试使用Seaborn的facetGrid绘制极坐标图,类似于在seaborn的画廊中详细描述的内容,我正在使用以下代码:

sns.set(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1.25)

# Set up a grid of axes with a polar projection
g = sns.FacetGrid(df_total, col="Construct", hue="Run", col_wrap=5,    subplot_kws=dict(projection='polar'), size=5, sharex=False, sharey=False, despine=False)

# Draw a scatterplot onto each axes in the grid
g.map(plt.plot, 'Rad', ''y axis label', marker=".", ms=3,   ls='None').set_titles("{col_name}")

plt.savefig('./image.pdf')

根据我的数据,结果如下:

在此处输入图片说明

我想保持每行5个地块的组织。

问题在于每个子图的标题与刻度的值重叠,与y轴标签相同。

有没有办法防止这种行为? 我可以以某种方式将标题稍微移动到其当前位置上方吗?可以将y轴标签稍微移动到其当前位置左侧吗?

提前谢谢了!

编辑:这不是这种重复SO的问题是一个插曲的标题重叠另一个插曲的轴标签。

在这里我的问题是一个插曲的标题具有相同的次要情节的蜱标签重叠,同样轴标签使用相同的次要情节的蜱标签重叠。

我还想补充一点,我不在乎它们在我的jupyter笔记本上重叠(因为它是用它创建的),但是我希望最终保存的图像没有重叠,所以也许我需要做一些保存工作为了避免这种情况,图片的格式略有不同,但是我不知道该怎么办(我仅使用plt.savefig保存该图片)。

编辑2:如果有人想重现问题,这里是一个最小的示例:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

sns.set()
sns.set(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1.5)
# Generate an example radial datast
r = np.linspace(0, 10000, num=100)
df = pd.DataFrame({'label': r, 'slow': r, 'medium-slow': 1 * r, 'medium': 2 * r, 'medium-fast': 3 * r, 'fast': 4 * r})

# Convert the dataframe to long-form or "tidy" format
df = pd.melt(df, id_vars=['label'], var_name='speed', value_name='theta')

# Set up a grid of axes with a polar projection
g = sns.FacetGrid(df, col="speed", hue="speed",
                  subplot_kws=dict(projection='polar'), size=4.5, col_wrap=5,
                  sharex=False, sharey=False, despine=False)

# Draw a scatterplot onto each axes in the grid
g.map(plt.scatter, "theta", "label")

plt.savefig('./image.png')
plt.show()

这给出了以下图像,其中标题不像我原来的问题那样糟糕(但仍然有些重叠),并且左侧的标签完全重叠。 在此处输入图片说明

为了将标题移高一点,您可以将其设置在新位置,

ax.title.set_position([.5, 1.1])

为了将ylabel向左移一点,您可以添加一些填充

ax.yaxis.labelpad = 25

要针对facetgrid的轴执行此操作,请执行以下操作:

for ax in g.axes:
    ax.title.set_position([.5, 1.1])
    ax.yaxis.labelpad = 25

在此SO 问题中 ImportanceOfBeingErnest提供的答案可能会有所帮助。

暂无
暂无

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

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