python Matplotlib模块的使用
作者:文渊 时间:2022-12-26 21:18:37
一、Matplotlib简介与安装
Matplotlib也就是Matrix Plot Library,顾名思义,是Python的绘图库。它可与NumPy一起使用,提供了一种有效的MATLAB开源替代方案。它也可以和图形工具包一起使用,如PyQt和wxPython。
安装方式:执行命令 pip install matplotlib
一般常用的是它的子包PyPlot,提供类似MATLAB的绘图框架。
二、使用方法
1.绘制一条直线 y = 3 * x + 4,其中 x 在(-2, 2),取100个点平均分布
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
# 创建数据
x = np.linspace(-2, 2, 100)
y = 3 * x + 4
# 创建图像
plt.plot(x, y)
# 显示图像
plt.show()
2.在一张图里绘制多个子图
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
"""
多个子图
"""
# 为了能够复现
np.random.seed(1)
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))
plt.figure(1)
# linear
# 使用.subplot()方法创建子图,221表示2行2列第1个位置
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)
# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)
# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('symlog')
plt.grid(True)
# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)
plt.gca().yaxis.set_minor_formatter(NullFormatter())
plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25,
wspace=0.35)
plt.show()
3.绘制一个碗状的3D图形,着色使用彩虹色
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
"""
碗状图形
"""
fig = plt.figure(figsize=(8, 5))
ax1 = Axes3D(fig)
alpha = 0.8
r = np.linspace(-alpha, alpha, 100)
X, Y = np.meshgrid(r, r)
l = 1. / (1 + np.exp(-(X ** 2 + Y ** 2)))
ax1.plot_wireframe(X, Y, l)
ax1.plot_surface(X, Y, l, cmap=plt.get_cmap("rainbow")) # 彩虹配色
ax1.set_title("Bowl shape")
plt.show()
4.更多用法
参见官网文档
来源:https://www.wenyuanblog.com/blogs/python-matplotlib.html
标签:python,Matplotlib,模块
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用Python实现分别输出每个数组
2021-10-30 00:28:49
PHP lcfirst()函数定义与用法
2023-06-05 01:25:20
![](https://img.aspxhome.com/file/2023/9/55379_0s.png)
用Python实现写倒序输出(任意位数)
2023-06-12 17:46:33
Python安装.whl文件流程以及问题解决方法
2022-10-24 15:15:23
![](https://img.aspxhome.com/file/2023/5/72805_0s.png)
利用Python编写的实用运维脚本分享
2022-07-15 21:32:46
wxPython之解决闪烁的问题
2022-05-12 13:21:30
![](https://img.aspxhome.com/file/2023/1/86361_0s.jpg)
详解python之配置日志的几种方式
2022-11-02 14:32:30
JavaScript使用math.js进行精确计算操作示例
2024-04-10 10:54:41
scrapy-redis分布式爬虫的搭建过程(理论篇)
2022-04-25 20:49:25
![](https://img.aspxhome.com/file/2023/5/76105_0s.jpg)
SQL Server 2008 阻止保存要求重新创建表的更改问题的设置方法
2024-01-14 13:53:03
![](https://img.aspxhome.com/file/2023/4/129324_0s.jpg)
MySql5.6使用validate password 插件加强密码强度的安装及使用方法
2024-01-24 15:39:20
Python对比校验神器deepdiff库使用详解
2023-05-14 11:05:35
在pycharm上mongodb配置及可视化设置方法
2022-12-04 07:05:31
![](https://img.aspxhome.com/file/2023/5/125045_0s.jpg)
MySQL 利用frm文件和ibd文件恢复表数据
2024-01-28 08:46:53
python基本语法练习实例
2021-02-25 06:50:07
python中使用enumerate函数遍历元素实例
2021-05-08 04:56:41
Javascript中Eval函数的使用
2024-03-24 19:55:23
Python可变与不可变数据和深拷贝与浅拷贝
2022-06-05 21:14:51
Win2008中安装的MSSQL2005后无法访问的解决方法
2024-01-21 03:57:57
![](https://img.aspxhome.com/file/2023/4/113544_0s.jpg)
sql server 编译与重编译详解
2024-01-14 11:02:59