Seaborn tsplot如何更改x轴标签

3

我将一个列表的列表传递给tsplot(假设每个列表中有31个项目),它显示0到31的x轴标签。

如何使它显示-15到15而不是0到31?

如果需要,可以参考教程中的示例:

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);
1个回答

6
你可以像这样做:
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]))

enter image description here


5
可以将刻度标签传递给tsplot函数的time参数,是的,没错。 - mwaskom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接