matplotlib中的球坐标图 [英] Spherical coordinates plot in matplotlib

查看:363
本文介绍了matplotlib中的球坐标图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R(teta,phi)= cos(phi ^ 2),teta [0,2 * pi],phi [0,pi]

R(teta, phi) = cos(phi^2), teta[0, 2*pi], phi[0,pi]

如何借助matplotlib在球形坐标中绘制此函数(R(teta,phi))的图形? 我还没有找到球形坐标的文档.

How to draw a graph of this function (R(teta, phi)) in spherical coordinates with the help of matplotlib? The documentation I have not found Spherical coordinates.

推荐答案

下面的代码与

The code below is very much like the 3D polar plot from the Matplotlib gallery. The only difference is that you use np.meshgrid to make 2D arrays for PHI and THETA instead of R and THETA (or what the 3D polar plot example calls P).

这个故事的寓意是,只要XYZ可以表示为两个参数的(平滑)函数,plot_surface可以对其进行绘制.

The moral of the story is that as long as X, Y, and Z can be expressed as (smooth) functions of two parameters, plot_surface can plot it.

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

theta, phi = np.linspace(0, 2 * np.pi, 40), np.linspace(0, np.pi, 40)
THETA, PHI = np.meshgrid(theta, phi)
R = np.cos(PHI**2)
X = R * np.sin(PHI) * np.cos(THETA)
Y = R * np.sin(PHI) * np.sin(THETA)
Z = R * np.cos(PHI)
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
plot = ax.plot_surface(
    X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('jet'),
    linewidth=0, antialiased=False, alpha=0.5)

plt.show()

收益

通常,半径R应该为正,因此您可能需要

Typically R, the radius, should be positive, so you might want

R = np.abs(np.cos(PHI**2))

在这种情况下,

import matplotlib.colors as mcolors
cmap = plt.get_cmap('jet')
norm = mcolors.Normalize(vmin=Z.min(), vmax=Z.max())
plot = ax.plot_surface(
    X, Y, Z, rstride=1, cstride=1, 
    facecolors=cmap(norm(Z)),
    linewidth=0, antialiased=False, alpha=0.5)

收益

谁知道R = np.abs(np.cos(PHI**2))是穿裙子的小女孩? :)

Who knew R = np.abs(np.cos(PHI**2)) is a little girl in a dress? :)

这篇关于matplotlib中的球坐标图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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