3
$\begingroup$

testfig = ContourPlot[Sin[x^2 + y^2], {x, -Pi, Pi}, {y, -Pi, Pi}, PlotLegends -> Automatic];

testfig2 = ContourPlot[Cos[x^2 + y^2], {x, -Pi, Pi}, {y, -Pi, Pi}, PlotLegends -> Automatic]

This produces two plots which have the same legend - not surprising since the plotted function is bounded between $-1$ and $1$.

How can I place the two plots into the same figure and only use one legend? It's easy enough to put them on the same plot using GraphicsGrid, but that yields a figure with two legends.

enter image description here

$\endgroup$

1 Answer 1

7
$\begingroup$
Clear["Global`*"]

size = 250;

testfig = ContourPlot[Sin[x^2 + y^2],
   {x, -Pi, Pi}, {y, -Pi, Pi},
   ImageSize -> size,
   PlotLegends -> Automatic];

testfig2 = ContourPlot[Cos[x^2 + y^2],
   {x, -Pi, Pi}, {y, -Pi, Pi},
   ImageSize -> size];

Row[{testfig, testfig2}]

enter image description here

EDIT: For a legend below

testfig = ContourPlot[Sin[x^2 + y^2], {x, -Pi, Pi}, {y, -Pi, Pi}, 
   ImageSize -> size];

Legended[Row[{testfig, testfig2}],
 Placed[BarLegend[{"M10DefaultDensityGradient", {-1, 1}}, 
   LegendLayout -> "Row"], Below]]

enter image description here

EDIT 2: For larger legend

Legended[Row[{testfig, testfig2}], Placed[
  BarLegend[{"M10DefaultDensityGradient", {-1, 1}},
   Range[-1, 1, 0.2],
   LegendLayout -> "Row",
   LegendMarkerSize -> 500], Below]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Is there a way to position the legend centered on the bottom of the figure? $\endgroup$
    – Allure
    Apr 15, 2021 at 12:32
  • $\begingroup$ @Moo that only places it below one of the contour plots, though. Is there a way to place it below both contour plots? $\endgroup$
    – Allure
    Apr 15, 2021 at 12:57

Your Answer

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

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