[英]How to add unit to axis-label at 'ridge plot' from seaborn
我想根據 seaborn 'ridge plot' 示例( https://seaborn.pydata.org/examples/kde_ridgeplot.html )執行以下操作:
我想將單位添加到 x 軸的 x 標簽中,使其在圖中顯示為[m]中的x 。 我怎么做? 將命令g.map(label, "x")
擴展到g.map(label, "x in [m]")
會導致以下錯誤: KeyError: "['x [m]'] not in index"
我的代碼如下:
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)
只是玩弄軸對象g
,我發現了一種使用set_xlabels
的簡單且看似直接的方法。 在這里閱讀文檔,感謝@DavidG 找到它。 具體來說,
set_xlabels([label]) 在網格的底行標記 x 軸。
g.map(label, "x")
g.set_xlabels('x in [m]')
只是一個附加說明,您可能希望將 pal 中的數量增加到 13 以保持顏色漸變。
pal = sns.cubehelix_palette(13, rot=-.25, light=.7)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.