python 画柱状图
时间: 2023-07-06 19:38:44 浏览: 61
好的,下面是使用 Python 和 Matplotlib 库画柱状图的基本步骤:
1. 导入 Matplotlib 库:
```
import matplotlib.pyplot as plt
```
2. 准备数据:
```
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
```
3. 创建一个画布和子图:
```
fig, ax = plt.subplots()
```
4. 使用 `bar` 函数画出柱状图:
```
ax.bar(x, y)
```
5. 添加标题和轴标签:
```
ax.set_title('Bar Chart')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
```
6. 显示图形:
```
plt.show()
```
完整代码如下:
```
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
fig, ax = plt.subplots()
ax.bar(x, y)
ax.set_title('Bar Chart')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
plt.show()
```
你可以根据自己的需求修改数据和图形的属性,例如颜色、宽度、标签等。