【问题标题】:Seaborn tsplot change labels on x-axisSeaborn tsplot 在 x 轴上更改标签
【发布时间】:2015-02-10 05:42:02
【问题描述】:

我正在向 tsplot 传递一个列表列表(比如每个列表中有 31 个项目),它显示的 x 轴标签为 0 到 31。

我怎样才能让它显示 -15 到 15?

the tutorial 中的示例,如有必要:

import numpy as np
np.random.seed(9221999)
import pandas as pd
from scipy import stats, optimize
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(palette="Set2")

def sine_wave(n_x, obs_err_sd=1.5, tp_err_sd=.3):
    x = np.linspace(0, (n_x - 1) / 2, n_x)
    y = np.sin(x) + np.random.normal(0, obs_err_sd) + np.random.normal(0, tp_err_sd, n_x)
    return y

sines = np.array([sine_wave(31) for _ in range(20)])
sns.tsplot(sines);

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    你可以这样做:

    ax = sns.tsplot(sines);                      # capture axis
    n = len(ax.xaxis.get_ticklabels())           # count labels
    ax.set_xticklabels(np.linspace(-15, 15, n))  # set new tick labels
    

    编辑: 之前的解决方案是通用 matplotlib 操作刻度标签的方式。正如 seaborn 的创建者 @mwaskom 所建议的,您也可以这样做:

    sns.tsplot(sines, time=np.linspace(-15, 15, sines.shape[1]))
    

    【讨论】:

    • 是的,尽管您也可以将刻度标签传递给tsplottime 参数。
    猜你喜欢
    • 2016-09-03
    • 2017-05-11
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多