Hi all,

I wanted to plot complex valued functions with real support:
 
  f : R -> C

The plot should show the absulote value abs(f)^2 and the phase angle(f).
The demand for such a plot type originates from quantum mechanics
where I want to plot |phi> and/or <phi|phi> in this way showing
the phase of the wave function.

For this task I wrote a little script and it works quite well.
But I have a question regarding the color coding of the phase.

Is there a better way to provide the rgb_colors to the line collection?

Any hints to make the script more efficient would help.
I need to run it on hundreds of frames to produce animations.


BTW: You can add this to your gallery page if you want.
Maybe it's helpful to other people. 


-- Raoul
from numpy import pi, empty, array
from matplotlib.colors import hsv_to_rgb
from matplotlib.collections import LineCollection
from matplotlib.pyplot import gca, plot


def phaseplot(grid, phase, modulus, label="", color="k", alpha=1.0, linestyle="solid", linewidth=1, marker="None"):
    """Plot the modulus complex valued function $f:R -> C$ together with its phase in a color coded fashion.
    @param grid: The grid nodes of the real domain R
    @param phase: The phase of the complex domain result f(grid)
    @param modulus: The modulus of the complex domain result f(grid)
    """
    # Color mapping
    hsv_colors = empty((1, len(grid), 3))
    hsv_colors[:, :, 0] = 0.5*(pi+phase)/pi
    hsv_colors[:, :, 1] = 1.0
    hsv_colors[:, :, 2] = 1.0
    rgb_colors = hsv_to_rgb(hsv_colors)

    # Put all the vertical line into a collection
    segments = [ array([[node,0], [node,value]]) for node, value in zip(grid, modulus) ]
    line_segments = LineCollection(segments)

    # Set some properties of the lines
    rgb_colors = line_segments.to_rgba(rgb_colors)
    line_segments.set_color(rgb_colors[0])
    line_segments.set_linestyle(linestyle)
    line_segments.set_linewidth(linewidth)

    # Plot to the current axis instance
    ax = gca()
    ax.add_collection(line_segments)
    ax.plot(grid, modulus, color=color, linestyle=linestyle, linewidth=linewidth, alpha=alpha)




if __name__=='__main__':
    import numpy as np
    from pylab import *

    x = np.r_[-1.:1.:1j*2**12]
    u = np.exp(-x**2)*(np.cos(10*x) + 1j *np.sin(10*x))

    rvals = np.real(u)
    ivals = np.imag(u)
    cvals = np.conjugate(u)*u
    angles = np.angle(u)

    figure(figsize=(20,20))
    subplot(2,2,1)
    phaseplot(x, angles, rvals)
    xlabel(r"$\Re \psi$")
    subplot(2,2,2)
    phaseplot(x, angles, ivals)
    xlabel(r"$\Im \psi$")
    subplot(2,2,3)
    phaseplot(x, angles, cvals)
    xlabel(r"$|\psi|^2$")
    
    savefig("phaseplot.png")
    #savefig("phaseplot.eps")

    show()
------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to