consistent colors between imshow and scatter

I have 2 float arrays of the same dimension which I use to generate a 3rd array, again of the same dimension, containing integers from a small set (I obtain the 3rd array via clustering in the 2 dimensional space of points obtained as values from the same location in the initial 2 arrays). I'd like to do a scatter plot using scatter, with x-axis values as values from the first array, y-values as values from the second array and color of the points corresponding to the integer in the 3rd array (so far so good; I can do that much). Then I'd like to plot an image using imshow of the 3rd array with colors corresponding to those in the scatter plot. I can generate the image with imshow ok but can't the colors to match those in the scatter plot. Here's a snippet (assume array1,array2, and array3 are 2D arrays):

hot()
scatter(array1.ravel(), array2.ravel(), c = numpy.array(array3.ravel(),float))
imshow(array3)
show()

The main problem is that I can't figure out how to force the plot colors for imshow to correspond to those in scatter. Thanks for any thought or suggestions. Cheers,

-- KY

w/o a complete, self contained code sample that we can run and play
with on our machines, it is harder to help. Have you tried forcing
the clim to be set to the interval you want?

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.clim

JDH

···

On Thu, Jun 4, 2009 at 6:38 PM, Karl Young<karl.young@...2634...> wrote:

I have 2 float arrays of the same dimension which I use to generate a
3rd array, again of the same dimension, containing integers from a small
set (I obtain the 3rd array via clustering in the 2 dimensional space of
points obtained as values from the same location in the initial 2
arrays). I'd like to do a scatter plot using scatter, with x-axis values
as values from the first array, y-values as values from the second array
and color of the points corresponding to the integer in the 3rd array
(so far so good; I can do that much). Then I'd like to plot an image
using imshow of the 3rd array with colors corresponding to those in the
scatter plot. I can generate the image with imshow ok but can't the
colors to match those in the scatter plot. Here's a snippet (assume
array1,array2, and array3 are 2D arrays):

hot()
scatter(array1.ravel(), array2.ravel(), c =
numpy.array(array3.ravel(),float))
imshow(array3)
show()

The main problem is that I can't figure out how to force the plot colors
for imshow to correspond to those in scatter. Thanks for any thought or
suggestions. Cheers,

Thanks for the tip and sorry I didn't include a complete code snippet; the current code involves images (scipy.ndimage) and clustering code and I thought that was a little too much too include; I'll try to extract something simpler. I guess the main question is how to use a set of integers to index a color space consistently for both scatter and imshow but I'll try to come up with a simple example. Thanks agan.

···

________________________________________
From: John Hunter [jdh2358@...287...]
Sent: Friday, June 05, 2009 5:43 AM
To: Young, Karl
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] consistent colors between imshow and scatter

On Thu, Jun 4, 2009 at 6:38 PM, Karl Young<karl.young@...2634...> wrote:

I have 2 float arrays of the same dimension which I use to generate a
3rd array, again of the same dimension, containing integers from a small
set (I obtain the 3rd array via clustering in the 2 dimensional space of
points obtained as values from the same location in the initial 2
arrays). I'd like to do a scatter plot using scatter, with x-axis values
as values from the first array, y-values as values from the second array
and color of the points corresponding to the integer in the 3rd array
(so far so good; I can do that much). Then I'd like to plot an image
using imshow of the 3rd array with colors corresponding to those in the
scatter plot. I can generate the image with imshow ok but can't the
colors to match those in the scatter plot. Here's a snippet (assume
array1,array2, and array3 are 2D arrays):

hot()
scatter(array1.ravel(), array2.ravel(), c =
numpy.array(array3.ravel(),float))
imshow(array3)
show()

The main problem is that I can't figure out how to force the plot colors
for imshow to correspond to those in scatter. Thanks for any thought or
suggestions. Cheers,

w/o a complete, self contained code sample that we can run and play
with on our machines, it is harder to help. Have you tried forcing
the clim to be set to the interval you want?

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.clim

JDH

Young, Karl wrote:

Thanks for the tip and sorry I didn't include a complete code
snippet; the current code involves images (scipy.ndimage) and
clustering code and I thought that was a little too much too include;
I'll try to extract something simpler. I guess the main question is
how to use a set of integers to index a color space consistently for
both scatter and imshow but I'll try to come up with a simple
example. Thanks agan.

Karl,

It sounds like you need to specify the norm=colors.NoNorm() argument.
http://matplotlib.sourceforge.net/api/colors_api.html?highlight=nonorm#matplotlib.colors.NoNorm
   Then if your data are integers (typed as integers, not just taking integer values), they will be interpreted as indices into the color table. You will probably also want to generate that table yourself and specify it via the cmap=my_cmap kwarg. If you have only a few values, then you may want to generate it as a colors.ListedColormap:
http://matplotlib.sourceforge.net/api/colors_api.html?highlight=listedcolormap#matplotlib.colors.ListedColormap
http://matplotlib.sourceforge.net/examples/api/colorbar_only.html?highlight=listedcolormap

It looks like we don't have any good examples sitting around showing the use of NoNorm; but try it anyway.

Eric

Hi Eric,

Thanks much - I'll try that.

···

________________________________________
From: Eric Firing [efiring@...202...]
Sent: Friday, June 05, 2009 10:43 AM
To: Young, Karl
Cc: John Hunter; matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] consistent colors between imshow and scatter

Young, Karl wrote:

Thanks for the tip and sorry I didn't include a complete code
snippet; the current code involves images (scipy.ndimage) and
clustering code and I thought that was a little too much too include;
I'll try to extract something simpler. I guess the main question is
how to use a set of integers to index a color space consistently for
both scatter and imshow but I'll try to come up with a simple
example. Thanks agan.

Karl,

It sounds like you need to specify the norm=colors.NoNorm() argument.
http://matplotlib.sourceforge.net/api/colors_api.html?highlight=nonorm#matplotlib.colors.NoNorm
   Then if your data are integers (typed as integers, not just taking
integer values), they will be interpreted as indices into the color
table. You will probably also want to generate that table yourself and
specify it via the cmap=my_cmap kwarg. If you have only a few values,
then you may want to generate it as a colors.ListedColormap:
http://matplotlib.sourceforge.net/api/colors_api.html?highlight=listedcolormap#matplotlib.colors.ListedColormap
http://matplotlib.sourceforge.net/examples/api/colorbar_only.html?highlight=listedcolormap

It looks like we don't have any good examples sitting around showing the
use of NoNorm; but try it anyway.

Eric

hmmm... in response to John's earlier request I cobbled a simple example but that seems to work; either there's a strange error in the more complex version or my misunderstanding of color maps is messing with me. Anyway, for the record, here's an example of what I want to do (that actually seems to work)

···

------------------------------------------------------------------------------------

#!/usr/bin/env python

# Example to try and use same color maps for scatter and imshow

import numpy as np
from pylab import *

# size of array dimensions
size = 10

# cluster centers
acenter1 = 3.0
asigma1 = 1.0
bcenter1 = 6.0
bsigma1 = 0.5

acenter2 = 8.0
asigma2 = 2.0
bcenter2 = 3.0
bsigma2 = 1.0

acenter3 = 13.0
asigma3 = 2.0
bcenter3 = 10.0
bsigma3 = 1.0

# populate arrays with random values distrubted about 2 locations
# (i.e. 2 clusters)

a = np.zeros([size,size],float)
b = np.zeros([size,size],float)
c = np.zeros([size,size],int)

# populate arrays
for i in range(size):
    for j in range(size):
        switcher = np.random.randint(3)
        if switcher == 0: # cluster 1
            a[i,j] = np.random.normal(acenter1,asigma1)
            b[i,j] = np.random.normal(bcenter1,bsigma1)
            c[i,j] = 0
        elif switcher == 1 : # cluster 2
            a[i,j] = np.random.normal(acenter2,asigma2)
            b[i,j] = np.random.normal(bcenter2,bsigma2)
            c[i,j] = 1
        else:
           a[i,j] = np.random.normal(acenter3,asigma3)
           b[i,j] = np.random.normal(bcenter3,bsigma3)
           c[i,j] = 2

hot()
subplot(1,2,1)
scatter(np.ravel(a),np.ravel(b),c=np.ravel(c))
subplot(1,2,2)
imshow(c)

show()

------------------------------------------------------------------------------------

Young, Karl wrote:
  

Thanks for the tip and sorry I didn't include a complete code
snippet; the current code involves images (scipy.ndimage) and
clustering code and I thought that was a little too much too include;
I'll try to extract something simpler. I guess the main question is
how to use a set of integers to index a color space consistently for
both scatter and imshow but I'll try to come up with a simple
example. Thanks agan.

Karl,

It sounds like you need to specify the norm=colors.NoNorm() argument.
http://matplotlib.sourceforge.net/api/colors_api.html?highlight=nonorm#matplotlib.colors.NoNorm
   Then if your data are integers (typed as integers, not just taking integer values), they will be interpreted as indices into the color table. You will probably also want to generate that table yourself and specify it via the cmap=my_cmap kwarg. If you have only a few values, then you may want to generate it as a colors.ListedColormap:
http://matplotlib.sourceforge.net/api/colors_api.html?highlight=listedcolormap#matplotlib.colors.ListedColormap
http://matplotlib.sourceforge.net/examples/api/colorbar_only.html?highlight=listedcolormap

It looks like we don't have any good examples sitting around showing the use of NoNorm; but try it anyway.

Eric

.