8
$\begingroup$

I have a series of ContourPlots that I want to compare in a GraphicsRow environment. I want to add a single legend for all three plots. Also, I really like the standard color scheme of the ContourPlot.

How can I use the standard ColorFunction from ContourPlot in a different Graphics object?

I did not find the ColorFunction that ContourPlot uses inside ColorData["Gradients"].

A minimal working example would be this line

Legended[ContourPlot[x y, {x, 0, 1}, {y, 0, 1}],
         Placed[BarLegend[{Automatic, {0, 1}}, LegendLayout -> "Row"], Below]]

Mathematica graphics

where I'm done when the BarLegend shows the same colors as the ContourPlot.

$\endgroup$
1
  • $\begingroup$ For reference: Sort[DataPaclets`ColorDataDump`gradientSchemeNames] gives a complete list of all the available gradients. $\endgroup$ Jun 9, 2016 at 7:26

2 Answers 2

12
$\begingroup$
Legended[ContourPlot[x y, {x, 0, 1}, {y, 0, 1}],
 Placed[BarLegend[{ColorData["M10DefaultDensityGradient"], {0, 1}}, 
   LegendLayout -> "Row"], Below]]

Mathematica graphics

I found the name of the color scheme by evaluating

plot = ContourPlot[x y, {x, 0, 1}, {y, 0, 1}, PlotLegends -> Automatic]

Mathematica graphics

and looking at the expression of the result. As Legended accepts two arguments with the second being the legend, this can be easily done by running

plot[[2]] // InputForm
$\endgroup$
4
  • $\begingroup$ Added some code to round out the example. $\endgroup$
    – rcollyer
    Apr 30, 2015 at 14:10
  • $\begingroup$ Thanks, the edits definitely improve the answer. $\endgroup$
    – Gerli
    Apr 30, 2015 at 14:12
  • $\begingroup$ The answer already got a +1 from me. I was just rounding it out. $\endgroup$
    – rcollyer
    Apr 30, 2015 at 14:18
  • $\begingroup$ Thanks for figuring this out! +1 $\endgroup$
    – Neuneck
    May 1, 2015 at 8:18
1
$\begingroup$

Create your plots with the option PlotLegends -> Automatic:

threecontourplots = {plot1, plot2, plot3} = 
 ContourPlot[#, {x, 0, 1}, {y, 0, 1}, PlotLegends -> Automatic, ImageSize -> 300] & /@ 
   {x y, x + y , x};

For a legended plot object plot, you can use Part to extract the main plot (plot1[[1]]) and the legend (plot1[[2,1]]).

threecontourplots[[1, 2, 1]]

enter image description here

You can use the extracted parts with Row, GraphicsRow, Grid,GraphicsGrid as well as with Legended:

Row[Append[threecontourplots[[1, 2, 1]]]@threecontourplots[[All, 1]], 
 Spacer[10]]

enter image description here

Legended[Row[threecontourplots[[All, 1]], Spacer[10]], 
 Placed[threecontourplots[[1, 2, 1]], Right]]

enter image description here

If you use a non-default color function in ContourPlot (say, ColorFunction -> "Rainbow"`) no modification will be needed:

`enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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