python代码,编写折线图,条形图,饼状图
时间: 2023-03-20 14:00:48 浏览: 126
以下是Python中使用matplotlib库来绘制折线图、条形图和饼状图的基本示例代码:
## 折线图
```python
import matplotlib.pyplot as plt
# 数据
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
# 绘制折线图
plt.plot(x, y)
# 添加标题和坐标轴标签
plt.title("Line Chart Example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# 显示图像
plt.show()
```
## 条形图
```python
import matplotlib.pyplot as plt
# 数据
x = ["A", "B", "C", "D", "E"]
y = [10, 8, 6, 4, 2]
# 绘制条形图
plt.bar(x, y)
# 添加标题和坐标轴标签
plt.title("Bar Chart Example")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# 显示图像
plt.show()
```
## 饼状图
```python
import matplotlib.pyplot as plt
# 数据
labels = ["A", "B", "C", "D", "E"]
sizes = [10, 8, 6, 4, 2]
# 绘制饼状图
plt.pie(sizes, labels=labels)
# 添加标题
plt.title("Pie Chart Example")
# 显示图像
plt.show()
```
阅读全文
相关推荐






