python - Seaborn tsplot 在 x 轴上更改标签

标签 python matplotlib seaborn

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

我怎样才能让它显示 -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);

最佳答案

你可以这样做:

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

关于python - Seaborn tsplot 在 x 轴上更改标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27427944/

相关文章:

python - 如何让 Node Web 客户端与 Heroku 上的 REST API 对话?

python - Matplotlib:annotate() 缺少 1 个必需的位置参数: 'self'

python - 获取seaborn因子图中箱线图的样本大小

matplotlib pyplot imshow 图像之间的紧密间距

python - Matplotlib 导航工具栏嵌入 pyqt5 窗口 - 重置原始 View 崩溃

python-3.x - 类型错误 : ufunc 'isnan' not supported for the input types, - seaborn 热图

python - 当色 block 数量超过颜色数量时,seaborn 会忽略自定义调色板

python - Python中所有可能的zip变体

python - 列的最大值和最小值之间的差异

python - 与 NumPy 等效的 Pandas nunique