Felipe Oliveira
Felipe Oliveira

Reputation: 95

How to adjust the spacing of Matplotlib and Seaborn graphics?

I'm not finding a function or ways to adjust my X axis, which contains many values, I wanted to sort them into a top 10, but I couldn't and the X axis was this mess with the values.

enter image description here enter image description here

Upvotes: 0

Views: 816

Answers (1)

JohanC
JohanC

Reputation: 80329

Use order=order[:10] to limit the plot to the 10 highest. Use ax.tick_params(axis='x', rotation=30) to rotate the ticks to make them easier to read.

Here is some code to test and show how it looks like:

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

N = 300
cars = pd.DataFrame({'CarName': [f'name_{n}' for n in np.random.randint(1, 30, N)]})
order = cars['CarName'].value_counts().index
ax = sns.countplot(cars['CarName'], data=cars, palette='rainbow', order=order[:10])
ax.tick_params(axis='x', rotation=30)
plt.tight_layout()
plt.show()

example plot

Upvotes: 1

Related Questions