2D array plot picking explicit values to colour and legend

Dear matplotlib-users,

I have a spatial data set that has coded values for each cell, which are limited to just a few numbers, ie -8888, 0, 100, and 9999. I would like to display this data with a plot similar to pcolor, but I don't want a colorbar, I want a legend showing the colors for each code and an explanation for what each code represents. I would like to be able to choose a subset of the codes as well, for example just plotting the 0 and 100 codes and ignoring the -8888 and 9999 codes.

I have seen a few similar attempts that used BoundaryNorm, but I don't want to show a range of values I just want to set colors for a few explicit values. Those examples also had a colorbar and, as I said, I would prefer a legend.

Can anyone offer any tips?

Thanks

Mat

···

--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

Dear matplotlib-users,

I have a spatial data set that has coded values for each cell, which are
limited to just a few numbers, ie -8888, 0, 100, and 9999. I would like
to display this data with a plot similar to pcolor, but I don't want a
colorbar, I want a legend showing the colors for each code and an
explanation for what each code represents. I would like to be able to
choose a subset of the codes as well, for example just plotting the 0
and 100 codes and ignoring the -8888 and 9999 codes.

I have seen a few similar attempts that used BoundaryNorm, but I don't
want to show a range of values I just want to set colors for a few
explicit values. Those examples also had a colorbar and, as I said, I
would prefer a legend.

Can anyone offer any tips?

For the plot itself you can use pcolor if your data are on a quadrilateral grid, or a PathCollection or PolyCollection otherwise. For the legend, you can use proxy artists:

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

Eric

···

On 04/09/2012 02:22 AM, Mathew Topper wrote:

Thanks

Mat

Hi Eric, thanks for the tip about the legend.

Regarding the data, assuming i am using pcolor, am I right in

thinking that using Boundarynorm would be the best way to control
the colors for each code?

Thanks

Mat
···

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artisthttp://p.sf.net/sfu/Boundary-d2dvs2Matplotlib-users@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/matplotlib-users

  Dr. Mathew Topper

  Institute for Energy Systems

  School of Engineering

  The University of Edinburgh

  Faraday Building

  The King’s Buildings

  Edinburgh EH9 3JL

  Tel: +44 (0)131 650 5570

  School fax: +44 (0)131 650 6554

  mathew.topper@...3926...

  [http://www.see.ed.ac.uk](http://www.see.ed.ac.uk/)

Hi Eric, thanks for the tip about the legend.

Regarding the data, assuming i am using pcolor, am I right in thinking
that using Boundarynorm would be the best way to control the colors for
each code?

Mat,

I think BoundaryNorm is overkill and/or awkward for your case. It sounds like you don't have ordinary values, but rather a set of labels that happen to be integers. I would use a ListedColormap and then use sequential integers as the C values to index directly into the colormap:

C = np.array([[0,1,2],[2,0,1]])
import matplotlib.colors as mcolors
cmap = mcolors.ListedColormap(['r', 'g', 'lightgray'])
pcolor(C, cmap=cmap, norm=mcolors.NoNorm())

Of course you would need to map your sequence of numbers (-8888, 0, ...) to a sequence of integers starting at zero.

The key point is that the NoNorm() instance leaves your original C values alone, and since they are integers, they are then used directly as indices.

You could also make your own mcolors.Normalize subclass which would process your labels and return either a float in the 0-1 range, or an integer for direct indexing.

When you need only a very few colors, the ListedColormap with direct indexing is nice because it allows you to specify those colors using any valid color specification method.

Eric

···

On 04/09/2012 08:17 AM, Mathew Topper wrote:

Thanks

Mat

On 04/09/2012 06:26 PM, Eric Firing wrote:

On 04/09/2012 02:22 AM, Mathew Topper wrote:

Dear matplotlib-users,

I have a spatial data set that has coded values for each cell, which are
limited to just a few numbers, ie -8888, 0, 100, and 9999. I would like
to display this data with a plot similar to pcolor, but I don't want a
colorbar, I want a legend showing the colors for each code and an
explanation for what each code represents. I would like to be able to
choose a subset of the codes as well, for example just plotting the 0
and 100 codes and ignoring the -8888 and 9999 codes.

I have seen a few similar attempts that used BoundaryNorm, but I don't
want to show a range of values I just want to set colors for a few
explicit values. Those examples also had a colorbar and, as I said, I
would prefer a legend.

Can anyone offer any tips?

For the plot itself you can use pcolor if your data are on a
quadrilateral grid, or a PathCollection or PolyCollection otherwise.
For the legend, you can use proxy artists:

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

Eric

Thanks

Mat

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
Dr. Mathew Topper
Institute for Energy Systems
School of Engineering
The University of Edinburgh
Faraday Building
The King’s Buildings
Edinburgh EH9 3JL
Tel: +44 (0)131 650 5570
School fax: +44 (0)131 650 6554
mathew.topper@...3926... <mailto:mathew.topper@…3926…>
http://www.see.ed.ac.uk/&gt;

The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options