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将大大增强你的数据分析和可视化能力。


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

参考文章

相关推荐
Juchecar26 分钟前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805136 分钟前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_37 分钟前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机7 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机8 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机8 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机8 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i9 小时前
drf初步梳理
python·django
每日AI新事件9 小时前
python的异步函数
python
这里有鱼汤10 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python