# Contour Plot with 2 Legends

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).

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

• "Someone posted a picture of a plot on here but didn't describe how they generated it." - could you give a link, please? Aug 11, 2017 at 16:58
• mathematica.stackexchange.com/questions/29186/… Someone asked how the user generated it but didn't receive a reply, unfortunately. Aug 11, 2017 at 17:02
• The one who posted that answer actually used a commercial package; anyway, let's see how we can do it with plain Mathematica. Aug 11, 2017 at 17:04
• 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"] Aug 11, 2017 at 17:14
• 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. Aug 11, 2017 at 17:38

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

Example:

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];

ContourPlot[
(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]
}]
]


Output:

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.

Example

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"}]
}
}]