如何调整Matplotlib和Seaborn图形的间距?

时间:2020-04-23 18:56:40

标签: python matplotlib seaborn

我找不到用于调整X轴的函数或方法,该轴包含许多值,我想将它们排在前10位,但是我无法,并且X轴被这些值弄乱了。 / p>

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

使用order=order[:10]将情节限制在最高10位。 使用ax.tick_params(axis='x', rotation=30)旋转刻度线,使其更易于阅读。

这里有一些代码可以测试并显示其外观:

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

相关问题