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

使用Seaborn绘制多量级时间序列,是否支持Y轴归一化功能?

解决Seaborn lmplot中子图Y轴归一化的问题

Seaborn的lmplot本身没有内置的归一化参数,但可以通过两种方式实现你要的效果,且无需提前修改原DataFrame:

方法1:让每个子图使用独立Y轴范围(最简单高效)

直接在lmplot中设置sharey=False,这样每个子图会根据自身数据的Y值范围生成独立刻度,完全规避量级差异带来的对比问题,效果远好于统一对数轴:

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

gbp_stats = pd.read_csv('price_data.csv')

# sharey=False 让每个子图使用专属Y轴
sns.lmplot(data=gbp_stats, x='numeric_time', y='last trade price', col='symbol', sharey=False)

plt.show()

方法2:通过FacetGrid自定义归一化绘图

如果确实需要对Y值做归一化(比如统一映射到[0,1]区间),可以基于FacetGrid手动实现,不修改原数据:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from sklearn.preprocessing import minmax_scale

gbp_stats = pd.read_csv('price_data.csv')

# 创建分面子图网格
g = sns.FacetGrid(gbp_stats, col='symbol')

# 定义归一化绘图函数
def plot_normalized_reg(x, y, **kwargs):
    # 对当前子图的Y数据做min-max归一化
    y_norm = minmax_scale(y)
    sns.regplot(x=x, y=y_norm, **kwargs)
    plt.ylabel('归一化后价格')

# 将函数映射到每个子图
g.map(plot_normalized_reg, 'numeric_time', 'last trade price')

plt.show()

这里用了minmax_scale做[0,1]归一化,你也可以换成StandardScaler做Z-score归一化,根据需求调整即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 12:05:31