Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Change colors in contour plot and including a legend?

  • To: mathgroup at
  • Subject: [mg82045] Re: Change colors in contour plot and including a legend?
  • From: Will Robertson <wspr81 at>
  • Date: Wed, 10 Oct 2007 04:29:07 -0400 (EDT)
  • References: <fefina$jol$>

On Oct 9, 6:47 pm, sean_incali <sean_inc... at> wrote:
> Can you change the output of the ContourPlot from gray levels to
> colors?  And then have a legend that breaks down the levels into say
> 10 or whatever number of bins and denote what the values are?

I've attached a package that might help.
I wrote it the other day following help from this very list, and I'm
sure it can be improved. Feedback welcome.

Use it like this:

ColorbarPlot[#1^2 + #2^2 &, {-10, 10}, {-5, 5}]

By default it creates a density plot, but that can be changed:

ColorbarPlot[#1^2 + #2^2 &, {-10, 10}, {-5, 5},
 PlotType -> ContourPlot,
 Colors -> "DarkBands",
 XLabel -> "x",
 YLabel -> "y",
 Title -> "Title",
 CLabel -> "Range",
 Height -> 300]

Hope this helps,


ColorbarPlot v0.1
2007 Oct 08

ColorbarPlot is a function to plot either a ContourPlot or a
DensityPlot with an attached colorbar to indicate the ranges of the
function that is being plotted. The syntax is not exactly the same as
for ContourPlot or DensityPlot.

Please see the code below (or ?ColorbarPlot) for further details. An
example file should be distributed with this package to demonstrate
its use.

This package has been written with and for Mathematica 6.0.0.
I doubt that it will work in previous versions of Mathematica.
Who knows if it will work in the future.

Please send comments and suggestions to wspr 81 at gmail dot com.

Copyright 2007
Will Robertson

This package consists of the files ColorbarPlot.m and ColorbarPlot-
examples.nb. It may be freely distributed and modified under the terms
& conditions of the Apache License, v2.0: <


ColorbarPlot::usage =
Creates a DensityPlot or a ContourPlot with an attached
colorbar to denote the range of the function F[x,y].

Options (defaults shown) are:
  PlotType -> DensityPlot (or ContourPlot, or ...?)
  Colors   -> \"LakeColors\" (or \"PidgeonTones\" or ...)
  XLabel   -> \"\" (label below the plot frame)
  YLabel   -> \"\" (label left of the plot frame)
  Title    -> None (label above the plot from)
  CLabel   -> None (label above the colorbar)
  Height   -> 8*72/2.54 (height of the plot; 8cm default)

Regular options to the plot function will also be
passed through. (Therefore, XLabel and YLabel, etc.
can be overriden with a regular FrameLabel option.)";


  Colors   -> "LakeColors",
  PlotType -> DensityPlot,
  XLabel   -> "",
  YLabel   -> "",
  CLabel   -> None,
  Title    -> None,
  Height   -> 8*72/2.54

  opts:OptionsPattern[]] :=
  (* local variables *)
   max = -Infinity,
   min = Infinity,

  (* Option processing
    (filter out options for the plot itself) *)
  Opt[x_] :=

  (* Define a function from the input that records the
     maxima and minima of the function as it is evaluated.
     This information is then used in the colorbar. *)
  monitor[x_?NumericQ,y_?NumericQ] :=

  (* Draw the plot and colorbar next to each other *)
    (* Here's the plot: *)
      ImageSize -> {Automatic,Opt[Height]},
      ColorFunction -> Opt[Colors],
      FrameLabel -> {{Opt[YLabel],None},
    (* And the colourbar: *)
      ImageSize -> {Automatic,Opt[Height]},
      ColorFunction -> Opt[Colors],
      PlotRange -> Full,
      AspectRatio -> Automatic,
      PlotRangePadding -> 0,
      FrameLabel -> {{"",""},{"",Opt[CLabel]}},
      (* the empty frame tick here is
         to align the colorbar with the plot: *)
      FrameTicks -> {{All,None},{{{0,""}},None}}]


  • Prev by Date: Re: Matrices
  • Next by Date: Re: tetrahedron problem
  • Previous by thread: Re: Change colors in contour plot and including a legend?
  • Next by thread: Re: Re: Change colors in contour plot and including a legend?