Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

color-bar legend for the LisContourPlot

604 views
Skip to first unread message

Karamiarokhloo, M.P. (Mehdipasha)

unread,
Dec 9, 2011, 5:55:06 AM12/9/11
to
Dear All,

I am using Mathematica 7 and have difficulties to plot the ListContourPlot
of ocean data with a neat color bar (legend).
For example if I have a temperature field range between {-2,30}, and I use
the ColorFunction Rainbow,
I cannot include a color-bar which has the same number of contours as the contourplot, and it does not show the related numbers for each color-shade.
The ShowLegend function of Mathematica is very primitive.
Has any body any suggestion?

Thanks you.
P.Karami

Armand Tamzarian

unread,
Dec 10, 2011, 7:32:17 AM12/10/11
to
On Dec 9, 9:55 pm, "Karamiarokhloo, M.P. (Mehdipasha)"
Can you provide an example of what you are plotting?

Mike

Jacopo Bertolotti

unread,
Dec 10, 2011, 7:36:52 AM12/10/11
to
I encountered a very similar problem and I am also searching for a
solution that do not involve either ShowLegend or a third-part package.
The very best I came up with is something like
GraphicsRow[{ListContourPlot[RandomReal[30, {10, 10}],
InterpolationOrder -> 3, ColorFunction -> "LakeColors"],
MatrixPlot[Transpose@{Table[i, {i, 1, 30}]},
FrameTicks -> {{None, {1, 10, 20, 30}}, {None, None}},
ColorFunction -> "LakeColors"]}]
that is still far from ideal. Anyone with suggestions is welcomed.

Jacopo

Armand Tamzarian

unread,
Dec 11, 2011, 3:54:22 AM12/11/11
to
On Dec 10, 11:36 pm, Jacopo Bertolotti <jacopo.bertolo...@gmail.com>
wrote:
> I encountered a very similar problem and I am also searching for a
> solution that do not involve either ShowLegend or a third-part package.
> The very best I came up with is something like
> GraphicsRow[{ListContourPlot[RandomReal[30, {10, 10}],
> InterpolationOrder -> 3, ColorFunction -> "LakeColors"],
> MatrixPlot[Transpose@{Table[i, {i, 1, 30}]},
> FrameTicks -> {{None, {1, 10, 20, 30}}, {None, None}},
> ColorFunction -> "LakeColors"]}]
> that is still far from ideal. Anyone with suggestions is welcomed.
>
> Jacopo
>
> On 12/09/2011 11:54 AM, Karamiarokhloo, M.P. (Mehdipasha) wrote:
>
>
>
>
>
>
>
> > Dear All,
>
> > I am using Mathematica 7 and have difficulties to plot the ListContourP=
lot
> > of ocean data with a neat color bar (legend).
> > For example if I have a temperature field range between {-2,30}, and I =
use
> > the ColorFunction Rainbow,
> > I cannot include a color-bar which has the same number of contours as t=
he contourplot, and it does not show the related numbers for each color-sha=
de.
> > The ShowLegend function of Mathematica is very primitive.
> > Has any body any suggestion?
>
> > Thanks you.
> > P.Karami

AFAIK Graphics grid renders fixed grid sizes so is not a good
aesthetic choice if the plots you are combining have quite different
sizes. You can do this a number of ways but I think Labeled makes this
pretty easy once you make the plot and legend the same height (this is
optional but for me it looks better):

legendwidth=30;
plotheights=300;

Labeled[
ListContourPlot[RandomReal[30, {10, 10}],
InterpolationOrder -> 3,
ImageSize -> plotheights+21,
ImagePadding -> {{20, 1}, {20, 1}},
ColorFunction -> "LakeColors"],
MatrixPlot[Transpose@{Table[i, {i, 1, 30}]},
AspectRatio -> plotheights/legendwidth,
FrameTicks -> {{None, {1, 10, 20, 30}}, {None, None}},
ImageSize -> {legendwidth+21, plotheights+2},
ImagePadding -> {{1, 20}, {1, 1}},
ColorFunction -> "LakeColors"],
{{Right, Top}}]

To understand what I am doing with image padding and aspect ratio see
the current post "Precicely defining plot size ". You could control
the spacing between the plot and the legend by adjusting the image
padding on the plots.

Mike

Armand Tamzarian

unread,
Dec 11, 2011, 3:55:53 AM12/11/11
to
On Dec 10, 11:36 pm, Jacopo Bertolotti <jacopo.bertolo...@gmail.com>
wrote:
BTW in the post I sent a few minutes ago I noticed that the legend
colours aren't scaled right. I'm not sure why actually.
ColorFunctionScaling should scale between 0 and 1 that but it doesn't
appear to be doing that -- unless I am misunderstanding the usage. I
never use these plot types so I might be missing something but would
be interested if someone could explain. In any case if you rescale
yourself and switch ColorFunctionScaling off it seems to work:

MatrixPlot[Transpose@{Table[i, {i, 0, 30}]/30},
AspectRatio -> plotheights/legendwidth,
FrameTicks -> {{None, {1, 10, 20, 30}}, {None, None}},
ImageSize -> {legendwidth + 21, plotheights + 2},
PlotRangePadding -> 0, ImagePadding -> {{1, 20}, {1, 1}},
ColorFunction -> "LakeColors", ColorFunctionScaling -> False]

Mike

Karamiarokhloo, M.P. (Mehdipasha)

unread,
Dec 13, 2011, 5:38:31 AM12/13/11
to
Hi,
The commands suggested by Jacopo and Armand, work well but if different data which include negative values are used
then the problem appears that negative values do not come in the legend.At least I could not do it.
Here is the command by Armand but I used a different input data.I got wrong color-bar...

data = Table[{x = RandomReal[{-2, 2}], y = RandomReal[{-2, 2}],
Sin[x y]}, {100}];

legendwidth = 30;
plotheights = 300;

Labeled[ListContourPlot[data, InterpolationOrder -> 3,
ImageSize -> plotheights + 21, ImagePadding -> {{20, 1}, {20, 1}},
ColorFunction -> "Rainbow"],
MatrixPlot[Transpose@{Table[i, {i, 1, -1, -0.2}]},
AspectRatio -> plotheights/legendwidth,
FrameTicks -> {{None, {-1, -0.5, 0, 0.5, 1}}, {None, None}},
ImageSize -> {legendwidth + 21, plotheights + 2},
ImagePadding -> {{1, 20}, {1, 1}},
ColorFunction -> "Rainbow"], {{Right, Top}}]
________________________________________
From: Armand Tamzarian [mike.hon...@gmail.com]
Sent: Sunday, December 11, 2011 9:47 AM
Subject: Re: color-bar legend for the LisContourPlot

On Dec 10, 11:36 pm, Jacopo Bertolotti <jacopo.bertolo...@gmail.com>
wrote:
> I encountered a very similar problem and I am also searching for a
> solution that do not involve either ShowLegend or a third-part package.
> The very best I came up with is something like
> GraphicsRow[{ListContourPlot[RandomReal[30, {10, 10}],
> InterpolationOrder -> 3, ColorFunction -> "LakeColors"],
> MatrixPlot[Transpose@{Table[i, {i, 1, 30}]},
> FrameTicks -> {{None, {1, 10, 20, 30}}, {None, None}},
> ColorFunction -> "LakeColors"]}]
> that is still far from ideal. Anyone with suggestions is welcomed.
>
> Jacopo
>
> On 12/09/2011 11:54 AM, Karamiarokhloo, M.P. (Mehdipasha) wrote:
>
>
>
>
>
>
>
> > Dear All,
>
> > I am using Mathematica 7 and have difficulties to plot the ListContourPlot
> > of ocean data with a neat color bar (legend).
> > For example if I have a temperature field range between {-2,30}, and I use
> > the ColorFunction Rainbow,
> > I cannot include a color-bar which has the same number of contours as the contourplot, and it does not show the related numbers for each color-shade.
> > The ShowLegend function of Mathematica is very primitive.
> > Has any body any suggestion?
>
> > Thanks you.
> > P.Karami

AFAIK Graphics grid renders fixed grid sizes so is not a good
aesthetic choice if the plots you are combining have quite different
sizes. You can do this a number of ways but I think Labeled makes this
pretty easy once you make the plot and legend the same height (this is
optional but for me it looks better):

legendwidth=30;
plotheights=300;

Labeled[
ListContourPlot[RandomReal[30, {10, 10}],
InterpolationOrder -> 3,
ImageSize -> plotheights+21,
ImagePadding -> {{20, 1}, {20, 1}},
ColorFunction -> "LakeColors"],
MatrixPlot[Transpose@{Table[i, {i, 1, 30}]},
AspectRatio -> plotheights/legendwidth,
FrameTicks -> {{None, {1, 10, 20, 30}}, {None, None}},
ImageSize -> {legendwidth+21, plotheights+2},
ImagePadding -> {{1, 20}, {1, 1}},
ColorFunction -> "LakeColors"],

Mike H

unread,
Dec 13, 2011, 5:46:27 AM12/13/11
to
If you evaluate ColorData["Rainbow"] you will see that the function runs
from {0,1} therefore if ColorFunctionScaling is True in your contour plot,
by default it is I think, then you need to similarly scale your legend.
Like I said I don't normally use these sorts of plots but its seems to me
that ColorFunctionScaling should do this for you on the MatrixPlot. I
couldn't get it to work so had to manually scale things. I'm hoping that
someone else can explain what was going on with ColorFunctionScaling.

So in your matrix plot you want to plot Table[i,{0, div}]/div where div is
the number of divisions you want, i.e. the resolution of your legend. Or
alternatively Table[i,{0, 1, 1/div}]. Make some ticks:

ticks = Table[{i + 1, Rescale[i, {0, div}, {-2., 2.}]}, {i, 0, div}]

Here is an example with div

data = Table[{x = RandomReal[{-2, 2}], y = RandomReal[{-2, 2}],
Sin[x y]}, {100}];

legendwidth = 30;
plotheights = 300;
div;

ticks = Table[{i + 1, Rescale[i, {0, 20}, {-2., 2.}]}, {i, 0, 20}];

Labeled[ListContourPlot[data, InterpolationOrder -> 3,
ImageSize -> plotheights + 21, ImagePadding -> {{20, 1}, {20, 1}},
ColorFunction -> "Rainbow"],
MatrixPlot[Transpose@{Table[i, {i, 0, div}]/div},
AspectRatio -> plotheights/legendwidth,
FrameTicks -> {{None, ticks}, {None, None}},
ImageSize -> {legendwidth + 21, plotheights + 2},
ImagePadding -> {{1, 20}, {1, 1}}, ColorFunctionScaling -> False,
ColorFunction -> "Rainbow"], {{Right, Top}}]


Mike
> To: math...@smc.vnet.net
> Subject: Re: color-bar legend for the LisContourPlot
>
> On Dec 10, 11:36 pm, Jacopo Bertolotti <jacopo.bertolo...@gmail.com>
> wrote:
> > I encountered a very similar problem and I am also searching for a
> > solution that do not involve either ShowLegend or a third-part package.
> > The very best I came up with is something like
> > GraphicsRow[{ListContourPlot[RandomReal[30, {10, 10}],
> > InterpolationOrder -> 3, ColorFunction -> "LakeColors"],
> > MatrixPlot[Transpose@{Table[i, {i, 1, 30}]},
> > FrameTicks -> {{None, {1, 10, 20, 30}}, {None, None}},
> > ColorFunction -> "LakeColors"]}]
> > that is still far from ideal. Anyone with suggestions is welcomed.
> >
> > Jacopo
> >
> > On 12/09/2011 11:54 AM, Karamiarokhloo, M.P. (Mehdipasha) wrote:
> >
> >
> >
> >
> >
> >
> >
> > > Dear All,
> >
> > > I am using Mathematica 7 and have difficulties to plot the
> ListContourP=
> lot
> > > of ocean data with a neat color bar (legend).
> > > For example if I have a temperature field range between {-2,30}, and I
> =
> use
> > > the ColorFunction Rainbow,
> > > I cannot include a color-bar which has the same number of contours as
> t=
> he contourplot, and it does not show the related numbers for each
> color-sha=

Dan

unread,
Dec 14, 2011, 6:03:52 AM12/14/11
to
On Dec 9, 5:55 am, "Karamiarokhloo, M.P. (Mehdipasha)"
I like using Grid for this, and in general for constructing compound
plots. For the legend, I use a trick I learned from David Park in this
forum. Just make another contour plot. This gives you great control
over choice and labeling of the contours. Here is a simple example
where I have identified the various layout elements explicitly.

With[{
zmin=-2,zmax=2,delz=0.5,datasize={10,10},
plotW=300,plotH=300,legendW,
ticklblW=30,ticklblH,grout=2,
colorset="ArmyColors"},

Module[{data,contours},
contours=Range[zmin,zmax,delz];
data=RandomReal[{zmin,zmax},datasize];

Grid[{{
ListContourPlot[data,InterpolationOrder->3,Contours->contours,
ImagePadding->{{ticklblW+grout,grout},{ticklblH+grout,grout}},
ImageSize->{plotW+ticklblW+2grout,plotH+ticklblH+2grout},
AspectRatio->plotH/plotW,
ColorFunction->colorset],
ContourPlot[y,{x,0,1},{y,zmin,zmax},Contours->contours,
ImagePadding->{{grout,ticklblW+grout},{ticklblH+grout,grout}},
ImageSize->{legendW+ticklblW+2grout,plotH+ticklblH+2grout},
AspectRatio->plotH/legendW,
FrameTicks->{{None,contours},{None,None}},
ColorFunction->colorset]
}}]
]
]

-- Dan

Dan

unread,
Dec 15, 2011, 4:54:15 AM12/15/11
to
Somehow some specific assignments went missing in this post. Since I
copied and pasted into the window I don't know how that could have
happened, but use:

legendW
ticklblH

to get the figure as I intended.

-- Dan

DrMajorBob

unread,
Dec 15, 2011, 4:59:23 AM12/15/11
to
That code fails with this error:

With::lvws: Variable legendW in local variable specification
{zmin=-2,zmax=2,delz=0.5,datasize={10,10},plotW=300,plotH=300,legendW,ticklblW=30,ticklblH,grout=2,colorset=ArmyColors}
requires a value. >>

Bobby

On Wed, 14 Dec 2011 05:00:36 -0600, Dan <dfl...@rcn.com> wrote:

> On Dec 9, 5:55 am, "Karamiarokhloo, M.P. (Mehdipasha)"
> <M.P.Karamiarokh...@uu.nl> wrote:
--
DrMaj...@yahoo.com

DrMajorBob

unread,
Dec 16, 2011, 5:57:39 AM12/16/11
to
> use:
>
> legendW
> ticklblH
>
> to get the figure as I intended.

Still without values?

Bobby

On Thu, 15 Dec 2011 03:51:58 -0600, Dan <dfl...@rcn.com> wrote:

> On Dec 14, 6:03 am, Dan <dfla...@rcn.com> wrote:
> Somehow some specific assignments went missing in this post. Since I
> copied and pasted into the window I don't know how that could have
> happened, but use:
>
> legendW
> ticklblH
>
> to get the figure as I intended.
>
> -- Dan
>


--
DrMaj...@yahoo.com

Dan

unread,
Dec 16, 2011, 6:08:27 AM12/16/11
to
I don't know why my postings are getting mangled. This is pretty
frustrating. In words, just to avoid whatever mechanism is going on:

set legendW to 20
set ticklblH to 20

the ticklblH (20) and ticklblW (30) are the pixel space allocated for
each numeric tick label. Of course, legendW (20) is the width of the
legend.

As an aside to the moderator, I would appreciate some guidance as to
how to avoid these mysterious modifications to my posts.



-- Dan

0 new messages