2013-10-31 73 views
0

我有一個代碼,我根據變量的值繪製一些箭頭,但有時這些值很大,比如9000,有時這些值比較小,例如400.所以,我怎麼能做的比例尺對數座標軸對應於箭頭的值而變化。把對數尺寸

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 
import wave 
import sys 
import matplotlib as mpl 


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

font = {'family' : 'broadway', 
     'color' : 'blue', 
     'weight' : 'normal', 
     'size' : 16, 
     'horizontalalignment': 'right', 
     } 

#dibujar cubo 
r = [-10000, 10000] 
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: " 

a1 = 120 
b1 = -410 
c1 = 200 
d1 = -652 
e1 = 800 
f1 = -500 

a1=0.3 
b1=-1 
c1=0.5 
d1=-0.6 
e1=0.2 
f1=-0.4 

a = Arrow3D([0,0],[0,a1],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="k") 
b = Arrow3D([0,b1],[0,0],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="r") 
c = Arrow3D([0,0],[0,0],[0,c1], mutation_scale=20, lw=1, arrowstyle="-|>", color="b") 
d = Arrow3D([0,0],[0,0],[0,d1], mutation_scale=20, lw=1, arrowstyle="-|>", color="g") 
e = Arrow3D([0,e1],[0,0],[0,0], mutation_scale=20, lw=1, arrowstyle="-|>", color="c") 
f = Arrow3D([0,0],[0,f1],[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) 

ax.set_xlabel("Amplitud en X", size = 18, color = 'r', family = 'verdana') 
ax.set_ylabel("Amplitud en Y", size = 18, color = 'r', family = 'verdana') 
ax.set_zlabel("Amplitud en Z", size = 18, color = 'r', family = 'verdana') 
ax.text2D(0.05, 0.95, "Intensimetro de Audio", fontsize=16, transform=ax.transAxes) 
#plt.title('Intensimetro de Audio',fontdict=font) 

lista = [a1,f1] 
list.sort(lista, key=abs, reverse=True) 
print lista 

lista1 = [b1,e1] 
list.sort(lista1, key=abs, reverse=True) 
print lista1 

lista2 = [c1,d1] 
list.sort(lista2, key=abs, reverse=True) 
print lista2 

if abs(b1)>abs(e1): 
    h=b1 
else: 
    h=e1 
if abs(a1)>abs(f1): 
    j=a1 
else: 
    j=f1 
if abs(c1)>abs(d1): 
    k=c1 
else: 
    k=d1 

a = [0, 0, 0] 
b = [h, j, k] 
xs,ys,zs = zip(a,b) 
print (xs,ys,zs) 

res = Arrow3D(xs, ys, zs, mutation_scale=20, lw=1, arrowstyle="simple", color='y') 
ax.add_artist(res) 

plt.show() 

我把所有的代碼只是爲了確定我必須改變比例尺來記錄它。

謝謝!

回答

2

使用軸方法set_xscaleset_yscale

ax.set_xscale('log') 
+0

是的,我嘗試過,但它給了我一個錯誤:「MaskError:不能蒙面元素轉換爲Python INT」。有一件事情,當我定義立方體時,我把尺寸知道最高箭頭的值,所以我很麻煩!我如何做到這一點靈活? –

+0

你有沒有想過只要將'log10'應用於箭頭的值呢?你將不得不抵消他們,使他們都是積極的,但規模仍然是日誌。 – mattexx