[英]How to adjust xlim, increase the distance between each graph and set a title for the '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的“山脊图”示例( https://seaborn.pydata.org/examples/kde_ridgeplot.html )执行以下操作:
I tried it with sns.plt.xlim(-10, 3)
, which leads to an error. 我尝试了
sns.plt.xlim(-10, 3)
,这导致了错误。 And here is my whole code: 这是我的整个代码:
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")
# 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)
For 1) you can change the x limits in the same call to g.set
when you remove the yticks: 对于1),您可以在删除yticks的同一调用
g.set
更改x限制:
g.set(yticks=[], xlim=[-5, 3])
For 2) you can just adjust the hspace
value in subplots_adjust
对于2),您可以仅在
subplots_adjust
调整hspace
值
For 3) you can create a suptitle
for the figure: 对于3),您可以为图形创建
suptitle
:
g.fig.suptitle("Some title")
This gives something like: 这给出了类似的内容:
Following is one way to address all the three points: 以下是解决所有这三点的一种方法:
g.set(xlim=(-4, 3))
sets the x-limits g.set(xlim=(-4, 3))
设置x限制 ax.text(0, .25,...)
A higher offset of 0.25 instead of 0.2 correctly positions the error names. ax.text(0, .25,...)
0.25而不是0.2的更高偏移量可以正确定位错误名称。 plt.suptitle('Main title')
puts a main title on the top of the figure. plt.suptitle('Main title')
在图的顶部放置一个主标题。 g.set(xlim=(-4, 3))
def label(x, color, label):
ax = plt.gca()
ax.text(0, .25, label, fontweight="bold", color=color,
ha="left", va="center", transform=ax.transAxes)
g.map(label, "x")
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)
plt.suptitle('Main title')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.