How to shift colormap?

Like in Basemap examples:
http://matplotlib.github.com/basemap/users/examples.html (topographic
image in the middle of page) ground 0 has some yellow/orange color
making seas and oceans coasts in that same, color instead light blue
(as we'd all expect I guess)

So how to shift this particular colormap (cm.GMT_haxby) up a bit, so
that I get expected colors?

I learned some more about matplotlib colormaps from here:

http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps

and tried to grasp cmap creation workflow.

Here is GMT_haxby:

_GMT_haxby_data = {

‘blue’: [

(0.0, 0.474509805441, 0.474509805441),

(0.0322580635548, 0.588235318661, 0.588235318661),

(0.0645161271095, 0.686274528503, 0.686274528503),

(0.0967741906643, 0.784313738346, 0.784313738346),

(0.129032254219, 0.831372559071, 0.831372559071),

(0.161290317774, 0.878431379795, 0.878431379795),

(0.193548381329, 0.941176474094, 0.941176474094),

(0.225806444883, 0.972549021244, 0.972549021244),

(0.258064508438, 1.0, 1.0),

(0.290322571993, 1.0, 1.0),

(0.322580635548, 1.0, 1.0),

(0.354838699102, 0.941176474094, 0.941176474094),

(0.387096762657, 0.882352948189, 0.882352948189),

(0.419354826212, 0.784313738346, 0.784313738346),

(0.451612889767, 0.68235296011, 0.68235296011),

(0.483870953321, 0.658823549747, 0.658823549747),

(0.516129016876, 0.635294139385, 0.635294139385),

(0.548387110233, 0.552941203117, 0.552941203117),

(0.580645143986, 0.474509805441, 0.474509805441),

(0.612903237343, 0.407843142748, 0.407843142748),

(0.645161271095, 0.341176480055, 0.341176480055),

(0.677419364452, 0.270588248968, 0.270588248968),

(0.709677398205, 0.29411765933, 0.29411765933),

(0.741935491562, 0.305882364511, 0.305882364511),

(0.774193525314, 0.352941185236, 0.352941185236),

(0.806451618671, 0.486274510622, 0.486274510622),

(0.838709652424, 0.61960786581, 0.61960786581),

(0.870967745781, 0.68235296011, 0.68235296011),

(0.903225779533, 0.768627464771, 0.768627464771),

(0.93548387289, 0.843137264252, 0.843137264252),

(0.967741906643, 0.921568632126, 0.921568632126),

(1.0, 1.0, 1.0)],

‘green’: [

(0.0, 0.0, 0.0),

(0.0322580635548, 0.0, 0.0),

(0.0645161271095, 0.0196078438312, 0.0196078438312),

(0.0967741906643, 0.0392156876624, 0.0392156876624),

(0.129032254219, 0.0980392172933, 0.0980392172933),

(0.161290317774, 0.156862750649, 0.156862750649),

(0.193548381329, 0.40000000596, 0.40000000596),

(0.225806444883, 0.505882382393, 0.505882382393),

(0.258064508438, 0.686274528503, 0.686274528503),

(0.290322571993, 0.745098054409, 0.745098054409),

(0.322580635548, 0.792156875134, 0.792156875134),

(0.354838699102, 0.882352948189, 0.882352948189),

(0.387096762657, 0.921568632126, 0.921568632126),

(0.419354826212, 0.921568632126, 0.921568632126),

(0.451612889767, 0.92549020052, 0.92549020052),

(0.483870953321, 0.960784316063, 0.960784316063),

(0.516129016876, 1.0, 1.0),

(0.548387110233, 0.960784316063, 0.960784316063),

(0.580645143986, 0.92549020052, 0.92549020052),

(0.612903237343, 0.843137264252, 0.843137264252),

(0.645161271095, 0.741176486015, 0.741176486015),

(0.677419364452, 0.627451002598, 0.627451002598),

(0.709677398205, 0.458823531866, 0.458823531866),

(0.741935491562, 0.313725501299, 0.313725501299),

(0.774193525314, 0.352941185236, 0.352941185236),

(0.806451618671, 0.486274510622, 0.486274510622),

(0.838709652424, 0.61960786581, 0.61960786581),

(0.870967745781, 0.701960802078, 0.701960802078),

(0.903225779533, 0.768627464771, 0.768627464771),

(0.93548387289, 0.843137264252, 0.843137264252),

(0.967741906643, 0.921568632126, 0.921568632126),

(1.0, 1.0, 1.0)],

‘red’: [

(0.0, 0.0392156876624, 0.0392156876624),

(0.0322580635548, 0.156862750649, 0.156862750649),

(0.0645161271095, 0.0784313753247, 0.0784313753247),

(0.0967741906643, 0.0, 0.0),

(0.129032254219, 0.0, 0.0),

(0.161290317774, 0.0, 0.0),

(0.193548381329, 0.101960785687, 0.101960785687),

(0.225806444883, 0.0509803928435, 0.0509803928435),

(0.258064508438, 0.0980392172933, 0.0980392172933),

(0.290322571993, 0.196078434587, 0.196078434587),

(0.322580635548, 0.266666680574, 0.266666680574),

(0.354838699102, 0.380392163992, 0.380392163992),

(0.387096762657, 0.415686279535, 0.415686279535),

(0.419354826212, 0.486274510622, 0.486274510622),

(0.451612889767, 0.541176497936, 0.541176497936),

(0.483870953321, 0.674509823322, 0.674509823322),

(0.516129016876, 0.803921580315, 0.803921580315),

(0.548387110233, 0.874509811401, 0.874509811401),

(0.580645143986, 0.941176474094, 0.941176474094),

(0.612903237343, 0.96862745285, 0.96862745285),

(0.645161271095, 1.0, 1.0),

(0.677419364452, 1.0, 1.0),

(0.709677398205, 0.956862747669, 0.956862747669),

(0.741935491562, 0.933333337307, 0.933333337307),

(0.774193525314, 1.0, 1.0),

(0.806451618671, 1.0, 1.0),

(0.838709652424, 1.0, 1.0),

(0.870967745781, 0.960784316063, 0.960784316063),

(0.903225779533, 1.0, 1.0),

(0.93548387289, 1.0, 1.0),

(0.967741906643, 1.0, 1.0),

(1.0, 1.0, 1.0)]}

Now imagine tweaking this map by hand, i.e. lower 0 value (~ 2/3 from whole cmap in example) from orange to green-blue without ruining it totally

So I want to ask this question differently: Is there some tool (Inkscape, CorelDraw, Photoshop, … anything) that would let me use GUI with some sliders so that I can try adjust matplotlib colormap?

···

On Sun, Nov 6, 2011 at 9:58 PM, klo uo <klonuo@…287…> wrote:

Like in Basemap examples:

http://matplotlib.github.com/basemap/users/examples.html (topographic

image in the middle of page) ground 0 has some yellow/orange color

making seas and oceans coasts in that same, color instead light blue

(as we’d all expect I guess)

So how to shift this particular colormap (cm.GMT_haxby) up a bit, so

that I get expected colors?

Or are matplotlib colormaps compatible with any other programs?

···

On Fri, Nov 11, 2011 at 4:19 PM, klo uo <klonuo@…287…> wrote:

So I want to ask this question differently: Is there some tool (Inkscape, CorelDraw, Photoshop, … anything) that would let me use GUI with some sliders so that I can try adjust matplotlib colormap?

Found easy way out :slight_smile:

If anyone is interested: create gradient in Gimp, then right click on it and export to POV-Ray. Contents of this POV-Ray map is in right terminal on screenshot. See Python dictionary object on left terminal how to extract needed data and arrange it.

In the middle is result map which is identical it seems with Gimp gradient map

···

On Fri, Nov 11, 2011 at 4:21 PM, klo uo <klonuo@…83…287…> wrote:

Or are matplotlib colormaps compatible with any other programs?

On Fri, Nov 11, 2011 at 4:19 PM, klo uo <klonuo@…287…> wrote:

So I want to ask this question differently: Is there some tool (Inkscape, CorelDraw, Photoshop, … anything) that would let me use GUI with some sliders so that I can try adjust matplotlib colormap?

I have this script that uses the matplotlib Slider object to control the
colormap of a histogram. This could be very close to what you want. Here is
the script:

### begin colormap_slider.py #################################
import math, copy
import numpy
from matplotlib import pyplot, colors, cm
from matplotlib.widgets import Slider

def cmap_powerlaw_adjust(cmap, a):
    '''
    returns a new colormap based on the one given
    but adjusted via power-law:

    newcmap = oldcmap**a
    '''
    if a < 0.:
        return cmap
    cdict = copy.copy(cmap._segmentdata)
    fn = lambda x : (x[0]**a, x[1], x[2])
    for key in ('red','green','blue'):
        cdict[key] = map(fn, cdict[key])
        cdict[key].sort()
        assert (cdict[key][0]<0 or cdict[key][-1]>1), \
            "Resulting indices extend out of the [0, 1] segment."
    return colors.LinearSegmentedColormap('colormap',cdict,1024)

def cmap_center_adjust(cmap, center_ratio):
    '''
    returns a new colormap based on the one given
    but adjusted so that the old center point higher
    (>0.5) or lower (<0.5)
    '''
    if not (0. < center_ratio) & (center_ratio < 1.):
        return cmap
    a = math.log(center_ratio) / math.log(0.5)
    return cmap_powerlaw_adjust(cmap, a)

def cmap_center_point_adjust(cmap, range, center):
    '''
    converts center to a ratio between 0 and 1 of the
    range given and calls cmap_center_adjust(). returns
    a new adjusted colormap accordingly
    '''
    if not ((range[0] < center) and (center < range[1])):
        return cmap
    return cmap_center_adjust(cmap,
        abs(center - range[0]) / abs(range[1] - range[0]))

if __name__ == '__main__':
    ### create some 2D histogram-type data
    def func3(x,y):
        return (1- x/2 + x**5 + y**3)*numpy.exp(-x**2-y**2)
    x = numpy.linspace(-3.0, 3.0, 60)
    y = numpy.linspace(-3.0, 3.0, 60)
    X,Y = numpy.meshgrid(x, y)
    Z = func3(X, Y)
    extent = [x[0],x[-1],y[0],y[-1]]

    plotkwargs = {
        'extent' : extent,
        'origin' : 'lower',
        'interpolation' : 'nearest',
        'aspect' : 'auto'}

    ### interactively adjustable with a slider
    fig = pyplot.figure(figsize=(6,4))
    fig.subplots_adjust(top=0.8)
    ax = fig.add_subplot(1,1,1)
    cmap = cm.seismic
    plt = ax.imshow(Z, cmap=cmap, **plotkwargs)
    cb = fig.colorbar(plt, ax=ax)

    axcmap = fig.add_axes([0.1, 0.85, 0.8, 0.05], axisbg='white')
    scmap = Slider(axcmap, '', 0.0, 1.0, valinit=0.5)

    def update(val):
        cmapcenter = scmap.val
        plt.set_cmap(cmap_center_adjust(cmap, cmapcenter))
    scmap.on_changed(update)

    pyplot.show()
### end colormap_slider.py ###################################

···


View this message in context: http://old.nabble.com/How-to-shift-colormap--tp32792283p32827012.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Thanks Johann,

that is exactly what I asked for

I knew that matplotlib can do GUI tricks but I didn’t felt skilled to go there. Seeing you code it seems easy now, but it’s always like that after you see the solution :smiley:

Cheers

···

On Fri, Nov 11, 2011 at 6:05 PM, johanngoetz <jgoetz@…2722…> wrote:

I have this script that uses the matplotlib Slider object to control the

colormap of a histogram. This could be very close to what you want. Here is

the script:

begin colormap_slider.py

import math, copy

import numpy

from matplotlib import pyplot, colors, cm

from matplotlib.widgets import Slider

def cmap_powerlaw_adjust(cmap, a):

'''

returns a new colormap based on the one given

but adjusted via power-law:



newcmap = oldcmap**a

'''

if a < 0.:

    return cmap

cdict = copy.copy(cmap._segmentdata)

fn = lambda x : (x[0]**a, x[1], x[2])

for key in ('red','green','blue'):

    cdict[key] = map(fn, cdict[key])

    cdict[key].sort()

    assert (cdict[key][0]<0 or cdict[key][-1]>1), \

        "Resulting indices extend out of the [0, 1] segment."

return colors.LinearSegmentedColormap('colormap',cdict,1024)

def cmap_center_adjust(cmap, center_ratio):

'''

returns a new colormap based on the one given

but adjusted so that the old center point higher

(>0.5) or lower (<0.5)

'''

if not (0. < center_ratio) & (center_ratio < 1.):

    return cmap

a = math.log(center_ratio) / math.log(0.5)

return cmap_powerlaw_adjust(cmap, a)

def cmap_center_point_adjust(cmap, range, center):

'''

converts center to a ratio between 0 and 1 of the

range given and calls cmap_center_adjust(). returns

a new adjusted colormap accordingly

'''

if not ((range[0] < center) and (center < range[1])):

    return cmap

return cmap_center_adjust(cmap,

    abs(center - range[0]) / abs(range[1] - range[0]))

if name == ‘main’:

### create some 2D histogram-type data

def func3(x,y):

    return (1- x/2 + x**5 + y**3)*numpy.exp(-x**2-y**2)

x = numpy.linspace(-3.0, 3.0, 60)

y = numpy.linspace(-3.0, 3.0, 60)

X,Y = numpy.meshgrid(x, y)

Z = func3(X, Y)

extent = [x[0],x[-1],y[0],y[-1]]





plotkwargs = {

    'extent' : extent,

    'origin' : 'lower',

    'interpolation' : 'nearest',

    'aspect' : 'auto'}



### interactively adjustable with a slider

fig = pyplot.figure(figsize=(6,4))

fig.subplots_adjust(top=0.8)

ax = fig.add_subplot(1,1,1)

cmap = cm.seismic

plt = ax.imshow(Z, cmap=cmap, **plotkwargs)

cb = fig.colorbar(plt, ax=ax)



axcmap = fig.add_axes([0.1, 0.85, 0.8, 0.05], axisbg='white')

scmap = Slider(axcmap, '', 0.0, 1.0, valinit=0.5)



def update(val):

    cmapcenter = scmap.val

    plt.set_cmap(cmap_center_adjust(cmap, cmapcenter))

scmap.on_changed(update)







pyplot.show()

end colormap_slider.py

View this message in context: http://old.nabble.com/How-to-shift-colormap–tp32792283p32827012.html

Sent from the matplotlib - users mailing list archive at Nabble.com.


RSA(R) Conference 2012

Save $700 by Nov 18

Register now

http://p.sf.net/sfu/rsa-sfdev2dev1


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users