【问题标题】:I want to display number of each bar我想显示每个栏的数量
【发布时间】:2020-03-30 11:08:42
【问题描述】:
#plt.figure(figsize=(15,5))

bins=[0,10,20,30,40,50]

ax=plt.title('SEM-2 adv-c Marks wise clasification', fontdict={'fontname': 'Sans Serif', 'fontsize': 15})
ax=plt.xlabel('Marks',fontdict={'fontname': 'Sans Serif', 'fontsize': 15})
ax=plt.ylabel('Total Students',fontdict={'fontname': 'Sans Serif', 'fontsize': 15})

ax=plt.hist(sem2.ADV_C,bins, color='r', density=False, label='No of Student')


plt.legend()
plt.show()

我想显示每个条的数量以了解什么是分类数

【问题讨论】:

  • 您是指 x 轴上的 bin 编号/刻度还是..?

标签: python-3.x matplotlib data-visualization


【解决方案1】:

平均每个 bin 的左右边界会在每个条的中心给出刻度位置。可以在此处放置具有此左右界限的标签。

同样的 x 位置也可用于放置文本,例如每个 bin 中的数字。

from matplotlib import pyplot as plt
import numpy as np

bins = np.array([0, 10, 20, 30, 40, 50])
# create sum dummy data
sem2_ADV_C = (np.random.beta(4, 3, 500) * 50).astype(np.int)

plt.title('SEM-2 adv-c Marks wise clasification', fontdict={'fontname': 'Sans Serif', 'fontsize': 15})
plt.xlabel('Marks', fontdict={'fontname': 'Sans Serif', 'fontsize': 15})
plt.ylabel('Total Students', fontdict={'fontname': 'Sans Serif', 'fontsize': 15})

plt.hist(sem2_ADV_C, bins, color='crimson', density=False, label='No of Student', edgecolor='black')
plt.xticks((bins[:-1] + bins[1:]) / 2, [f'{b0}–{b1}' for b0, b1 in zip(bins[:-1], bins[1:])])
values, _, _ = plt.hist(sem2_ADV_C, bins, color='crimson', density=False, label='No of Student', edgecolor='black')
plt.xticks((bins[:-1] + bins[1:]) / 2, [f'{b0}–{b1}' for b0, b1 in zip(bins[:-1], bins[1:])])
for i, val in enumerate(values):
    plt.text((bins[i] + bins[i+1]) / 2, val, int(val), ha='center', va='bottom')

plt.show()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-04
  • 2019-07-20
  • 2018-10-30
  • 2017-10-14
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多