2017-02-13 12 views
1

私は、matplotlibを使ってプロットした3Dグラフを持っています。3Dマットプロットグラフの画像投影角度を制御

3Dプロットを特定の角度で投影したいですが、 axis.view_init(elevation_angle、azimuthal_angle)は、私が望む角度をカバーしていません。これは、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のペイントで、回転と軸を示す行を追加した下の図に示すグラフを生成します。

赤は、Y軸を中心に回転する方位角の角度制御を示します。

青は、X軸を中心に回転する仰角を示します。

黒は、私が必要とする回転を示しています。これは、Z軸を中心に回転しますが、axis.view_init(elevation_angle、azimuth_angle)では使用できません。 enter image description here

どのように私がこのローテーションを達成できるかについての助けや洞察力はありますか? ありがとう

答えて

1

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() 
関連する問題