Combining LineLegend and BarLegend in CountourPlot
PlotLegends
can take a list of legends:
ContourPlot[{Cos[y + x] x y}, {x, 0, 3}, {y, 0, 3},
PlotRange -> All, PlotLegends -> {
BarLegend[Automatic],
Placed[LineLegend[{Directive[Red]}, {"Test"}], After]
},
Epilog -> {{Red, Line[Table[{i, Cos[i]}, {i, 0., 3., .1}]]}}]
By default one legend will go to the right, and one below, so I used Placed
to put both on the right.
Here is a work-around using separate ContourPlot
s:
p1 = ContourPlot[{Cos[y + x] x y}, {x, 0, 3}, {y, 0, 3},
PlotRange -> All,
PlotLegends -> BarLegend[Automatic]];
p2 = ContourPlot[y == Cos[x], {x, 0, 3}, {y, 0, 3},
ContourStyle -> Red,
PlotLegends -> LineLegend[{"Test"}]];
Show[p1, p2]
Will this be ok?
Each plot type allows only specific legends to be used with PlotLegends
, for ContourPlot
and DensityPlot
it is BarLegend
. So, it is getting confused when you add LineLegend
. (Per Marius, there is a switchover to LineLegend
when only specific contours are used, e.g. f == g
form of input to ContourPlot
.)
The correct way is to use Legended
to add the additional legend:
Legended[
ContourPlot[{Cos[y + x] x y}, {x, 0, 3}, {y, 0, 3},
PlotRange -> All, PlotLegends -> BarLegend[Automatic],
Epilog -> {{Red, Line[Table[{i, Cos[i]}, {i, 0., 3., .1}]]}}]
,
LineLegend[{Directive[Red]}, {"Test"}]
]