Seaborn Joyplot无法完全填充至顶部问题求助
解决Seaborn Joyplot无法填充至顶部的问题
我明白你在用Seaborn制作Joyplot时碰到了图像没法完全填充到顶部的问题,这大概率是因为子图间距、布局参数没调整到位。我把你的代码补全并修正了关键设置,能完美解决这个问题:
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, fill=True, alpha=1, linewidth=1.5) g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2) g.refline(y=0, linewidth=2, linestyle="-", color=None, clip_on=False) # Adjust the subplots to overlap and fill the top g.figure.subplots_adjust(hspace=-0.25) # Remove axes details that don't play well with overlap g.set_titles("") g.set(yticks=[], ylabel="") g.despine(bottom=True, left=True) # 保存图像 plt.savefig("joyplot.png", dpi=300, bbox_inches="tight") plt.close()
关键修正点:
- 设置
hspace=-0.25:通过负数的子图垂直间距,让每个密度图向上重叠,彻底消除顶部的空白区域 - 确保
fill=True:让密度曲线下方完全填充颜色,避免出现半透明或未填充的情况 - 添加
bbox_inches="tight"到plt.savefig():确保保存图像时不会裁剪掉边缘内容,保证整体布局完整
内容的提问来源于stack exchange,提问作者sammosummo
相关产品推荐
相关产品推荐

