Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve spacing of labels on Seaborn axis

Tags:

python

seaborn

My code below produces a graph where the labels on the x-axis are squished together and hard to read. 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))

enter image description here

like image 793
user554481 Avatar asked Dec 23 '22 20:12

user554481


2 Answers

If you just want to show every other year instead of all years,you can use set_visible method:

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:

enter image description here

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.

By the way,maybe

ax.set_xticklabels( years, rotation=45 ) is also a good way.

Hope this helps.

like image 93
McGrady Avatar answered Dec 26 '22 10:12

McGrady


Use rotation

ax.set_xticklabels(years, rotation=30)
like image 26
Vaishali Avatar answered Dec 26 '22 11:12

Vaishali