Matplotlib是Python中常用的绘图库,使用Matplotlib可以实现数据的可视化。常用的绘制方法有:
- 导入matplotlib模块
## python www.itzhimei.com 代码
import matplotlib.pyplot as plt
- 绘制简单线图
## python www.itzhimei.com 代码
plt.plot([1,2,3])
plt.show()
- 设置标题和标签
## python www.itzhimei.com 代码
plt.title("Line Chart")
plt.xlabel("x axis")
plt.ylabel("y axis")
- 绘制条形图
## python www.itzhimei.com 代码
plt.bar([1,2,3],[4,5,6])
- 绘制散点图
## python www.itzhimei.com 代码
plt.scatter([1,2,3],[4,5,6])
- 绘制饼图
## python www.itzhimei.com 代码
plt.pie([10,20,30])
- 设置图例
## python www.itzhimei.com 代码
plt.plot([1,2,3],label="line")
plt.legend()
- 保存图像
## python www.itzhimei.com 代码
plt.savefig("image.png")
通过Matplotlib,可以便捷地绘制各种图表,从而对数据进行可视化。