Displaying raster colormap in a colorbar

All:

I have a raster that I want to add a colorbar to, and I've tried playing
around with a few things, both giving the axes image a name and then
calling it in the colorbar function i.e:

cax = axes.imshow(raster_contrast*sorted_raster, aspect=aspect_ratio)
cbar = fig.colorbar(cax, orientation='horizontal')

and after the image is plotted like so:

cax = ax[1].get_images()[0]
cbar = fig.colorbar(cax, orientation='horizontal', cax=ax[2])

In either case it produces a PCyGY colormap instead of the diverging
RGBlack color scheme. How can I replicate the correct color map in the
color bar? Both of these methods appear to work for 2-D array.

the sorted raster is an array of shape [861, 138, 3].

Thanks in advance for any help!

···

--
Matthew Bradley
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/874334c9/attachment.html>

Sorry I don?t quite understand this question.

Is there any chance you can make a concrete minimal, runnavle example using
fake data that delonstrates the issue?

···

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

All:

I have a raster that I want to add a colorbar to, and I've tried playing
around with a few things, both giving the axes image a name and then
calling it in the colorbar function i.e:

cax = axes.imshow(raster_contrast*sorted_raster, aspect=aspect_ratio)
cbar = fig.colorbar(cax, orientation='horizontal')

and after the image is plotted like so:

cax = ax[1].get_images()[0]
cbar = fig.colorbar(cax, orientation='horizontal', cax=ax[2])

In either case it produces a PCyGY colormap instead of the diverging
RGBlack color scheme. How can I replicate the correct color map in the
color bar? Both of these methods appear to work for 2-D array.

the sorted raster is an array of shape [861, 138, 3].

Thanks in advance for any help!
--
Matthew Bradley
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/f8c06c19/attachment.html>

I'll work on it, thanks for the prompt response!

···

On Fri, Aug 3, 2018 at 2:46 PM, Nathan Goldbaum <nathan12343 at gmail.com> wrote:

Sorry I don?t quite understand this question.

Is there any chance you can make a concrete minimal, runnavle example
using fake data that delonstrates the issue?

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

All:

I have a raster that I want to add a colorbar to, and I've tried playing
around with a few things, both giving the axes image a name and then
calling it in the colorbar function i.e:

cax = axes.imshow(raster_contrast*sorted_raster, aspect=aspect_ratio)
cbar = fig.colorbar(cax, orientation='horizontal')

and after the image is plotted like so:

cax = ax[1].get_images()[0]
cbar = fig.colorbar(cax, orientation='horizontal', cax=ax[2])

In either case it produces a PCyGY colormap instead of the diverging
RGBlack color scheme. How can I replicate the correct color map in the
color bar? Both of these methods appear to work for 2-D array.

the sorted raster is an array of shape [861, 138, 3].

Thanks in advance for any help!
--
Matthew Bradley
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

--
Matthew Bradley
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/a40e5b2f/attachment.html>

Le 03/08/2018 ? 22:43, Matthew Bradley a ?crit?:

All:

I have a raster that I want to add a colorbar to, and I've tried
playing around with a few things, both giving the axes image a name
and then calling it in the colorbar function i.e:

cax = axes.imshow(raster_contrast*sorted_raster, aspect=aspect_ratio)
cbar = fig.colorbar(cax, orientation='horizontal')

and after the image is plotted like so:

cax = ax[1].get_images()[0]
cbar = fig.colorbar(cax, orientation='horizontal', cax=ax[2])

In either case it produces a PCyGY colormap

Viridis[0]?

instead of the diverging RGBlack color scheme. How can I replicate the
correct color map in the color bar? Both of these methods appear to
work for 2-D array.?

the sorted raster is an array of shape [861, 138, 3].

Thanks in advance for any help!

I don?t really know how to get a colorbar for RGB(A) arrays, but you
need to add something I guess. You?re currently getting the default
colormap and associated colorbar.

[0]: https://matplotlib.org/2.0.0/users/plotting/colormaps/lightness_00.png

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/289eec13/attachment.html>

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

···

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

I'll work on it, thanks for the prompt response!

On Fri, Aug 3, 2018 at 2:46 PM, Nathan Goldbaum <nathan12343 at gmail.com> > wrote:

Sorry I don?t quite understand this question.

Is there any chance you can make a concrete minimal, runnavle example
using fake data that delonstrates the issue?

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

All:

I have a raster that I want to add a colorbar to, and I've tried playing
around with a few things, both giving the axes image a name and then
calling it in the colorbar function i.e:

cax = axes.imshow(raster_contrast*sorted_raster, aspect=aspect_ratio)
cbar = fig.colorbar(cax, orientation='horizontal')

and after the image is plotted like so:

cax = ax[1].get_images()[0]
cbar = fig.colorbar(cax, orientation='horizontal', cax=ax[2])

In either case it produces a PCyGY colormap instead of the diverging
RGBlack color scheme. How can I replicate the correct color map in the
color bar? Both of these methods appear to work for 2-D array.

the sorted raster is an array of shape [861, 138, 3].

Thanks in advance for any help!
--
Matthew Bradley
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

--
Matthew Bradley

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/4595ea07/attachment-0001.html>

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

Off the top of my head, I wonder if you can set the colormap after the fact
on the image and then create the colorbar. The colorbar is going to assume
whatever colormap was set, and the default is viridis. The problem is that
I don't think a colormap is set on the image when RGB data is passed in
(so, it'll be None), which then means that colorbar falls back on the
default from the system, which is usually viridis. If you call
`im.set_cmap('NameOfYourColormap')`, and then call colorbar(), I think
that'll work.

Ben Root

···

On Fri, Aug 3, 2018 at 5: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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/3d2a638e/attachment.html>

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

--
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>

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

--
Matthew Bradley

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/e092f9ab/attachment.html>

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

--
Matthew Bradley

--
Matthew Bradley
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/65664566/attachment-0001.html>

Le 03/08/2018 ? 23:48, Matthew Bradley a ?crit?:

Nice solution, I wasn't thinking in those sorts of terms before but I
think that is the way to go!

Thanks!

    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?

What you want is likely a colormap like `diverging_gwr` or
`diverging_gkr` from colorcet (https://colorcet.pyviz.org/). I mean, if
you want to keep those colors.?:wink:

Bruno
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180803/ef890cf4/attachment.html>

···

On Fri, Aug 3, 2018 at 3:39 PM, Benjamin Root <ben.v.root at gmail.com > <mailto:ben.v.root at gmail.com>> wrote:

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

--
Matthew Bradley

--
Matthew Bradley

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180805/d0dfa9af/attachment.html>