求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Modeler   模型库  
会员   
 


DeepSeek大模型应用开发实践
6月12-13日 厦门



基于 UML 和EA进行分析设计
6月23-24日 北京+线上



人工智能、机器学习& TensorFlow+Keras
6月22-23日 北京
 
追随技术信仰

随时听讲座
每天看新闻
 
 
Matplotlib教程
1.数据可视化是什么
2.Matplotlib是什么
3.Matplotlib下载和安装
4.Matplotlib.pyplot接口汇总
5.第一个Matplotlib绘图程序
6.PyLab绘制曲线图
7.Matplotlib figure图形对象
8.Matplotlib axes类使用详解
9.Matplotlib subplot()函数用法详解
10.Matplotlib subplots()函数详解
11.Matplotlib subplot2grid()函数详解
12.Matplotlib设置网格格式
13.Matplotlib设置坐标轴格式
14.Matplotlib设置坐标轴范围
15.Matplotlib设置刻度和标签
16.Matplotlib中文乱码解决方案
17.Matplotlib双轴图
18.Matplotlib设置刻度和标签
19.Matplotlib柱状图
20.Matplotlib直方图
21.Matplotlib饼状图
22.Matplotlib折线图
23.Python Matplotlib散点图
24.Matplotlib等高线图
25.Matplotlib振动图
26.Matplotlib箱型图
27.Matplotlib提琴图
28.Python Matplotlib 3D绘图详解
29.Matplotlib绘制文本
30.Matplotlib数学表达式
31.Matplotlib image图像处理
32.Matplotlib转换对象
 

 
目录
 
Matplotlib等高线图
来源:C语言中文网
156 次浏览
3次  

等高线图(也称“水平图”)是一种在二维平面上显示 3D 图像的方法。等高线有时也被称为 “Z 切片”,如果您想要查看因变量 Z 与自变量 X、Y 之间的函数图像变化(即 Z=f(X,Y)),那么采用等高线图最为直观。

自变量 X 和 Y 需要被限制在矩形网格内,您可以将 x 、y 数组作为参数传递给 numpy.meshgrid() 函数来构建一个网格点矩阵。

Matplotlib API 提供了绘制等高线(contour)与填充等高线( contourf)的函数。这两个函数都需要三个参数,分别是 X、Y 与 Z。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. #创建xlist、ylist数组
  4. xlist = np.linspace(-3.0, 3.0, 100)
  5. ylist = np.linspace(-3.0, 3.0, 100)
  6. #将上述数据变成网格数据形式
  7. X, Y = np.meshgrid(xlist, ylist)
  8. #定义Z与X,Y之间的关系
  9. Z = np.sqrt(X**2 + Y**2)
  10. fig,ax=plt.subplots(1,1)
  11. #填充等高线颜色
  12. cp = ax.contourf(X, Y, Z)
  13. fig.colorbar(cp) # 给图像添加颜色柱
  14. ax.set_title('Filled Contours Plot')
  15. ax.set_xlabel('x (cm)')
  16. ax.set_ylabel('y (cm)')
  17. #画等高线
  18. plt.contour(X,Y,Z)
  19. plt.show()

代码执行后,输出结果如下:

图1:等高线示例图

左侧图像绘制了两个变量 X、Y ,右侧的颜色柱(colorbar)则表示 X 的取值,颜色越深表示值越小,中间深色部分的圆心点表示 x=0,y=0,z=0。


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



156 次浏览
3次