Message Boards Message Boards

0
|
2793 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How to label the max and min value in ContourPlot?

Posted 2 years ago

Hello, I have been plotting some contour plots with plot legends. Contours are ok, but legend bar is not showing the upper and lower limits as shown in the pic attached. I want to know the maximum and minimum value of the legend bar.

ClearAll["Global`*"];
da = 1;
re = 1;
Derivative[1][Subscript[p, 0]][s] = 1;
\[Lambda] = 1;
\[Beta] = ArcSin[\[Lambda]/Sqrt[1 + \[Lambda]^2] ];
u = da  re (-(1/3)
        r^2 \[Epsilon]^2 \[Lambda] Cos[\[Zeta]] Sin[\[Zeta]] + 
     5/6 r^3 \[Epsilon]^3 \[Lambda] Cos[\[Zeta]] Sin[\[Zeta]]^2) \
Derivative[1][Subscript[p, 0]][s];
v = da  re (Cos[\[Beta]] - 
     1/2 \[Epsilon]^2 Cos[\[Beta]] (-r^2 + r^2 Cos[2 \[Zeta]]) + 
     1/3 r^2 \[Epsilon]^2 \[Lambda] Cos[\[Zeta]]^2 Sin[\[Beta]] - 
     r \[Epsilon] Cos[\[Beta]] Sin[\[Zeta]] - 
     5/6 r^3 \[Epsilon]^3 \[Lambda] Cos[\[Zeta]]^2 Sin[\[Beta]] Sin[\
\[Zeta]] - r^3 \[Epsilon]^3 Cos[\[Beta]] Sin[\[Zeta]]^3) Derivative[
    1][Subscript[p, 0]][s];
w = da  re (1/
      3 r^2 \[Epsilon]^2 \[Lambda] Cos[\[Beta]] Cos[\[Zeta]]^2 - 
     Sin[\[Beta]] + 
     1/2 \[Epsilon]^2 (-r^2 + r^2 Cos[2 \[Zeta]]) Sin[\[Beta]] - 
     5/6 r^3 \[Epsilon]^3 \[Lambda] Cos[\[Beta]] Cos[\[Zeta]]^2 Sin[\
\[Zeta]] + r \[Epsilon] Sin[\[Beta]] Sin[\[Zeta]] + 
     r^3 \[Epsilon]^3 Sin[\[Beta]] Sin[\[Zeta]]^3) Derivative[1][
    Subscript[p, 0]][s];
V = Sqrt[u^2 + v^2 + w^2];
  Table[Rotate[
  ContourPlot[
   {V} /. {r -> Norm[{x, y}], \[Zeta] -> ArcTan[x, y]}, {x, -1, 
    1}, {y, -1, 1},
   RegionFunction -> (Norm[{#, #2}] <= 1 &),
   Frame -> False,
   ContourShading -> Automatic,
   ColorFunction -> "Rainbow",
   PlotLegends -> Automatic,
   Axes -> False,
   Contours -> 30
   ], 0 Degree],
 {\[Epsilon], 0.1, 0.3, 0.1}
 ]

enter image description here

POSTED BY: KRISHAN SHARMA
4 Replies
Posted 2 years ago

Thanks Eric Rimbey,
First reply answered my question. Just have to export the data.

POSTED BY: KRISHAN SHARMA
Posted 2 years ago

Here's a simple example:

But I had to calculate the range of the legend from the actual values. Even then, it didn't want to peg a label to the low end. And honestly, I don't like this as well as what Mathematica did automatically. The accidental boundary values that the function reaches in some particular region don't feel to me like they should determine the "spectrum" used for the legend. If I had set the ranges to be like {x, 0, 2}, then I'd have to do a lot of fiddling to get a faithful legend.

POSTED BY: Eric Rimbey
Posted 2 years ago

Okay, I need to walk some of that back. I suppose if your data is obviously bounded, then a bar legend with well-defined extremes makes perfect sense. And I played around with BarLegend a bit, and it is totally capable of doing that:

BarLegend[{Blend["Rainbow", #1] & , {0, 1}}, LabelStyle -> {}, 
   LegendLayout -> "Column", LegendMarkerSize -> 225, 
 Charting`AxisLabel -> None, 
   ScalingFunctions -> {Identity, Identity}, 
 Charting`TickSide -> Right, ColorFunctionScaling -> True]

What I did is take one of the BarLegends generated by your code, removed the countours, and changed the bounds to {0,1}. It creates a graphic with the labels pegged right at the extremes.

So,

  1. Again, I jumped to conclusions about bar legends because I was just thinking about countour plots that could stretch to extreme values, and I assumed that BarLegend was sort of trying to communicate that semantic.
  2. You're still going to have to build this "by hand". You're going to need to specify the specific value for PlotLegends. You'll need to figure out what your extreme values are and come up with a BarLegend expression that has the right color function and the right labels. This still seems daunting to me, but less so that I previously thought.
POSTED BY: Eric Rimbey
Posted 2 years ago

If you literally just want to know the extreme values used to generate the graphical representation of the legend, then you can actually just look at the graphic data directly.

plots = Table[
   Rotate[
    ContourPlot[{V} /. {r -> Norm[{x, y}], \[Zeta] -> 
        ArcTan[x, y]}, {x, -1, 1}, {y, -1, 1}, 
     RegionFunction -> (Norm[{#, #2}] <= 1 &), Frame -> False, 
     ContourShading -> Automatic, ColorFunction -> "Rainbow", 
     PlotLegends -> Automatic, Axes -> False, Contours -> 30], 
    0 Degree], {\[Epsilon], 0.1, 0.3, 0.1}];

Extract[plots, Most /@ Position[plots, BarLegend]] // InputForm

What I'm doing here is finding the location of the data structure representing the legend (which I can see is a BarLegend) and then extracting those structures, and then applying InputForm to show the data directly rather than the graphical representation. You should find some elements that look like this:

BarLegend[{Blend["Rainbow", #1] & , {0.9090000000005358, 1.111000000002436}},<long list of contour data>,<options>}]

(I've elided the contour data and options)

So, for that particular legend, the extreme values are 0.9090000000005358 & 1.111000000002436.

On the other hand, if what you want to know is the extreme values of your data, then you should compute that from your data rather than infer it from the legend.

On yet another hand, if what you want is to display the bar legend with the extreme values of the legend labeled, then we need to nail down what your desired semantics for your legend is. A bar legend (at least as I think of it) includes colors for "everything above x" and "everything below y". So, if a bar legend had literal max/min values (in the semantic sense, not just in the graphical sense of my first example above), then any part of your plot that exceeded these bounds couldn't be colored. Imagine that your contour plot went off to infinity somewhere. Would you want to have infinite contours and a bar legend that was infinitely long? Instead of doing that, a bar legend just leaves its tail ends to represent "everything beyond this value".

But, of course, at the end of the day it's all just data. So, if you truly do want a bar legend that is semantically capped at its extreme ends and labeled as such, then there is probably some way to custom build your own legend graphic. I suspect that it would be tedious and time-consuming.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract