# [Matplotlib-users] Displaying raster colormap in a colorbar

Benjamin Root ben.v.root at gmail.com
Sun Aug 5 11:24:45 EDT 2018

```Glad that helped. Sometimes, one get so deep within fixing a problem that
it is hard to see the easy solutions. That's why I am a huge fan of code
reviews and forums like this.

Cheers!
Ben Root

On Fri, Aug 3, 2018 at 5:48 PM, Matthew Bradley <mbatr27 at gmail.com> wrote:

> Nice solution, I wasn't thinking in those sorts of terms before but I
> think that is the way to go!
>
> Thanks!
>
> On Fri, Aug 3, 2018 at 3:39 PM, Benjamin Root <ben.v.root at gmail.com>
> wrote:
>
>> Well, why are you creating an RGB array?. Why not just pass the data into
>> imshow() and give it vmin/vmax and the colormap that you'd like to use?
>>
>> On Fri, Aug 3, 2018 at 5:26 PM, Matthew Bradley <mbatr27 at gmail.com>
>> wrote:
>>
>>> 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]
>>>         else:
>>>             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")
>>> plt.show()
>>>
>>> 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> wrote:
>>>
>>>> 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
>>>>
>>>
>>>
>>>
>>> --
>>>
>>
>>
>
>
> --