2013-09-27 134 views
1

enter image description here我的工作n中的項目,我需要做一個立方體的頂部和側面的看法,但我不知道怎麼畫代表從頂視圖廣場上簡單的2D方形。這裏是立方體的代碼:繪製與matplotlib

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 
from itertools import product, combinations, cycle 
from numpy import sin, cos 
from matplotlib.patches import Rectangle, Circle, PathPatch 
import mpl_toolkits.mplot3d.art3d as art3d 


fig = plt.figure() 
ax = fig.gca(projection='3d') 
ax.set_aspect("auto") 
ax.set_autoscale_on(True) 


#dibujar cubo 
r = [-1, 1] 
for s, e in combinations(np.array(list(product(r,r,r))), 2): 
    if np.sum(np.abs(s-e)) == r[1]-r[0]: 
     ax.plot3D(*zip(s,e), color="b") 

#dibujar punto 
ax.scatter([0],[0],[0],color="g",s=100) 

#dibujar vector 
from matplotlib.patches import FancyArrowPatch 
from mpl_toolkits.mplot3d import proj3d 

class Arrow3D(FancyArrowPatch): 
    def __init__(self, xs, ys, zs, *args, **kwargs): 
     FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs) 
     self._verts3d = xs, ys, zs 

    def draw(self, renderer): 
     xs3d, ys3d, zs3d = self._verts3d 
     xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M) 
     self.set_positions((xs[0],ys[0]),(xs[1],ys[1])) 
     FancyArrowPatch.draw(self, renderer) 
#print "ingrese coordenada inicial: " 
m=float(input("Ingrese valor de vector: ")) 
a = Arrow3D([0,0],[0,1],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="k") 
b = Arrow3D([0,-1],[0,0],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="r") 
c = Arrow3D([0,0],[0,0],[0,1], mutation_scale=20, lw=1, arrowstyle="-|>", color="b") 
d = Arrow3D([0,0],[0,0],[0,-1], mutation_scale=20, lw=1, arrowstyle="-|>", color="g") 
e = Arrow3D([0,m],[0,0],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="c") 
f = Arrow3D([0,0],[0,-0.5],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="m") 

ax.add_artist(a) 
ax.add_artist(b) 
ax.add_artist(c) 
ax.add_artist(d) 
ax.add_artist(e) 
ax.add_artist(f) 


a = [0, 0, 0] 
b = [m, 0, 1] 
orig = [0, 0, 0] 
for (_b, _e), _c in zip([[orig, a], [orig, b], [a, b]], cycle(['m', 'r', 'g', 'b'])): 
    xs, ys, zs = zip(_b, _e) 
    res = Arrow3D(xs, ys, zs, mutation_scale=20, lw=1, arrowstyle="simple", color='y') 
    ax.add_artist(res) 

plt.show() 

所以,我怎樣才能在像示例圖像的副圖中的立方體頂視圖?

+0

你有一個例子或參考圖像,你可以帶我們去? – wflynny

+0

就像我更新的例子! –

回答

0

我認爲解決的辦法可能是作爲設置view_init一樣簡單()。

喜歡的東西:

ax.view_init(0,0) 

在你的代碼的末尾,前plt.show()爲我工作。

可以與海拔設置角度在z平面上,然後在x-y平面的方位角。

docs

view_init(海拔=無,阿齊姆=無)設定 軸線的仰角和方位角。

這可以用於以編程方式旋轉軸。

「海拔」存儲在z平面的仰角。 'azim'在x,y平面中存儲 方位角。

如果elev或azim爲None(默認值),則使用在Axes3D構造函數中指定的初始值 。

+0

這是一個好的!但我真的需要在二維的不同子圖上繪製這些視圖。 –

+0

好吧,看看子圖[文檔](http://matplotlib.org/api/pyplot_api.html?highlight=subplot#matplotlib.pyplot.subplot)。發佈您嘗試過的以及失敗的地方。 – drexiya