刻度标记周围的填充

时间:2019-05-06 14:19:13

标签: matplotlib seaborn

我正在制作条形图。

sns_plot = sns.barplot(years, yields, ax=axes[0, 0])
sns_plot.set_xticklabels(years, rotation=90)
sns_plot.xaxis.set_tick_params(pad=10)

但是文字太乱了。即标签之间需要分开。我怎样才能做到这一点? pad=10似乎是从轴上推标签,而不是从标签上推。

标签垂直放置(90度)后,它们仍会聚在一起。我想找到合适的拉杆...

1 个答案:

答案 0 :(得分:0)

测试数据:

In [1]:
import numpy as np
import re

x = """Lorem ipsum dolor sit amet, consectetur adipiscing 
elit, sed do eiusmod tempor incididunt ut labore et dolore 
magna aliqua. Cras nibh turpis, ullamcorper ac lectus vel, 
aliquet consectetur odio. Cras vel scelerisque tortor. 
Interdum et malesuada fames ac ante ipsum primis in faucibus. 
Proin id dignissim ante, a dictum ipsum. Fusce at lacus ac purus 
pulvinar dignissim eget a quam. Sed quis mollis ligula, sed 
ullamcorper velit. Curabitur vel congue metus. Ut placerat 
ipsum non leo posuere, non vestibulum eros posuere. 
Donec eu viverra augue, sit amet tempus ex. Vivamus 
sit amet tempus ipsum. Fusce consequat, augue a mollis 
hendrerit, quam neque dapibus ligula, vitae blandit ipsum 
lorem eu mauris. """
x = pd.Series(x.split(' '))
x = x.apply(lambda x: re.sub('\W+', '', x))
y = np.random.randn(x.shape[0])

df = pd.DataFrame({'X': x, 'Y': y})
df.head()


Out [1]:
    X   Y
0   Lorem   -0.562246
1   ipsum   1.085094
2   dolor   1.044887
3   sit     -1.424002
4   amet    -0.87682

测试图:

In [2]:
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1, figsize=(9, 6))
ax.plot(df.X, df.Y, 'ks')
ax.tick_params(axis='x', rotation=90)

Out [2]:

因此,要总结对原始帖子的评论,有多种方法可以删除混乱的刻度标签,您可以有多种选择:

1)小标签文字:ax.tick_params(axis='x', labelsize=6) enter image description here

2)更少的标签:

for label in ax.xaxis.get_ticklabels()[::2]:
    label.set_visible(False)

enter image description here

3)长轴:fig.set_size_inches((15, 4)) enter image description here 等等...