python - 如何绘制条件为 (r < 1.5*rs) 的数组?

标签 python matplotlib plot

我正在对绕黑洞运行的光子进行编码。当光子直接进入黑洞时,由于被零除,其轨迹非常奇怪。我想忽略 r < 1.5*rs 数组中的光子,但我不知道如何实现

我尝试过使用 while True 和 if,但没有成功

h0=[t0, r2, theta, phi2, pt2, pr2, ptheta, pphi2] 

T = np.linspace(0, 1000, 9000)

zz=odeint(func, h0, T, args=(rs,))

r22 = zz[:, 1]
theta22 = zz[:, 2]
phi22 = zz[:, 3]
pt22 = zz[:, 4]
pr22 = zz[:, 5]
pphi22 = zz[:, 7]

def sph2cart(r, phi, theta):
    X = r * np.cos(phi) * np.sin(theta)
    Y = r * np.sin(phi) * np.sin(theta)
    Z = r * np.cos(theta)
    return(X, Y, Z)

X2, Y2, Z2 = sph2cart(r22, phi22, theta22)

plt.plot(X2, Y2, Z2, 'g')

我认为你真的不需要代码来帮助我,但是有人知道如何绘制半径 r < 1.5*rs 的 X2、Y2、Z2 (rs 在代码中定义)吗?

最佳答案

Numpy 内置了运行良好的掩码

r=np.arange(20)
r_mask = np.ma.masked_where(r < 10 , r)

所以,因为你只想在 r<1.5*rs 时生成 X、Y 和 Z 值(我认为你在某一点上以相反的方式说过,但在任何情况下你只需要翻转符号),您的代码可能如下所示

import numpy as np
h0=[t0, r2, theta, phi2, pt2, pr2, ptheta, pphi2] 

T = np.linspace(0, 1000, 9000)

zz=odeint(func, h0, T, args=(rs,))

r22 = zz[:, 1]
theta22 = zz[:, 2]
phi22 = zz[:, 3]
pt22 = zz[:, 4]
pr22 = zz[:, 5]
pphi22 = zz[:, 7]

def sph2cart(r, phi, theta):
    X = r * np.cos(phi) * np.sin(theta)
    Y = r * np.sin(phi) * np.sin(theta)
    Z = r * np.cos(theta)
    return(X, Y, Z)

r22_masked = np.ma.masked_where(r22 > 1.5*rs , r22)

X2, Y2, Z2 = sph2cart(r22_masked, phi22, theta22)

这只会为 r22 的未屏蔽值生成 X2、Y2 和 Z2 值(即 r22 小于 1.5*rs)

关于python - 如何绘制条件为 (r < 1.5*rs) 的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57089322/

相关文章:

python - 索引错误: list index out of range - when plotting images

python - Scipy 排名数据从高到低反转

python - Python 中的 XGBoost XGBClassifier 默认值

Python:Matplotlib:如何在其中打印不同大小的一个文本?

python - 无法修改 numpy 数组

python - Matplotlib 散点图,其中每个 x 的 y 值数组

plot - 具有条件格式的gnuplot平滑频率

python - 如何根据 pyplot 极坐标图的平均值为其着色

python - 单元测试遍历循环的方法

python - 在 Python 中使用契约式设计