matplotlib绘制条形图案例
时间: 2023-07-12 11:18:34 浏览: 110
好的,以下是一个简单的matplotlib绘制条形图的案例代码:
```python
import matplotlib.pyplot as plt
# 数据
labels = ['A', 'B', 'C', 'D', 'E']
values = [20, 45, 30, 15, 10]
# 绘制条形图
plt.bar(labels, values)
# 设置横纵轴标签和标题
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart Example')
# 显示图形
plt.show()
```
解释一下代码:
1. 导入matplotlib库
2. 定义数据,包括标签和对应的值
3. 使用plt.bar函数绘制条形图,输入标签和值
4. 使用plt.xlabel、plt.ylabel和plt.title分别设置横纵轴标签和标题
5. 使用plt.show()显示图形
你可以根据自己的需求修改标签、值和其他参数,来绘制出符合自己需求的条形图。
阅读全文
相关推荐


















