簡體   English   中英

將 y 軸 label 添加到山脊線 plot 和 seaborn

[英]Add y-axis label to ridgeline plot with seaborn

我想使用 seaborn 在 Z236EEEB4347BDD755AB 為了制作山脊線 plot,我遵循seaborn 庫中的代碼。 為方便起見,我在下面復制了他們的代碼片段。 我應該如何以不與密度曲線重疊的方式將其修改為 y 軸 label?

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})

# Create the data
rs = np.random.RandomState(1979)
x = rs.randn(500)
g = np.tile(list("ABCDEFGHIJ"), 50)
df = pd.DataFrame(dict(x=x, g=g))
m = df.g.map(ord)
df["x"] += m

# Initialize the FacetGrid object
pal = sns.cubehelix_palette(10, rot=-.25, light=.7)
g = sns.FacetGrid(df, row="g", hue="g", aspect=15, height=.5, palette=pal)

# Draw the densities in a few steps
g.map(sns.kdeplot, "x", clip_on=False, shade=True, alpha=1, lw=1.5, bw=.2)
g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw=.2)
g.map(plt.axhline, y=0, lw=2, clip_on=False)


# Define and use a simple function to label the plot in axes coordinates
def label(x, color, label):
    ax = plt.gca()
    ax.text(0, .2, label, fontweight="bold", color=color,
            ha="left", va="center", transform=ax.transAxes)


g.map(label, "x")

# Set the subplots to overlap
g.fig.subplots_adjust(hspace=-.25)

# Remove axes details that don't play well with overlap
g.set_titles("")
g.set(yticks=[])
g.despine(bottom=True, left=True)

需要明確的是,我希望 plot 看起來像: 想要的情節

您可以像文本一樣添加一般的 label 使用以下代碼行g.fig.text(0.04, 0.5, 'Y axis label', va='center', rotation='vertical') ,您將獲得以下內容: 在此處輸入圖像描述

我認為用 g.fig.subplots_adjust( g.fig.subplots_adjust(hspace=.1)替換g.fig.subplots_adjust(hspace=-.25) ) 應該可以解決問題。

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM