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 smc.vnet.net
  • Subject: [mg82045] Re: Change colors in contour plot and including a legend?
  • From: Will Robertson <wspr81 at gmail.com>
  • Date: Wed, 10 Oct 2007 04:29:07 -0400 (EDT)
  • References: <fefina$jol$1@smc.vnet.net>

On Oct 9, 6:47 pm, sean_incali <sean_inc... at yahoo.com> 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,
Will

*************

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: <http://www.apache.org/
licenses/LICENSE-2.0>

BeginPackage["ColorbarPlot`"];

ColorbarPlot::usage =
"ColorbarPlot[F[x,y],{Xmin,Xmax},{Ymin,Ymax},<options>]:
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.)";

Begin["`Private`"]

Options[ColorbarPlot]={
  Colors   -> "LakeColors",
  PlotType -> DensityPlot,
  XLabel   -> "",
  YLabel   -> "",
  CLabel   -> None,
  Title    -> None,
  Height   -> 8*72/2.54
};

ColorbarPlot[
  function_,{___,x1_,x2_},{___,y1_,y2_},
  opts:OptionsPattern[]] :=
Module[
  (* local variables *)
  {contours,
   monitor,
   max = -Infinity,
   min = Infinity,
   Opt,x,y},

  (* Option processing
    (filter out options for the plot itself) *)
  Opt[x_] :=
    OptionValue[ColorbarPlot,
                FilterRules[{opts},Options[ColorbarPlot]],
                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] :=
    Module[{val=function[x,y]},
      min=Min[min,val];
      max=Max[max,val];
      val];

  (* Draw the plot and colorbar next to each other *)
  Row[{
    (* Here's the plot: *)
    Opt[PlotType][
      monitor[x,y],{x,x1,x2},{y,y1,y2},
      Evaluate[FilterRules[{opts},
               Options[Opt[PlotType]]]],
      ImageSize -> {Automatic,Opt[Height]},
      ColorFunction -> Opt[Colors],
      FrameLabel -> {{Opt[YLabel],None},
                    {Opt[XLabel],Opt[Title]}}],
    (* And the colourbar: *)
    Opt[PlotType][
      y,{x,0,(max-min)/10},{y,min,max},
      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}}]
}]]

End[];
EndPackage[];



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