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

问题描述 投票:0回答:1

我没有找到一个函数或方法来调整我的X轴,该轴包含很多值,我想将它们排在前10位,但是我不能,而且X轴也弄乱了这些值。

enter image description hereenter image description here

matplotlib graphics seaborn data-science axis
1个回答
0
投票

使用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.show()
© www.soinside.com 2019 - 2024. All rights reserved.