python 画折线统计图
Python 画折线统计图(line chart)最常用的是 matplotlib
。
最基本的折线图代码如下:
import matplotlib.pyplot as plt# 假设这是你的数据
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]# 创建折线图
plt.plot(x, y, marker='o') # marker='o' 是在点上画小圆圈
plt.title('Simple Line Chart')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')# 显示网格
plt.grid(True)# 展示图表
plt.show()
小总结:
plot(x, y)
画线marker='o'
在每个数据点加个小圈(好看!)title()
,xlabel()
,ylabel()
加标题grid(True)
加网格线(统计图必备)
进阶一点点:多条折线!
import matplotlib.pyplot as plt# 两组数据
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [2, 5, 10, 17, 26]plt.plot(x, y1, label='Data 1', marker='o')
plt.plot(x, y2, label='Data 2', marker='s')plt.title('Multiple Lines Example')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend() # 添加图例
plt.grid(True)plt.show()