Seaborn tsplot change labels on x-axis

I pass the tsplot a list of lists (say 31 items in each of the lists) and it shows the x-axis labels from 0 to 31.

How can I do this instead of show -15 to 15?

Example from the tutorial , if needed:

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

      

+3


source to share


1 answer


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 matplotlib's general way of manipulating tick marks. As suggested by seabed creator @mwaskom, you can also do:



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

      

enter image description here

+6


source







All Articles