简体   繁体   English

如何改善Seaborn轴上的标签间距

[英]How to improve spacing of labels on Seaborn axis

My code below produces a graph where the labels on the x-axis are squished together and hard to read. 我的下面的代码生成一个图表,其中x轴上的标签被挤压在一起并且难以阅读。 How can I show just every other year instead of all years? 我怎样才能显示每隔一年而不是所有年份?

import seaborn as sns
import pandas as pd

years = list(range(1996,2017))
count = [2554.9425,2246.3233,1343.7322,973.9502,706.9818,702.0039,725.9288,
         598.7818,579.0219,485.8281,474.9578,358.1385,311.4344,226.3332,
        161.4288,132.7368,78.1659,39.8121,23.1321,0.2232,0.0015]
df = pd.DataFrame({'year':years, 'count':count})

dot_size = 0.7
ax = sns.pointplot(x="year",y="count", scale = dot_size, data=df)
ax.set(xlabel='Year', ylabel='Amount')
ax.set(ylim=(0, 3000))

在此输入图像描述

If you just want to show every other year instead of all years,you can use set_visible method: 如果您只想显示每隔一年而不是所有年份,您可以使用set_visible方法:

xticks=ax.xaxis.get_major_ticks()
for i in range(len(xticks)):
    if i%2==1:
        xticks[i].set_visible(False)

plt.show()

And you will get: 你会得到:

在此输入图像描述

Actually,the spacing between labels is dynamic,I tried to run your code with plt.show() ,and resize the image window,and it looks better. 实际上,标签之间的间距是动态的,我试图用plt.show()运行你的代码,并调整图像窗口的大小,它看起来更好。

By the way,maybe 顺便说一下,也许吧

ax.set_xticklabels( years, rotation=45 ) is also a good way. ax.set_xticklabels( years, rotation=45 )也是一个好方法。

Hope this helps. 希望这可以帮助。

使用旋转

ax.set_xticklabels(years, rotation=30)

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

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