user554481
user554481

Reputation: 2075

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. 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

Upvotes: 5

Views: 13262

Answers (2)

McGrady
McGrady

Reputation: 11487

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.

Upvotes: 9

Vaishali
Vaishali

Reputation: 38415

Use rotation

ax.set_xticklabels(years, rotation=30)

Upvotes: 1

Related Questions