首页 > 解决方案 > 如何在 Matplotlib 的水平条形图中将最大比例放在顶部?

问题描述

在下面,我创建了一个水平条形图。然而,最低的比例在顶部。我是 Pandas 和 Matplotlib 的新手,所以我希望你能向我展示一种将最大比例放在顶部的简单方法。

 plt.style.use('fivethirtyeight')
 total = 9674

 langs = ['Computer science, computer engineering, or software engineering',
 'Another engineering discipline (such as civil, electrical, mechanical, etc.)',
 'Information systems, information technology, or system administration',
 'A natural science (such as biology, chemistry, physics, etc.)',
 'Mathematics or statistics',
 'A humanities discipline (such as literature, history, philosophy, etc.)',
 'A social science (such as anthropology, psychology, political science, etc.)',
 'A business discipline (such as accounting, finance, marketing, etc.)',
 'Fine arts or performing arts (such as graphic design, music, studio art, etc.)',
 'Web development or web design',
 'I never declared a major',
 'A health science (such as nursing, pharmacy, radiology, etc.)']

fig, ax = plt.subplots()

langs_users_num = np.array([5387, 764, 687, 528, 420, 407, 392, 385, 327, 212, 128, 37])

percent = langs_users_num/total*100

new_labels = [i+'  {:.2f}%'.format(j) for i, j in zip(langs, percent)]

plt.barh(langs, langs_users_num, color='lightskyblue', edgecolor='blue')
plt.yticks(range(len(langs)), new_labels)
plt.tight_layout()

for spine in ax.spines.values():
    spine.set_visible(False)


figure(num=None, figsize=(20, 20), dpi=100, facecolor='w', edgecolor='k')


ax.axes.get_xaxis().set_visible(False)
ax.tick_params(axis="y", left=False)
plt.show()
#plt.tight_layout()

标签: pythonpandasmatplotlib

解决方案


推荐阅读