将径向数据转换为笛卡尔网格以进行表面绘图 [英] Translating radial data to a cartesian grid for surface plot

查看:190
本文介绍了将径向数据转换为笛卡尔网格以进行表面绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据列表,其成分与2D网格上某个径向距离r处的电势相对应.数据对应于极坐标中的数据点,并且在theta分量中是对称的

import numpy as np
import matplotlib.pyplot as plt

r = np.linspace(1.0, 5.0, 99)
#Data looks like:
V = np.array([9.0,...,0.0])

x = np.linspace(-5.0,5.0,99)
y = np.linspace(-5.0,5.0,99)

xx,yy = np.meshgrid(x,y)

我想在(x,y)空间中创建数据的表面图,但是要使用matplotlib,您需要一个与每个(x,y)位置上的电势相对应的数据点网格.假设我有一组在(r,theta)空间中测量的数据,如何创建表面图?

解决方案

您将需要先将数据转换为笛卡尔坐标,并通过为每个theta重复值V来创建与网格网格形状相同的值数组

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()

ax = fig.gca(projection='3d')

r = np.linspace(1.0, 5.0, 99)
V = np.sqrt(np.sinc(r-0.5)**2) # your data here (same length as r)


theta = np.linspace(0,2*np.pi,50)
R, Theta = np.meshgrid(r,theta)
X = R * np.cos(Theta)
Y = R * np.sin(Theta)
Z = np.tile(V,(len(theta),1))

norm = plt.Normalize(vmin=Z.min(), vmax=Z.max())
ax.plot_surface(X,Y,Z, facecolors=plt.cm.RdYlGn(norm(Z)))

plt.show()

I have a list of data whose components correspond to the potential at some radial distance r on a 2D grid. The data corresponds to data points in polar coordinates and is symmetric in the theta component

import numpy as np
import matplotlib.pyplot as plt

r = np.linspace(1.0, 5.0, 99)
#Data looks like:
V = np.array([9.0,...,0.0])

x = np.linspace(-5.0,5.0,99)
y = np.linspace(-5.0,5.0,99)

xx,yy = np.meshgrid(x,y)

I would like to create a surface plot of the data in (x,y) space, however to use matplotlib you need a grid of data points corresponding to the potential at each (x,y) location. Given that I have a set of data measured in (r,theta) space, how can I create a surface plot?

解决方案

You will need to tranform your data to cartesian coordinates first and create a value array of the same shape as the meshgrid by repeating the value V for each theta.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()

ax = fig.gca(projection='3d')

r = np.linspace(1.0, 5.0, 99)
V = np.sqrt(np.sinc(r-0.5)**2) # your data here (same length as r)


theta = np.linspace(0,2*np.pi,50)
R, Theta = np.meshgrid(r,theta)
X = R * np.cos(Theta)
Y = R * np.sin(Theta)
Z = np.tile(V,(len(theta),1))

norm = plt.Normalize(vmin=Z.min(), vmax=Z.max())
ax.plot_surface(X,Y,Z, facecolors=plt.cm.RdYlGn(norm(Z)))

plt.show()

这篇关于将径向数据转换为笛卡尔网格以进行表面绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆