Displaying raster colormap in a colorbar

Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Displaying raster colormap in a colorbar

Matthew Bradley
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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

Nathan Goldbaum
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 <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

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

On Fri, Aug 3, 2018 at 2:46 PM, Nathan Goldbaum <[hidden email]> 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 <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users



--
Matthew Bradley

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

Bruno Pagani
In reply to this post by Matthew Bradley
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


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

Benjamin Root
In reply to this post by Matthew Bradley
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 <[hidden email]> wrote:
I'll work on it, thanks for the prompt response!

On Fri, Aug 3, 2018 at 2:46 PM, Nathan Goldbaum <[hidden email]> 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 <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users



--
Matthew Bradley

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

Bruno Pagani
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
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

Benjamin Root
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 <[hidden email]> 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


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

Matthew Bradley
In reply to this post by Bruno Pagani
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 <[hidden email]> 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

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

Raster_Basic_Example.ipynb (261K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

Benjamin Root
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 <[hidden email]> 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 <[hidden email]> 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


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

Matthew Bradley
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

Bruno Pagani
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!

On Fri, Aug 3, 2018 at 3:39 PM, Benjamin Root <[hidden email]> 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?

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. ;)

Bruno

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying raster colormap in a colorbar

Benjamin Root
In reply to this post by Matthew Bradley
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users