2

This question is related Issue with number formatting in Bar Legend but does not fix my problem.

I have a contourplot with logarithmic scaling. Without trying to change anything, the barlegend looks as follows.

barlegend without changes

All I really want is to cut down the amount of digits to something consistent. Adapting the answer from the linked question, I did

myLegFunc[x_] := 
  x /. {NumberForm[y_, {w_, z_}] :> ScientificForm[y, 4], 
    Row[{m_, Superscript[b_, e_]}, "\[Times]"] :> 
     NumberForm[m*b^e, {3, 2}]};

ContourPlot[..., PlotLegends -> {LegendFunction -> myLegFunc}, ScalingFunctions -> "Log"]

The result of which is result with legend function

So the fix from that old answer does not work for the values that would be notated in scientific notation anyway. I tried various functions to get it to a number that I could maybe use ScientificForm on but to no avail. My understanding of mathematica is poor, but it feels like the b^e part is a number, but m is not (in the myLegFunc function). I can get 10^{-6} instead of fraction over 100,000. I cannot get it to consider the whole expression as a number.

--

As per the request, this generates a similar issue. I cannot share the original function

ContourPlot[x^10, {x, 0, 5}, {y, 0, 5}, Axes -> True, 
 AxesLabel -> {x, y}, 
 Sequence[ColorFunction -> "DeepSeaColors", Exclusions -> None], 
 Contours -> 10, PerformanceGoal -> "Quality", MaxRecursion -> 2, 
 PlotLegends -> Automatic, ScalingFunctions -> "Log"]

screenshot of output


I have accepted an answer, there is still the issue that depending on settings (plotrange etc.) occasionally the last entry is formatted wrong. I cannot reproduce it in a minimal example, so I am giving up on this. Maybe it is something in my version of mathematica or specific to my data or ..

last entry wrong

Bort
  • 123
  • 4
  • 1
    Welcome to the Mathematica Stack Exchange. Please load a minimal example (as Mathematica code) that generates the legend that needs fixing. Thanks. – Syed Oct 20 '23 at 13:53
  • @Syed I tried, see edit – Bort Oct 20 '23 at 14:01

3 Answers3

4

You can use the option LabelingFunction in BarLegend (ignore the red syntax highlighting):

ContourPlot[x^10, {x, 0, 5}, {y, 0, 5}, Axes -> True, 
 AxesLabel -> {x, y}, 
 Sequence[ColorFunction -> "DeepSeaColors", Exclusions -> None], 
 Contours -> 10, PerformanceGoal -> "Quality", MaxRecursion -> 2, 
 ScalingFunctions -> "Log", 
 PlotLegends -> 
  BarLegend[Automatic, 
   LabelingFunction -> 
   (#3 /. {r_Row :> (r /. {Infinity, _} -> {Infinity, 2}), 
           NumberForm[n_, {_, _}] :> ScientificForm[n, 3]} &)]]

enter image description here

Alternatively, replace LabelingFunction -> ... with

Charting`TickWrappers -> 
  (# /. {r_Row :> (r /. {Infinity, _} -> {Infinity, 2}), 
         NumberForm[n_, {_, _}] :> ScientificForm[n, 3]} &)

to get the same result.

kglr
  • 394,356
  • 18
  • 477
  • 896
  • I had tried LabelingFunction, but simply did not manage to get the row[..] stuff into numbers. This almost works, despite throwing an error "NumberForm::iprf: Formatting specification {,} should be a positive integer or a pair of positive integers." I say almost, because I have two plots and for the last value in the second plot it displays way more digits. I don't know why – Bort Oct 20 '23 at 15:58
3

Try this:

ContourPlot[x^10, {x, 0, 5}, {y, 0, 5}, Axes -> True, 
 AxesLabel -> {x, y}, 
 Sequence[ColorFunction -> "DeepSeaColors", Exclusions -> None], 
 Contours -> 10, PerformanceGoal -> "Quality", MaxRecursion -> 2, 
 PlotLegends -> 
  BarLegend[Automatic, None, 
   "Ticks" -> {4.1`*^-10, 1.5*^-8, 5.59`*^-7, 0.00002`, 0.00075`, 
     0.027, 1.`, 37.`, 1300.`, 49000.`}, 
   "TickLabels" -> {"4.1\[Times]\!\(\*SuperscriptBox[\(10\), \(-10\)]\
\)", "1.5\[Times]\!\(\*SuperscriptBox[\(10\), \(-8\)]\)", 
     "5.6\[Times]\!\(\*SuperscriptBox[\(10\), \(-7\)]\)", 
     "2.0\[Times]\!\(\*SuperscriptBox[\(10\), \(-5\)]\)", 
     "7.5\[Times]\!\(\*SuperscriptBox[\(10\), \(-4\)]\)", 
     "2.7\[Times]\!\(\*SuperscriptBox[\(10\), \(-2\)]\)", "1.0", 
     "3.7\[Times]\!\(\*SuperscriptBox[\(10\), \(1\)]\)", 
     "1.3\[Times]\!\(\*SuperscriptBox[\(10\), \(3\)]\)", 
     "4.9\[Times]\!\(\*SuperscriptBox[\(10\), \(4\)]\)"}, 
   LegendMarkerSize -> 300], ScalingFunctions -> "Log"]

enter image description here

Edit I do not think that any tools are available that would enable you to avoid some minimal programming. Nevertheless, find below some conciser way to do the labels:

lst = Table[1.*10^(-10 + i), {i, 0, 16, 2}];
lst2 = Map[ScientificForm, lst];
ContourPlot[x^10, {x, 0, 5}, {y, 0, 5}, Axes -> True, 
 AxesLabel -> {x, y}, 
 Sequence[ColorFunction -> "DeepSeaColors", Exclusions -> None], 
 Contours -> 10, PerformanceGoal -> "Quality", MaxRecursion -> 2, 
 PlotLegends -> 
  BarLegend[Automatic, None, "Ticks" -> lst, "TickLabels" -> lst2, 
   LegendMarkerSize -> 300], ScalingFunctions -> "Log"]

enter image description here

Have fun!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96
3

From the answer to this question

f[x_] := x /. {NumberForm[y_, {w_, z_}] :> ScientificForm[y, 2]}

ContourPlot[x^10, {x, 0, 5}, {y, 0, 5}, Axes -> True, AxesLabel -> {x, y}, Sequence[ColorFunction -> "DeepSeaColors", Exclusions -> None], Contours -> 10, PerformanceGoal -> "Quality", MaxRecursion -> 2, ScalingFunctions -> "Log", PlotLegends -> {LegendFunction -> f}]

enter image description here

MelaGo
  • 8,586
  • 1
  • 11
  • 24
  • Thanks. This for me has the same issue as the other answer that occasionally the last entry is formatted wrong. I am giving up in trying to understand this nonsense. I dont know maybe its my old version of mathematica, will edit the image or whatever. I have accepted the other answer because it came earlier – Bort Oct 21 '23 at 09:05