基于python,Matplotlib绘制函数的等高线与三维图像
作者:夏小悠?? 时间:2021-09-06 08:51:33
本篇文章记录一下函数的等高线及其三维图像的可视化方法。
本例绘制的函数为:
1. 网格点
  在绘制曲线之前,先了解一下网格点的绘制。比如绘制一个3x3的网格,那么就需要9个坐标点:
(0,2)-----(1,2)-----(2,2)
(0,1)-----(1,1)-----(2,1)
(0,0)-----(1,0)-----(2,0)
  将其x轴和y轴坐标分开表示:
# x轴:
[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]]
# y轴:
[[0, 0, 0],
[1, 1, 1],
[2, 2, 2]]
  在numpy中可以使用np.meshgrid()来生成网格点:
import numpy as np
import matplotlib.pyplot as plt
# 10x10
x = np.linspace(-1.5, 1.5, num=10)
y = np.linspace(-1.5, 1.5, num=10)
# generate grid
X, Y = np.meshgrid(x, y)
plt.plot(X, Y, marker='.', linestyle='')
plt.grid(True)
plt.show()
2. 等高线
  绘制等高线需要的数据有点的坐标位置(x, y)以及坐标的高度z,高度z就是将坐标点(x, y)带入函数f ( x , y ) f(x, y)f(x,y)中计算得到的,在matplotlib
中可以使用plt.contour()来绘制:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1.5, 1.5, num=100)
y = np.linspace(-1.5, 1.5, num=100)
X, Y = np.meshgrid(x, y)
f = X * np.exp(-X**2 - Y**2)
fig = plt.figure()
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)
# draw
ax = plt.contour(X, Y, f, levels=10, cmap=plt.cm.cool)
# add label
plt.clabel(ax, inline=True)
# plt.savefig('img1.png')
plt.show()
 
# add color
plt.contourf(X, Y, f, levels=10, cmap=plt.cm.cool)
# plt.colorbar()
# plt.savefig('img1.png')
plt.show()
 更多的api参数请参考官方文档。
3. 三维图像
  函数的三维图像的绘制需要的数据与等高线一样,即坐标位置(x, y)以及坐标的高度z,在matplotlib中可以使用mpl_toolkits.mplot3d来绘制:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(-1.5, 1.5, num=100)
y = np.linspace(-1.5, 1.5, num=100)
X, Y = np.meshgrid(x, y)
f = X * np.exp(-X**2 - Y**2)
fig = plt.figure()
ax = Axes3D(fig)
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)
ax.plot_surface(X, Y, f, cmap=plt.cm.cool)
# plt.savefig('img1.png')
plt.show()
  有关mpl_toolkits.mplot3d的使用可以参考官方文档;
  更多的颜色搭配可参考matplotlib的colormap官方手册。
来源:https://blog.csdn.net/qq_42730750/article/details/122564379
标签:Matplotlib,绘制,函数,三维图像
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 监测内存和cpu的使用率实例
2022-07-03 23:49:49
使用python实现希尔、计数、基数基础排序的代码
2023-07-12 09:02:24
![](https://img.aspxhome.com/file/2023/9/65129_0s.gif)
一段ASP单页显示文件夹下所有图片的代码
2011-02-28 11:23:00
tensorflow 2.1.0 安装与实战教程(CASIA FACE v5)
2022-06-12 08:41:23
![](https://img.aspxhome.com/file/2023/8/92208_0s.jpg)
Python中导入模块的几种方式总结
2023-08-04 17:03:54
![](https://img.aspxhome.com/file/2023/2/61432_0s.png)
Python字符串、元组、列表、字典互相转换的方法
2022-10-28 20:25:51
9个提高 Python 编程的小技巧
2022-01-20 16:53:28
PyQt5固定窗口大小的方法
2021-05-18 12:34:43
![](https://img.aspxhome.com/file/2023/0/99040_0s.jpg)
使用Python爬取弹出窗口信息的实例
2021-08-27 19:11:25
![](https://img.aspxhome.com/file/2023/8/100858_0s.jpg)
oracle 11g的警告日志和监听日志的删除方法
2023-06-29 13:59:36
Python连接Mssql基础教程之Python库pymssql
2022-01-24 04:47:20
Python详解argparse参数模块之命令行参数
2021-06-20 21:16:18
Python-apply(lambda x: )的使用及说明
2022-05-11 04:31:29
Python中的函数参数传递问题
2023-09-28 05:38:12
![](https://img.aspxhome.com/file/2023/0/62290_0s.png)
Python统计列表元素出现次数的方法示例
2021-03-06 00:43:23
python读取和保存图片5种方法对比
2022-05-27 23:54:32
基于opencv和pillow实现人脸识别系统(附demo)
2023-04-06 10:28:53
![](https://img.aspxhome.com/file/2023/9/101709_0s.png)
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
2022-08-26 07:14:52
深入解答关于Python的11道基本面试题
2021-11-13 00:34:51
![](https://img.aspxhome.com/file/2023/5/66285_0s.jpg)
必备的JS调试技巧汇总
2023-08-07 06:26:50
![](https://img.aspxhome.com/file/2023/0/55930_0s.gif)