【问题标题】:Add y-axis label to ridgeline plot with seaborn使用 seaborn 将 y 轴标签添加到山脊线图
【发布时间】:2019-09-22 22:35:56
【问题描述】:

我想在 python 中使用 seaborn 为密度脊线图添加 y 轴标签。为了制作山脊线图,我遵循the seaborn gallery 中的代码。为方便起见,我在下面复制了他们的代码 sn-p。我应该如何修改它以不与密度曲线重叠的方式标记 y 轴?

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)

为了清楚起见,我希望情节看起来像:

【问题讨论】:

    标签: python seaborn ridgeline-plot


    【解决方案1】:

    您可以像文本一样添加通用标签,使用以下代码行g.fig.text(0.04, 0.5, 'Y axis label', va='center', rotation='vertical'),您将获得以下内容:

    【讨论】:

      【解决方案2】:

      我认为用g.fig.subplots_adjust(hspace=.1) 替换g.fig.subplots_adjust(hspace=-.25) 应该可以解决问题。

      【讨论】:

      • 为了澄清,我的意思是 y 轴的标题,而不是标记 y 轴的级别。
      猜你喜欢
      • 2020-11-27
      • 2020-11-05
      • 1970-01-01
      • 2014-12-07
      • 2013-01-23
      • 2019-07-28
      • 2022-11-24
      • 2022-01-18
      相关资源
      最近更新 更多