如何在seaborn图中设置x轴刻度标签? [英] How to set x axis ticklabels in a seaborn plot?

查看:111
本文介绍了如何在seaborn图中设置x轴刻度标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法为 seaborn 线图正确设置 x 轴刻度标签.

I am unable to set x axis ticklabels for a seaborn line plot correctly.

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({'a':np.random.rand(8),'b':np.random.rand(8)})
sns.set(style="darkgrid")
g = sns.lineplot(data=df)
g.set_xticklabels(['2011','2012','2013','2014','2015','2016','2017','2018'])

x 轴上的年份未正确对齐.

The years on the x axis are not aligning properly.

推荐答案

每当你手动设置 x-ticklabels 时,你应该尝试先设置相应的刻度,然后再指定标签.在您的情况下,因此您应该这样做

Whenever you set the x-ticklabels manually, you should try to first set the corresponding ticks, and then specify the labels. In your case, therefore you should do

g = sns.lineplot(data=df)
g.set_xticks(range(len(df))) # <--- set the ticks first
g.set_xticklabels(['2011','2012','2013','2014','2015','2016','2017','2018'])

这篇关于如何在seaborn图中设置x轴刻度标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆