3
$\begingroup$

Simple question with a (probably) complicated answer: How can I include the ClippingStyle colors of a ContourPlot in its BarLegend?

Take for example this plot (I know, some of the options are superfluous in this simple example, but these are essentially the options I will use later):

ContourPlot[Log[x y], {x, 0, 3}, {y, 0, 3}, PlotRange -> {0, 1}, Contours -> 9, ClippingStyle -> {Black, White}, ColorFunctionScaling -> False, ColorFunction -> ColorData["WL13DefaultDensityGradient"], PlotLegends -> BarLegend[{"WL13DefaultDensityGradient", {0, 1}}, 9] ] 

Evidently, Black and White are missing from the legend. How can they, and the corresponding labels 0 and 1 be included in the legend? Further requirements:

  • I do not want a continuous color gradient in the bar legend
  • it should work for different PlotRanges and for different number of Contours

Is there any solution that does not involve implementing a completely new legend from scratch? I thought about redefining the ColorDataFunction, but the documentation doesn't give any insights on how to do that...

Many thanks already!

$\endgroup$
1
  • $\begingroup$ If you add the clipping colors to the bar legend then the plot is misleading. You got an impression that values of function in black area are in range (-0.1, 0) and in white area are in range (1, 1.1) which is not true. Because of this Bob Hanlon's answer is better. $\endgroup$ Commented Mar 7 at 19:26

2 Answers 2

3
$\begingroup$

Something like this perhaps:

 cf[val_, bounds : {low_, high_}, clipColors : {clow_, cup_}] := Which[ val <= low, clow, val >= high, cup, True, ColorData["WL13DefaultDensityGradient"][val] ] 

and

 ContourPlot[Log[x y], {x, 0, 3}, {y, 0, 3}, PlotRange -> {0, 1}, Contours -> 9, ClippingStyle -> {Black, White}, ColorFunctionScaling -> False, ColorFunction -> ColorData["WL13DefaultDensityGradient"], PlotLegends -> BarLegend[{cf[#, {0, 1}, {Black, White}] &, {-.1, 1.1}}, 11] ] 
$\endgroup$
1
  • $\begingroup$ oh wow, that's a surprisingly simple and elegant solution! $\endgroup$ Commented Mar 7 at 19:02
6
$\begingroup$
$Version (* "14.2.0 for Mac OS X ARM (64-bit) (December 26, 2024)" *) Clear["Global`*"] {zmin, zmax} = {0, 1}; ContourPlot[Log[x y], {x, 0, 3}, {y, 0, 3}, PlotRange -> {zmin, zmax}, FrameLabel -> (Style[#, 14] & /@ {x, y}), Contours -> 9, ClippingStyle -> {Black, White}, ColorFunctionScaling -> False, ColorFunction -> ColorData["WL13DefaultDensityGradient"], PlotLegends -> Column[{ SwatchLegend[{White}, {StringForm["\[GreaterEqual]\[ThinSpace]``", zmax]}], BarLegend[{"WL13DefaultDensityGradient", {0, 1}}, 9], SwatchLegend[{Black}, {StringForm["\[LessEqual]\[ThinSpace]``", zmin]}]}, Spacings -> 0]] 

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.