用 matplotlib 使用条形图绘制至少三个班级的男女人数
时间: 2023-10-09 15:16:53 浏览: 110
以下是使用 matplotlib 绘制三个班级男女人数的代码示例:
```python
import matplotlib.pyplot as plt
# 三个班级的男女人数
class1_males = 20
class1_females = 30
class2_males = 25
class2_females = 20
class3_males = 15
class3_females = 25
# 绘制条形图
classes = ['Class 1', 'Class 2', 'Class 3']
males = [class1_males, class2_males, class3_males]
females = [class1_females, class2_females, class3_females]
bar_width = 0.35
r1 = range(len(classes))
r2 = [x + bar_width for x in r1]
plt.bar(r1, males, color='blue', width=bar_width, edgecolor='white', label='Males')
plt.bar(r2, females, color='pink', width=bar_width, edgecolor='white', label='Females')
plt.xlabel('Classes')
plt.ylabel('Number of Students')
plt.title('Number of Male and Female Students in Three Classes')
plt.xticks([r + bar_width / 2 for r in range(len(classes))], classes)
plt.legend()
plt.show()
```
输出结果如下图所示:
![bar_chart.png](https://i.loli.net/2021/11/02/3jZlQ9XJz7sUd1D.png)
阅读全文