2012-11-05 92 views
5

我想繪製脈衝傳播的方式在每一步,它繪製脈衝形狀。換句話說,我想要一系列x-z圖,對於y的每個值。像這樣的東西(沒有顏色): Pulse propagationMatplotlib在3d脈衝傳播

我該如何使用matplotlib(或Mayavi)來做到這一點?下面是我做的,到目前爲止:

def drawPropagation(beta2, C, z): 
    """ beta2 in ps/km 
     C is chirp 
     z is an array of z positions """ 
    T = numpy.linspace(-10, 10, 100) 
    sx = T.size 
    sy = z.size 

    T = numpy.tile(T, (sy, 1)) 
    z = numpy.tile(z, (sx, 1)).T 

    U = 1/numpy.sqrt(1 - 1j*beta2*z * (1 + 1j * C)) * numpy.exp(- 0.5 * (1 + 1j * C) * T * T/(1 - 1j*beta2*z*(1 + 1j*C))) 

    fig = pyplot.figure() 
    ax = fig.add_subplot(1,1,1, projection='3d') 
    surf = ax.plot_wireframe(T, z, abs(U)) 
+1

下面是從文檔的例子:http://matplotlib.org/examples/mplot3d/bars3d_demo.html –

回答

8

更改爲:

ax.plot_wireframe(T, z, abs(U), cstride=1000) 

,並呼籲:

drawPropagation(1.0, 1.0, numpy.linspace(-2, 2, 10)) 

將創建以下圖表:

enter image description here

如果您需要的曲線被填充白色:

import numpy 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import pyplot 
from matplotlib.collections import PolyCollection 

def drawPropagation(beta2, C, z): 
    """ beta2 in ps/km 
     C is chirp 
     z is an array of z positions """ 
    T = numpy.linspace(-10, 10, 100) 
    sx = T.size 
    sy = z.size 

    T = numpy.tile(T, (sy, 1)) 
    z = numpy.tile(z, (sx, 1)).T 

    U = 1/numpy.sqrt(1 - 1j*beta2*z * (1 + 1j * C)) * numpy.exp(- 0.5 * (1 + 1j * C) * T * T/(1 - 1j*beta2*z*(1 + 1j*C))) 

    fig = pyplot.figure() 
    ax = fig.add_subplot(1,1,1, projection='3d') 
    U = numpy.abs(U) 

    verts = [] 
    for i in xrange(T.shape[0]): 
     verts.append(zip(T[i, :], U[i, :])) 

    poly = PolyCollection(verts, facecolors=(1,1,1,1), edgecolors=(0,0,1,1)) 
    ax.add_collection3d(poly, zs=z[:, 0], zdir='y') 
    ax.set_xlim3d(numpy.min(T), numpy.max(T)) 
    ax.set_ylim3d(numpy.min(z), numpy.max(z)) 
    ax.set_zlim3d(numpy.min(U), numpy.max(U)) 

drawPropagation(1.0, 1.0, numpy.linspace(-2, 2, 10)) 
pyplot.show() 

enter image description here