通过一个函数对比 mgrid以及meshgrid函数

举报
tsinghuazhuoqing 发表于 2022/01/11 00:39:56 2022/01/11
【摘要】 简 介: 利用3D曲面显示可以更好的将三维函数性能可视化展示。利用view_init()设置不同的视角,动态显示曲面的不同方面。 关键词: meshgrid,mgrid,view_init ...

简 介: 利用3D曲面显示可以更好的将三维函数性能可视化展示。利用view_init()设置不同的视角,动态显示曲面的不同方面。

关键词 meshgridmgridview_init

Axes3D函数曲面
文章目录
绘制Axes3D
函数曲面
利用mgrid产生x,y
利用meshgrid
产生xy
多角度的Axes3D
contour3D
总结

 

§01 Axes3D函数曲面


1.1 绘制Axes3D函数曲面

1.1.1 利用mgrid产生x,y

import sys,os,math,time
sys.path.append("/home/aistudio/external-libraries")
import matplotlib.pyplot as plt
from numpy import *

from mpl_toolkits.mplot3d import Axes3D

def fxy(x,y):
    return 3*(1-x)**2*exp(-(x**2+(y+1)**2))+\
           10*(x/5-x**3-y**5)*exp(-(x**2+y**2))-\
           exp(-((x+1)**2+y**2))/3

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
grid_num = 100
x,y = mgrid[-4:4:grid_num*1j, -2:2:grid_num*1j]

  
 
  • 1
  • 2
xx = x.flatten()
yy = y.flatten()

ff = fxy(xx,yy).reshape(x.shape)

ax = Axes3D(plt.figure(figsize=(12,8)))
ax.plot_surface(x,y,ff,
                rstride=1,
                cstride=1,
                cmap=plt.cm.Blues)
plt.show()

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

▲ 图1 mgrid产生的函数曲面

▲ 图1 mgrid产生的函数曲面

1.1.2 利用meshgrid产生xy

grid_num = 100
x = linspace(-4,4,grid_num)
y = linspace(-2,2,grid_num)
x,y = meshgrid(x,y)

  
 
  • 1
  • 2
  • 3
  • 4

▲ 图1.1.2 mgrid产生的函数曲面

▲ 图1.1.2 mgrid产生的函数曲面

1.2 多角度的Axes3D

gifpath = '/home/aistudio/GIF'
if not os.path.isdir(gifpath):
    os.makedirs(gifpath)
gifdim = os.listdir(gifpath)
for f in gifdim:
    fn = os.path.join(gifpath, f)
    if os.path.isfile(fn):
        os.remove(fn)

xx = x.flatten()
yy = y.flatten()

ff = fxy(xx,yy).reshape(x.shape)

ax = Axes3D(plt.figure(figsize=(12,8)))
ax.plot_surface(x,y,ff,
                rstride=1,
                cstride=1,
                cmap=plt.cm.Blues)

count = 0
for i in range(0, 180, 10):
    ax.view_init(30,i)
    fn = os.path.join(gifpath, '%03d.jpg'%count)
    count += 1
    plt.savefig(fn)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

▲ 图  旋转GIF

▲ 图 旋转GIF

▲ 图  旋转GIF
▲ 图 旋转GIF

▲ 图  旋转GIF
▲ 图 旋转GIF

1.3 contour3D

xx = x.flatten()
yy = y.flatten()

ff = fxy(xx,yy).reshape(x.shape)

ax = Axes3D(plt.figure(figsize=(12,8)))

ax = plt.axes(projection='3d')
ax.contour3D(x,y,ff, 100, cmap=plt.cm.hot)

count = 0
for i in tqdm(range(0, 350, 10)):
    ax.view_init(30, i)
    fn = os.path.join(gifpath, '%03d.jpg'%count)
    count += 1
    plt.savefig(fn)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

▲ 图  contour3D

▲ 图 contour3D

▲ 图  contour3D
▲ 图 contour3D

▲ 图  contour3D
▲ 图 contour3D

ax.contour3D(x,y,ff, 100, cmap=plt.cm.hot)

  
 
  • 1

▲ 图  contour3D

▲ 图 contour3D

 

结 ※


  用3D曲面显示可以更好的将三维函数性能可视化展示。利用view_init()设置不同的视角,动态显示曲面的不同方面。


● 相关图表链接:

文章来源: zhuoqing.blog.csdn.net,作者:卓晴,版权归原作者所有,如需转载,请联系作者。

原文链接:zhuoqing.blog.csdn.net/article/details/122387550

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。