简体   繁体   English

Seaborn tsplot在x轴上更改标签

[英]Seaborn tsplot change labels on x-axis

I'm passing tsplot a list of lists (say with 31 items in each of the lists) and it shows x-axis labels of 0 to 31. 我正在通过tsplot列表列表(比如每个列表中有31个项目),它显示0到31的x轴标签。

How can I make it instead show -15 to 15? 如何让它显示-15到15?

Example from the tutorial , if its necessary: 教程中的示例,如果必要的话:

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);

You can do it like this: 你可以这样做:

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

Edit: The previous solution is the generic matplotlib's way of manipulating the tick labels. 编辑:以前的解决方案是通用的matplotlib操纵刻度标签的方式。 As suggested by seaborn's creator @mwaskom you can also do: 正如seaborn的创建者@mwaskom所建议你也可以这样做:

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM