如何更改 Seaborn FacetGrid 中子图标题和轴标签的位置?
How to change the positions of subplot titles and axis labels in Seaborn FacetGrid?
我正在尝试使用 Seaborn 的 facetGrid 绘制极坐标图,类似于 seaborn's gallery 中的详细说明
我正在使用以下代码:
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 轴标签略微移动到其当前位置的左侧?
非常感谢!
编辑:
这不是这个 的副本,因为问题是一个子图的标题与 另一个 子图的轴标签重叠.
这里我的问题是一个子图的标题与 相同 子图的刻度标签重叠,同样轴标签重叠与 相同的 子图的刻度标签。
我还想补充一点,我不在乎它们在我的 jupyter notebook 上重叠(因为它是用它创建的),但是我想要 final 保存的图像没有重叠,所以也许我需要做一些事情来以稍微不同的格式保存图像以避免这种情况,但我不知道是什么(我只使用 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()
下图的标题没有我原来的问题那么糟糕(但仍有一些重叠),左侧的标签完全重叠。
ImportanceOfBeingErnest 在此 SO 中提供的答案可能会有所帮助。
为了将标题移高一点,您可以在新位置设置,
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
我正在尝试使用 Seaborn 的 facetGrid 绘制极坐标图,类似于 seaborn's gallery 中的详细说明 我正在使用以下代码:
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 轴标签略微移动到其当前位置的左侧?
非常感谢!
编辑:
这不是这个
这里我的问题是一个子图的标题与 相同 子图的刻度标签重叠,同样轴标签重叠与 相同的 子图的刻度标签。
我还想补充一点,我不在乎它们在我的 jupyter notebook 上重叠(因为它是用它创建的),但是我想要 final 保存的图像没有重叠,所以也许我需要做一些事情来以稍微不同的格式保存图像以避免这种情况,但我不知道是什么(我只使用 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()
下图的标题没有我原来的问题那么糟糕(但仍有一些重叠),左侧的标签完全重叠。
ImportanceOfBeingErnest 在此 SO
为了将标题移高一点,您可以在新位置设置,
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