I want to redefine the legend contours within a specific region of my plot. Someone posted a picture of a plot on here, but didn't describe how they generated it. I want something similar, where I don't need to differentiate within a large region of my plot (from 10 to 125 and -125 to -10 in the example below), but care about little differences within a certain type of region (the -10 to 10 region in the picture below).

Could anyone please help me how to generate something similar? Thank you!

Just for completeness: I want to have 10 different contours/colors for values between 0 and 2, and 5 different contours/colors between 2 and 20

2 different Legends for one contour plot

  • $\begingroup$ "Someone posted a picture of a plot on here but didn't describe how they generated it." - could you give a link, please? $\endgroup$ Commented Aug 11, 2017 at 16:58
  • 1
    $\begingroup$ mathematica.stackexchange.com/questions/29186/… Someone asked how the user generated it but didn't receive a reply, unfortunately. $\endgroup$
    – Kookiano
    Commented Aug 11, 2017 at 17:02
  • $\begingroup$ The one who posted that answer actually used a commercial package; anyway, let's see how we can do it with plain Mathematica. $\endgroup$ Commented Aug 11, 2017 at 17:04
  • $\begingroup$ This doesn't have two legends, but it might give you a starting point: ContourPlot[x^2 - 3 x y + y^3, {x, -5, 5}, {y, -5, 5}, Contours -> Join[Range[-150, 125, 25], Range[-10, 10, 2]], PlotLegends -> Automatic, ColorFunction -> "TemperatureMap"] $\endgroup$
    – Jason B.
    Commented Aug 11, 2017 at 17:14
  • $\begingroup$ Thanks, this is close to what I'm looking for. Any idea how I can costumize the color ranges in the legend/plot? In your example, the small area of contours between -10 and 10 has the same color, whereas I would love to have different colors there. $\endgroup$
    – Kookiano
    Commented Aug 11, 2017 at 17:38

2 Answers 2


You can supply arbitrary expressions to the PlotLegend option. In this case, you can solve your issue by supplying a Row of BarLegends.


color[h_ /; h < 2] := ColorData["SunsetColors"][1 - h/2];
color[h_ /; h >= 2] := ColorData["GrayTones"][(h - 2)/20];
lowcontours = Range[0, 2, 2/10];
highcontours = Range[2, 20, 18/5];

     (Sin[x]*Cos[y]+1)*10, {x, -5, 5}, {y, -5, 5}
     , ColorFunction -> color
     , ColorFunctionScaling -> False
     , Contours -> Join[lowcontours, highcontours]
     , PlotLegends -> Row[
           { BarLegend[{color[#]&, {0, 20}}]
           , BarLegend[{color[#]&, {0, 2}}, lowcontours]


Example of contour plot with a composite color scaling and two legends.

BarLegend doesn't support the unequally spaced contour levels, and it doesn't guarantee that the contours will be shown if provided. For that, you may have to make your own bar legend using more primitive functions (see also this question)


The example below aims to demonstrate a simple way how to compose a data plot with two or more legends. The plot and the legend used are arbitrary, and serve only as demonstration. These components can be replaced to address personal requirements.


Framed @ Row[{
   BarChart[{Style[1, Red], Style[2, Green], Style[3, Blue]}],
   Row @ {
     SwatchLegend[{Red, Green, Blue}, {"red", "green", "blue"}],
     SwatchLegend[{Red, Green, Blue}, {"1", "2", "3"}]

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.