Seaborn jointplot叠加scatterplot后无法显示完整网格边框如何解决
问题描述
使用seaborn的jointplot和scatterplot绘制如下类型的图表:
当前遇到的问题是图表无法显示完整网格边框,本应作为边框存在的部分网格出现缺失,原有代码如下:
import pandas as pd import seaborn as sns df1 = pd.DataFrame({'col1': X, 'col2':Y}) df2 = pd.DataFrame({'col1': X_main_map, 'col2':Y_main_map}) graph = sns.jointplot(data = df1,x="col1", y="col2",kind="scatter",s=2,color='darkred',marginal_kws={"bins":100,"color":"darkred"},marginal_ticks = True) graph2 = sns.scatterplot(data = df2, x = "col1",y="col2", s=1,ax=graph.ax_joint) graph2.set(xticklabels=[]) graph2.set(xlabel=None) graph2.tick_params(left=False) # remove the ticks graph2.set(yticklabels=[]) graph2.set(ylabel=None) graph2.tick_params(bottom=False) # remove the ticks graph2.set(ylim=(-12,20),xlim=(-10,13))
解决方案
该问题是因为seaborn默认会隐藏jointplot联合绘图区域的上、右侧脊线,隐藏刻度的操作也可能连带影响边框的显示状态,在原有代码末尾追加如下配置即可恢复完整边框:
# 显示四个方向的轴脊线(即四周边框) for spine in graph2.spines.values(): spine.set_visible(True) # 可选:调整边框粗细,让边框更醒目 spine.set_linewidth(1.2) # 如需显示完整的背景网格,再追加以下代码 graph2.grid(True, which='both', axis='both', linestyle='-', alpha=0.3)
如果仅需要保留四周边框不需要显示背景网格,只保留前半部分脊线配置即可。
内容的提问来源于stack exchange,提问作者Miguel
相关产品推荐
相关产品推荐

