Python中Matplotlib详解

文章目录

Python中Matplotlib详解

一、引言

Matplotlib是Python中一个非常流行的绘图库,它提供了丰富的接口来绘制高质量的图形。无论是简单的线图、复杂的散点图,还是动态交互式的可视化,Matplotlib都能轻松应对。本文将详细介绍Matplotlib的使用方法,包括基本的绘图、定制化设置以及多种图表类型的绘制。

二、Matplotlib基础

1、包的引入

在使用Matplotlib之前,需要先安装并引入这个库。

python 复制代码
# 安装Matplotlib
# 在命令行输入pip install matplotlib

import matplotlib.pyplot as plt
import numpy as np

# 查看版本号
print(plt.matplotlib.__version__)

2、基本绘图

Matplotlib的pyplot模块提供了类似于MATLAB的绘图框架。

2.1、绘制线图
python 复制代码
# 绘制x和y点
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.show()
2.2、无线绘图
python 复制代码
# 仅绘制标记点,使用快捷字符串符号参数'o'
plt.plot(xpoints, ypoints, 'o')
plt.show()
2.3、多点绘制
python 复制代码
# 可以根据需要绘制任意数量的点
xpoints = np.array([33, 7, 6, 13])
ypoints = np.array([3, 23, 88, 42])
plt.plot(xpoints, ypoints)
plt.show()

三、定制化图表

3、标记和颜色

3.1、标记
python 复制代码
# 关键字:marker,用指定的标记强调每个点
plt.plot(xpoints, ypoints, marker='*')
plt.show()
3.2、颜色参考
字符 颜色
'b' 蓝色
'g' 绿色
'r' 红色
'c' 青色
'm' 品红色
'y' 黄色
'k' 黑色
'w' 白色
3.3、格式化字符串fmt
python 复制代码
# 格式:marker\|line\|color
plt.plot(xpoints, ypoints, 'o:r')
plt.show()

4、线条样式

4.1、线条
python 复制代码
# 使用关键字linestyle或ls来更改线条样式
plt.plot(xpoints, ypoints, ls='dashed')
plt.show()
4.2、线条颜色和宽度
python 复制代码
# 使用关键字color或c来设置线条颜色
# 使用关键字linewidth或lw来设置线条宽度
plt.plot(xpoints, ypoints, lw=2.5, color='blue')
plt.show()

5、多条线

python 复制代码
# 添加更多的plt.plot()函数来绘制任意数量的线
plt.plot(xpoints, ypoints1)
plt.plot(xpoints, ypoints2)
plt.show()

四、图表增强

6、标签与标题

6.1、设置标签
python 复制代码
# 设置字体为楷体
plt.rcParams['font.sans-serif'] = ['KaiTi']

plt.plot(xpoints, ypoints)
plt.xlabel('时间节点')
plt.ylabel('收入')
plt.show()
6.2、设置标题
python 复制代码
plt.title('我是标题')
plt.show()

7、网格线

python 复制代码
# 添加网格线
plt.grid()
plt.plot(xpoints, ypoints)
plt.show()

8、多图显示

8.1、subplots
python 复制代码
# 图一
plt.subplot(1, 2, 1)
plt.plot(xpoints, ypoints)

# 图二
plt.subplot(1, 2, 2)
plt.plot(xpoints, ypoints2)

plt.show()

9、散点图

python 复制代码
# 创建散点图
plt.scatter(xpoints, ypoints)
plt.show()

10、柱状图

python 复制代码
# 创建柱状图
x = np.array(["A", "B", "C", "D", "E", "F"])
y = np.array([78, 13, 44, 99, 150, 8])
plt.bar(x, y)
plt.show()

11、直方图

python 复制代码
# 创建直方图
x = np.random.normal(170, 10, 250)
plt.hist(x)
plt.show()

12、饼图

python 复制代码
# 创建饼图
y = np.array([20, 20, 45, 15])
plt.pie(y)
plt.show()

五、示例展示

例子一:自定义颜色和样式的多线图

python 复制代码
import matplotlib.pyplot as plt
import numpy as np

# 生成数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# 绘制多线图
plt.plot(x, y1, label='sin(x)', color='blue', linestyle='-', linewidth=2)
plt.plot(x, y2, label='cos(x)', color='red', linestyle='--', linewidth=2)

# 添加图例
plt.legend()

# 添加标题和轴标签
plt.title('Sin and Cos Waves')
plt.xlabel('x')
plt.ylabel('y')

# 显示网格
plt.grid(True)

# 显示图表
plt.show()

例子二:带有颜色图的散点图

python 复制代码
import matplotlib.pyplot as plt
import numpy as np

# 生成数据
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)
sizes = 1000 * np.random.rand(50)

# 绘制散点图
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis')

# 添加颜色条
plt.colorbar()

# 添加标题和轴标签
plt.title('Scatter Plot with Colormap')
plt.xlabel('x')
plt.ylabel('y')

# 显示图表
plt.show()

六、总结

Matplotlib作为Python中强大的绘图库,不仅能够绘制基本的图形,还支持高度的定制化和复杂的图表类型。通过本文的介绍,希望你能对Matplotlib有一个全面的了解,并能够灵活运用于你的项目中。绘图是一个探索数据、传达信息的重要手段,掌握Matplotlib将大大增强你的数据分析和可视化能力。


版权声明:本博客内容为原创,转载请保留原文链接及作者信息。

参考文章:

相关推荐
cpolar技术支持4 小时前
端口被谁占了、这台机上开了哪些监听?本机起一个端口占用只读清单,cpolar 给异地同事远程看谁在监听
运维·python·内网穿透·cpolar·端口占用
fensnote4 小时前
西门子 S7 PLC 内部寄存器读写 Demo(C 语言)
c语言·开发语言
WeiXin_DZbishe4 小时前
基于springboot装修风格选择系统的设计与开发-计算机毕设【课程设计】77964
java·vue.js·spring boot·后端·python·php·课程设计
XZ-0700014 小时前
week9-三元表达式
java·前端·python
峰哥AI手记5 小时前
金融合规问答助手-RAG查询路由落地
python·aigc
benchmark_cc5 小时前
量化项目启动时,应该先设计数据接口还是先编写策略?从系统架构角度做出正确选择
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
CaptainT5 小时前
第 4 卷:面向对象、数据建模与类型系统
python
2601_962885725 小时前
如何用 Python 判断一只股票“追涨”还是“抄底”更有效?
android·开发语言·python
denlaku5 小时前
JDK 15 新特性详解
java·开发语言
用户7783366132115 小时前
选 SERP API 别靠感觉:用 SerpBase 实测的评估脚本与判定标准
python·api