I have a ContourPlot where I have let Mathematica draw the contours automatically. I would like to extract the zmax and zmin contour values that have been determined internally so that I can pass them to ShowLegend to be shown with the color-bar. I am using Mathematica 8.
Needs["PlotLegends`"]
plTest = ContourPlot[xv^2 + yv^2, {xv, 0, 1}, {yv, 0, 1},
Contours -> 9, ColorFunction -> "Rainbow"];
ShowLegend[plTest, {ColorData["Rainbow"][1 - #1] &, 10, "max", "min",
LegendPosition -> {0.6, 0}, BaseStyle -> {FontSize -> 14}}]
I would like the actual zmax and zmin values to appear in the colorbar in the legend instead of the "max" and "min" above. Can someone please help me with this?
There is a similar post: ShowLegend values , but I can't get this to work with ContourPlot type. Thanks.
Answer
The question refers to my answer to "ShowLegend values" and mentions that it doesn't work with this plot. However, it does work.
The only thing is that for a ContourPlot
, one may not want a smooth color gradient in the legend. I actually addressed that in a subsequent answer to "How can I label a ListDensityPlot with a color bar?".
So just follow the instructions in the last link, i.e., load the definitions from the first link and then change colorLegend
as given in the second link.
With that, you could then do your plot as follows:
{plot, colors, range} =
reportColorRange[
ContourPlot[xv^2 + yv^2, {xv, 0, 1}, {yv, 0, 1}, Contours -> 9,
ColorFunction -> "Rainbow"]]
The last output is the specific answer to the question: it states the range {0, 2}
as it was detected by reportColorRange
. For legending purposes, it's important to realize that the contour values don't reflect the entire value range because the top and bottom of the range aren't drawn as contours. So post-processing only the drawn contours isn't the correct approach to make a legend. This is why I made the reportColorRange
function which monitors what actually is calculated at the time the plot is done.
contour =
display[{plot // at[{0, 0}, .8],
colorLegend[colors, range, 11] //
at[{0.8, 0.1}, Scaled[{.15, 1.5}]]}]
The third argument in colorLegend
is the number of tick marks (including the bottom and top-most marks), so in this case for 9
contours it's 11
because the top and bottom of the range aren't drawn as contours.
Comments
Post a Comment