gpt4 book ai didi

python - 控制 3D matplotlib 图中图像投影的角度

转载 作者:行者123 更新时间:2023-12-01 03:18:40 28 4
gpt4 key购买 nike

我有一个使用 matplotlib 绘制的 3D 图形。

我想让 3D 绘图以特定角度投影,但使用axis.view_init(elevation_angle, azimuthal_angle) 没有覆盖我想要的角度,这是因为 3 个维度中存在 3 个可能的旋转平面,并且您只能在 matplotlib 中指定 2 个角度。

这是一个最小的工作示例。

import numpy,matplotlib,scipy
from matplotlib import pyplot
from scipy import constants
from mpl_toolkits.mplot3d.axes3d import Axes3D

pi=scipy.constants.pi

w= numpy.arange(0,5000,1)


def lorentzian(x,center,width,time,tau):
return 1.0/((1+((x-center)/width)**2)*width*pi)*numpy.exp(-time/tau)

centers= [1000.0,2000.0,4000.0]
widths = [100.0,300.0,50.0]

taus=numpy.zeros(len(widths))

for i in xrange(0,len(widths),1):
taus[i] = 1.0/widths[i]

time = numpy.array([0,0.019,0.033])
B = numpy.zeros((3,len(w)))
for z in xrange(0,len(time),1):
a= numpy.zeros(len(w))
for i in xrange(0,len(centers),1):
a = a + lorentzian(w,centers[i],widths[i],time[z],taus[i])

B[z] = a

ThreeD_spectrum_figure = matplotlib.pyplot.figure()
ThreeD_spectrum_axis = ThreeD_spectrum_figure.add_subplot(111, projection="3d")

colour = ["purple","green","orange"]
for i in xrange(0,len(time),1):
ThreeD_spectrum_axis.plot(w, B[i],time[i],color=colour[i],linewidth=3)

ThreeD_spectrum_axis.view_init( 135,-90)
matplotlib.pyplot.show()

这将生成如下图所示的图表,我在 ms Paint 中匆忙添加了线条来显示旋转和轴。

红色表示方位角控制,绕Y轴旋转。

蓝色显示围绕 X 轴旋转的仰角。

黑色显示了我需要的旋转,它将围绕 Z 轴旋转,这是使用 axis.view_init(elevation_angle, azimuth_angle) 不可能实现的。 enter image description here

任何人都可以提供任何帮助或见解来帮助我实现这种轮换吗?谢谢

最佳答案

仅使用方位角即可实现绕 z 轴的旋转。

enter image description here

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

pi=np.pi
w= np.arange(0,5000,1)

def lorentzian(x,center,width,time,tau):
return 1.0/((1+((x-center)/width)**2)*width*pi)*np.exp(-time/tau)

centers= [1000.0,2000.0,4000.0]
widths = [100.0,300.0,50.0]

taus=np.zeros(len(widths))

for i in xrange(0,len(widths),1):
taus[i] = 1.0/widths[i]

time = np.array([0,0.019,0.033])
B = np.zeros((3,len(w)))
for z in xrange(0,len(time),1):
a= np.zeros(len(w))
for i in xrange(0,len(centers),1):
a = a + lorentzian(w,centers[i],widths[i],time[z],taus[i])
B[z] = a

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")

colour = ["purple","green","orange"]
for i in xrange(len(time)-1,-1,-1):
ax.plot(time[i]*np.ones_like(w), w ,B[i]*np.ones_like(w),color=colour[i],linewidth=3)

ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")

#(elev, azim)
ax.view_init(45,0)

phi = np.linspace(0, 2*np.pi)

def update(phi):
ax.view_init(45, phi*180./np.pi)

ani = matplotlib.animation.FuncAnimation(fig, update, frames=phi)
ani.save(__file__+".gif", writer='imagemagick', fps=10)
plt.show()

关于python - 控制 3D matplotlib 图中图像投影的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42207083/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com