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

时间:2017-02-13 14:59:12

标签: python matplotlib 3d angle

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

我想将3D绘图投影到特定角度,但使用 axis.view_init(elevation_angle,azimuthal_angle)不包括我想要的角度,这是由于3个维度中有3个可能的旋转平面,而且只能在matplotlib中指定2个角度。

这是一个最低限度的工作示例。

//Decorator Design Pattern
interface Coffee {
    public double getCost();

    public String getIngredients();
}

class Simplecoffee implements Coffee {
    public double getCost() {
        return 1;
    }

    public String getIngredients() {
        return "Coffee";
    }
}

abstract class CoffeeDecorator implements Coffee {
    protected Coffee decoratedcoffee;
    protected String ingredientseparator = ":";

    public CoffeeDecorator(Coffee decoratedcoffee) {

        this.decoratedcoffee = decoratedcoffee;
    }

    public double getCost() {
        return decoratedcoffee.getCost();
    }

    public String getIngredients() {
        return decoratedcoffee.getIngredients();
    }
}

class Milk extends CoffeeDecorator {

    public Milk(Coffee decoratedcoffee) {

        super(decoratedcoffee);
        System.out.println("Milk Constructor");
    }

    public double getCost() {
        return super.getCost() + 0.5;
    }

    public String getIngredients() {
        return super.getIngredients() + ingredientseparator + "milk";
    }
}

public class Decorator {    
    public static void main(String[] ar) {
        System.out.println("calling simplecoffee in main");
        Coffee c = new Simplecoffee();
        System.out.println("Cost:" + c.getCost());
        c = new Milk(c);
        System.out.println("Cost:" + c.getCost());
    }    
}

这将生成下图中所示的图形,在ms画面中,匆匆用ms绘制,添加线条以显示旋转和轴。

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

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

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

任何人都可以提供有关如何实现此轮换的任何帮助或见解吗? 感谢

1 个答案:

答案 0 :(得分: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()