Hi,

I've written two plot scripts for plotting functions f: R -> C.
(Scalar mappings of a real to a complex domain.)

The function plotcf(...) plots the modulus
as y value and the phase as color code.

The function stemcf(...) does the same, but
emulates a stem plot.

I attached also two demo scripts that show these
functions in action.

If you have any questions or improvements,
please tell me.


Feel free to add these scripts to the gallery
if you think they could be of a broader
interest.


-- 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 plotcf(grid, phase, modulus, axes=None, linestylep="solid", linewidthp=1, color="k", **kwargs):
    """Plot the modulus of a 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)
    @keyword axes: The axes instance used for plotting.
    @keyword linestylep: The line style of the phase curve.
    @keyword linewidthp: The line width of the phase curve.
    @keyword color: The color of the phase curve.
    @note: Additional keyword arguments are passe to the plot function.
    """
    # 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(linestylep)
    line_segments.set_linewidth(linewidthp)

    # Plot to the given axis instance or retrieve the current one
    if axes is None:
        axes = gca()

    # Plot the phase
    axes.add_collection(line_segments)
    # Plot the modulus
    axes.plot(grid, modulus, color=color, **kwargs)
from numpy import pi, empty, array, zeros, real
from matplotlib.colors import hsv_to_rgb
from matplotlib.collections import LineCollection
from matplotlib.pyplot import gca, plot, scatter


def stemcf(grid, phase, modulus, axes=None, linestylep="solid", linewidthp=2, color=None, markerp="o", **kwargs):
    """Stemplot the modulus of a complex valued function $f:I -> 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)
    @keyword axes: The axes instance used for plotting.
    @keyword linestylep: The line style of the phase curve.
    @keyword linewidthp: The line width of the phase curve.
    @keyword color: The color of the stemmed markers.
    @keyword markerp: The shape of the stemmed markers.
    @note: Additional keyword arguments are passe to the plot function.
    """
    # 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(linestylep)
    line_segments.set_linewidth(linewidthp)

    # Plot to the given axis instance or retrieve the current one
    if axes is None:
        axes = gca()

    # Plot the phase
    axes.add_collection(line_segments)
    # Plot the modulus
    if color is None:
        # Scatter has a problem with complex data type, even if values are real
        # make sure values are purely real
        axes.scatter(grid, real(modulus), c=rgb_colors[0], **kwargs)
    else:
        axes.plot(grid, modulus, linestyle="", marker=markerp, color=color, **kwargs)
    # Plot the ground line
    axes.plot(grid, zeros(grid.shape), linestyle=linestylep, color="k", **kwargs)
import numpy as np
from matplotlib.pyplot import *

from plotcf import plotcf

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)
plotcf(x, angles, rvals)
xlabel(r"$\Re \psi$")

subplot(2,2,2)
plotcf(x, angles, ivals)
xlabel(r"$\Im \psi$")

subplot(2,2,3)
plotcf(x, angles, cvals)
xlabel(r"$|\psi|^2$")

savefig("phaseplot.png")

show()
import numpy as np
from matplotlib.pyplot import *

from stemcf import stemcf

x = np.r_[-1.:1.:1j*2**6]
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)
stemcf(x, angles, rvals)
xlabel(r"$\Re \psi$")

subplot(2,2,2)
stemcf(x, angles, ivals, color="k")
xlabel(r"$\Im \psi$")

subplot(2,2,3)
stemcf(x, angles, cvals)
xlabel(r"$|\psi|^2$")

savefig("phaseplot.png")

show()
------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to