Consider the following example:

DensityPlot[10^(LogTQ)*10^(LogA),{LogTQ,-2,1},{LogA,2,6},PlotLegends->BarLegend[Automatic,LabelStyle->Directive[30],LegendLabel->"Log10[Subscript[P, C]]"],FrameTicksStyle->Directive[30],FrameLabel->{"Log10[Subscript[T, Q]]","Log10[A]"} ,LabelStyle->Directive[30],PlotRange->All,ColorFunction->"SunsetColors"]

It produces the following:

enter image description here

My question is:

Here I figured out that putting the ColorFunction as option for DensityPlot works. I also tried the following:

DensityPlot[10^(LogTQ)*10^(LogA),{LogTQ,-2,1},{LogA,2,6},PlotLegends->BarLegend["SunsetColors",LabelStyle->Directive[30],LegendLabel->"Log10[Subscript[P, C]]"],FrameTicksStyle->Directive[30],FrameLabel->{"Log10[Subscript[T, Q]]","Log10[A]"} ,LabelStyle->Directive[30],PlotRange->All]

And this time the color bar doesn't has the same color.

My question is: How can I know to who I should pass the parameters about the legend ? I would like a general "conceptual" answer (but for a beginner please !). For instance if I want to choose a min/max value for my legend should I specify it to the DensityPlot or to the BarLegend ?

Again, I would like to understand the "philosophy" behind how it works. I am struggling with too many concept in mathematica.

  • $\begingroup$ There seems to be a bug in BarLegend. You can use instead: PlotLegends ->Automatic. Please make a bug report at: [email protected] $\endgroup$ Sep 30, 2020 at 19:50

1 Answer 1


font = 18;

DensityPlot[LogTQ + LogA, {LogTQ, -2, 1}, {LogA, 2, 6},
 PlotLegends -> BarLegend[{"SunsetColors", {0, 7}},
   LegendLabel -> 
    StringForm["``(``)", Subscript[log, 10], Subscript[P, C]]],
 FrameTicksStyle -> Directive[font],
 FrameLabel ->
  (StringForm["``(``)", Subscript[log, 10], #] & /@
      Q], A}),
 LabelStyle -> Directive[font],
 PlotRange -> All,
 ColorFunction -> "SunsetColors"]

enter image description here

  • $\begingroup$ Thank you for your answer. With respect to my question: why do we have to provide BOTH at BarLegend and ColorFunction the parameter SunsetColors ? What is the "spirit" behind using such functions ? Why only specifying SunsetColors to the BarLegend doesn't work ? How without trial and error can I guess that ? $\endgroup$
    – StarBucK
    Oct 1, 2020 at 10:34
  • $\begingroup$ The plot must either use the default color function (cf) or one that you specify. You choose the latter. Once you also choose to explicitly specify a BarLegend (as opposed to just using PlotLegends -> Automatic), the BarLegend syntax requires that it be provided with a cf. To have the cf match the plot's cf, you can either repeat it or use Automatic, e.g., PlotLegends -> BarLegend[{Automatic, {0, 7}}, LegendLabel -> StringForm["``(``)", Subscript[log, 10], Subscript[P, C]]] The BarLegend can inherit from the plot but never the reverse. $\endgroup$
    – Bob Hanlon
    Oct 1, 2020 at 12:11
  • $\begingroup$ Thanks for the answer. This is the kind of info I am interested in. Is there a general spirit such that functions "inside" other function inherhit from their parents but never the other way around ? It seems to be the case here as you say, but is it general ? I am still struggling to understand the spirit behind mathematica. $\endgroup$
    – StarBucK
    Oct 1, 2020 at 12:16
  • 1
    $\begingroup$ The general spirit is that there can be no ambiguity. If the outer function (parent) could (which it can't) inherit from an option, would Automatic in the outer function refer to its own default or to some inheritance? And what if there were more than one option that it could conceivably inherit from? $\endgroup$
    – Bob Hanlon
    Oct 1, 2020 at 12:34
  • $\begingroup$ Allright, so to avoid ambiguity, the way it is thought is that you inherit from your "closer" parent ? Because, I could also imagine 3 stages instead of two conceptually. The bottom one either has an option (then it takes its value), if is has automatic, it will inherhit from stage 2, which also has either an option, either automatic then it inherhit from stage 3. Would you agree ? Is there an article explaining all this ? $\endgroup$
    – StarBucK
    Oct 1, 2020 at 12:38

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.