Hi Guy,
I am also interested in the answer to this. The cplot function in the
mpmath module does exactly this using matplotlib, but very
inefficiently, as it computes the colour of each pixel in the image in
hls colour-space and generates the corresponding rgb value directly. I
suspect this is how it has to be done, as colormaps in matplotlib are 1D
sequences and the black-white (lightness) value is really another
dimension. However mpmath's method can be improved by doing the mapping
using array operations instead of computing it for each pixel.
I've attached a function I wrote to reproduce the Sage cplot command in
my own work. It's a bit old and can be improved. It takes the Arg and
Abs of a complex array as the first two arguments - you can easily
change this to compute these inside the function if you prefer. The line
np.vectorize(hls_to_rgb) can be replaced - recent versions of matplotlib
have a vectorized function called hsv_to_rgb() inside colors.py - so you
replace the return line with the commented-out version if you first
import hsv_to_rgb from colors.
I hope this helps.
I'm also curious: the plots you point to also show plots of the function
extrema, which are the phase singularities - does mathematica have a
function that gives you these, or did you write your own function to
find them?
regards,
Gary
Guy Rutenberg wrote:
Hi,
Is there a way to generate colormaps for complex-valued functions using
matplotlib? The type of plots I'm looking for are like the plots in:
http://commons.wikimedia.org/wiki/User:Jan_Homann/Mathematics
Thanks in advance,
Guy
def cplot_like(ph, intens=None, int_exponent=1.0, s=1.0, l_bias=1.0, drape=0,
is_like_mpmath=False):
'''
Implements the mpmath cplot-like default_color_function
The combined image is generated in hls colourspace then transformed to rgb
*phase*
A filename or 2D n x m array containing phase data in the range -pi-pi
*intens*
If None, set to 1.0
A filename or 2D n x m array containing intensity or amplitude data in
the range 0-max
*int_exponent*
Default 1.0 applies the intens mask directly to the hls
lightness-channel
0.6 works well when drape==0
*s*
saturation. Defaults to 1.0. mpmath uses 0.8.
*l_bias*
biases the mean lightness value away from 0.5. mpmath uses 1.0.
Examples are: l_bias=2 - mean=0.33 (ie darker), l_bias=0.5 -
mean=0.66 (lighter)
*drape*
If 1, drapes a structured maximum filter of size drape x drape over
the intensity data
*is_like_mpmath*
If True, sets int_exponent = 0.3, s = 0.8
'''
from colorsys import hls_to_rgb
if type(ph) is str:
cph = plt.imread(ph)/256.*2*pi-pi # -pi-pi
if len(cph.shape) == 3: cph = cph[...,0] # if ph is RGB or RGBA,
extract the R-plane
else:
cph = ph.copy()
if intens is None:
cintens = np.ones_like(cph)
elif type(intens) is str:
cintens = plt.imread(intens)/255. # 0-1
if len(cintens.shape) == 3: cintens = cintens[...,0] # if intens is
RGB or RGBA, extract the R-plane
else:
cintens = intens.copy()
cintens /= cintens.max() # autoscale intensity data
to 0-1
if drape 1:
# envelope the intensity
cintens = maximum_filter(cintens, size=drape)
h = ((cph + pi) / (2*pi)) % 1.0
if is_like_mpmath:
# apply mpmath values
int_exponent = 0.3
s = 0.8
l = 1.0 - l_bias/(l_bias+cintens**int_exponent)
v_hls_to_rgb = np.vectorize(hls_to_rgb)
#~ return hsv_to_rgb(dstack((h,np.ones_like(h),l)))
return dstack(v_hls_to_rgb(h,l,s))--
Download Intel#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev___
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users