[英]How can I plot multiple subplots with respect to multiple ridge plots? (seaborn)
Recently, I want to plot several multiple ridge plots in a single plot.最近,我想在一个 plot 中 plot 几个多脊图。 Thus, I try to use the subplot command to achieve this goal.
因此,我尝试使用 subplot 命令来实现这个目标。 However, it seems that it doesn't work.
但是,它似乎不起作用。 When I run the following code fragment, it should output a single picture containing two ridge plots.
当我运行以下代码片段时,它应该 output 一张包含两个山脊图的图片。 However, in fact, the following code fragment will output three figures, including two blank figures.
但是,其实下面的代码片段会 output 三个数字,包括两个空白数字。 How can I achieve my goal?
我怎样才能实现我的目标?
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
for i in range(2):
plt.subplot(1, 2, i + 1)
sns.set_theme(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",
bw_adjust=.5, clip_on=False,
fill=True, alpha=1, linewidth=1.5)
g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw_adjust=.5)
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)
plt.show()
The code below creates two columns of ridge plots for two dataframes.下面的代码为两个数据框创建了两列岭图。 It supposes the dataframes have a similar structure.
它假设数据帧具有相似的结构。 For seaborn to work well, the dataframes are combined together.
为使 seaborn 正常工作,将数据帧组合在一起。 Note that a lot of tweaking may be needed to get a good-looking result, depending on your specific data.
请注意,可能需要进行大量调整才能获得美观的结果,具体取决于您的具体数据。
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})
rs = np.random.RandomState(1979)
# create a first data frame
x1 = rs.randn(500)
g1 = np.repeat(list("ABCDEFGHIJ"), 50)
df1 = pd.DataFrame(dict(x=x1 + np.repeat(np.arange(2, 22, 2), 50), g=g1))
# create a second data frame of a similar form
x2 = rs.randn(700)
g2 = np.repeat(list("ABCDEFGHIJ"), 70)
df2 = pd.DataFrame(dict(x=x2 * 0.5 + np.repeat(np.arange(2, 22), 35), g=g2))
df1['source'] = 'df1'
df2['source'] = 'df2'
df = pd.concat([df1, df2])
df['hue'] = df['source'] + df['g']
# Initialize the FacetGrid object
pal1 = sns.cubehelix_palette(10, rot=-.15, light=.7)
pal2 = sns.cubehelix_palette(10, start=.4, rot=.15, light=.7)
g = sns.FacetGrid(df, row="g", hue="hue", col="source", aspect=15, height=.5, palette=pal1 + pal2)
# Draw the densities in a few steps
g.map(sns.kdeplot, "x",
bw_adjust=.5, clip_on=False,
fill=True, alpha=1, linewidth=1.5)
g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw_adjust=.5)
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[-1:], 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.set(xlabel="")
g.despine(bottom=True, left=True)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.