You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Seaborn jointplot边际直方图设置对数刻度及显示纵轴刻度问题

完整实现代码

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# 示例数据,可替换为你自己的数据集
np.random.seed(42)
my_df = pd.DataFrame({
    'x_axis': np.random.randn(1000),
    'y_axis': np.random.randn(1000)
})

my_plot = sns.jointplot(x='x_axis', y='y_axis', data=my_df)
my_plot.fig.set_figwidth(19)
my_plot.fig.set_figheight(3)

# ------------------- 边际直方图对数刻度配置 -------------------
# 上方x轴边际直方图:计数对应y轴,设置y轴对数刻度
my_plot.ax_marg_x.set_yscale('log')
# 右侧y轴边际直方图:计数对应x轴,设置x轴对数刻度
my_plot.ax_marg_y.set_xscale('log')

# ------------------- 边际直方图垂直刻度显示配置 -------------------
# 处理上方边际直方图:开启y轴刻度线与刻度标签
my_plot.ax_marg_x.tick_params(axis='y', labelleft=True, left=True)
# 处理右侧边际直方图:开启x轴刻度线与刻度标签
my_plot.ax_marg_y.tick_params(axis='x', labelbottom=True, bottom=True)

plt.show()

配置说明

  • 对数刻度逻辑:seaborn jointplot默认将上方边际图ax_marg_x的纵轴作为计数轴,右侧边际图ax_marg_y的横轴作为计数轴,直接调用对应轴的set_yscale/set_xscale方法即可实现对数效果。
  • 刻度显示逻辑:seaborn默认隐藏了边际轴的刻度线与刻度标签,你之前的代码仅设置了单个轴的标签可见,未开启刻度线,也没有配置右侧边际轴,因此无法生效。通过tick_params同时开启刻度线和标签的可见性即可正常显示垂直刻度。

内容的提问来源于stack exchange,提问作者aBiologist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 18:39:06