[Matplotlib-users] Displaying raster colormap in a colorbar

Matthew Bradley mbatr27 at gmail.com
Fri Aug 3 17:26:35 EDT 2018

Here's a basic mockup of what I'm running into:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)

example_2D = np.random.rand(841,138)

example_raster = np.empty([example_2D.shape[0],example_2D.shape[1],3])
for i in range(example_2D.shape[0]):
    for j in range(example_2D.shape[1]):
        if example_2D[i,j] < 0.5:
            example_raster[i,j] = [0,example_2D[i,j],0]
            example_raster[i,j] = [example_2D[i,j],0,0]

fig, ax = plt.subplots(figsize=(4,4))

im = ax.imshow(example_raster, aspect='auto')

fig.colorbar(im, orientation="horizontal")

There is a ipynb file attached for anyone who wants to play around with it.

@ Bruno and Benjamin: Yes, that's essentially what I am doing. Here in this
simple example I'm just doctoring it so that I get red and green values but
in the actual dataset there are negative values that I set to be green and
all the positive values are red

On Fri, Aug 3, 2018 at 3:14 PM, Bruno Pagani <bruno.pagani at astrophysics.eu>

> Le 03/08/2018 à 23:09, Benjamin Root a écrit :
> > I think the issue is that the user is passing in an RGB array, but
> > wants a colormap that matches that array. That is a bit tricky. Do I
> > have my understanding correct?
> >
> > Ben
> Exactly what I understood too. So do you have a solution for this? I’ve
> stackoverflow’d that question a bit, nothing cames out.
> Bruno

Matthew Bradley
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/a2801e82/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Raster_Basic_Example.ipynb
Type: application/octet-stream
Size: 195909 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/a2801e82/attachment-0001.obj>

More information about the Matplotlib-users mailing list