简体   繁体   English

如何在seaborn的“岭图”处向轴标签添加单位

[英]How to add unit to axis-label at 'ridge plot' from seaborn

I would like to do following based on the seaborn 'ridge plot' example ( https://seaborn.pydata.org/examples/kde_ridgeplot.html ):我想根据 seaborn 'ridge plot' 示例( https://seaborn.pydata.org/examples/kde_ridgeplot.html )执行以下操作:

I want to add the unit to the x label of the x-axis, such that it appears as x in [m] in the plot.我想将单位添加到 x 轴的 x 标签中,使其在图中显示为[m]中的x How do I do that?我怎么做? Extending the command g.map(label, "x") to g.map(label, "x in [m]") leads to following error: KeyError: "['x [m]'] not in index"将命令g.map(label, "x")扩展到g.map(label, "x in [m]")会导致以下错误: KeyError: "['x [m]'] not in index"

My code is the following one:我的代码如下:

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)})


errorNames = ['Error 1 - abc.....',
              'Error 2 - abc.....',
              'Error 3 - abc.....',
              'Error 4 - abc.....',
              'Error 5 - abc.....',
              'Error 6 - abc.....',
              'Error 7 - abc.....',
              'Error 8 - abc.....',
              'Error 9 - abc.....',
              'Error 10 - abc.....',
              'Error 11 - abc.....',
              'Error 12 - abc.....',
              'Error 13 - abc.....']


# Create the data
rs = np.random.RandomState(1979)
x = rs.randn(650)
#g = np.tile(list("ABCDEFGHIJKLM"), 50)
g = np.tile(list(errorNames), 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)


#sns.plt.xlim(-10, 3)


# 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")
#g.map(label, "x [m]")

# Set the subplots to overlap 
# Erst hier wird geplotted
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)

Just some playing around with the axis object g and I found a simple and seemingly direct way to do it using set_xlabels .只是玩弄轴对象g ,我发现了一种使用set_xlabels的简单且看似直接的方法。 Read the docs here , thanks to @DavidG for finding it.这里阅读文档,感谢@DavidG 找到它。 Specifically,具体来说,

set_xlabels([label]) Label the x axis on the bottom row of the grid. set_xlabels([label]) 在网格的底行标记 x 轴。

g.map(label, "x")
g.set_xlabels('x in [m]')

在此处输入图片说明

One way would be to get an array of the axes using g.axes .一种方法是使用g.axes获取轴数组。 Then get the last axes in the array and set the xlabel of that axes:然后获取数组中的最后一个轴并设置该轴的 xlabel:

last_ax = g.axes.flat[-1]
last_ax.set_xlabel("x in [m]")

在此处输入图片说明

只是一个附加说明,您可能希望将 pal 中的数量增加到 13 以保持颜色渐变。

pal = sns.cubehelix_palette(13, rot=-.25, light=.7)

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

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