簡體   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