How do I get the colors to match the plot theme when I use a PlotLegend
that is separate to the plots? I have had a look at How to access new colour schemes in version 10? but can't seem to find what I need. Here is some data and an array of plots.
Initialization
data1 =
{Table[{x, Sin[x]}, {x, 0, 2 π, 0.1}],
Table[{x, Sin[2 x]}, {x, 0, 2 π, 0.1}],
Table[{x, Sin[3 x]}, {x, 0, 2 π, 0.1}]};
data2 =
{Table[{x, BesselJ[1, x]}, {x, 0, 2 π, 0.1}],
Table[{x, BesselJ[2, 2 x]}, {x, 0, 2 π, 0.1}],
Table[{x, BesselJ[3, 3 x]}, {x, 0, 2 π, 0.1}]};
data3 =
{Table[{x, BesselJ[4, x]}, {x, 0, 2 π, 0.1}],
Table[{x, BesselJ[5, 2 x]}, {x, 0, 2 π, 0.1}],
Table[{x, BesselJ[6, 3 x]}, {x, 0, 2 π, 0.1}]};
data4 =
{Table[{x, BesselJ[7, x]}, {x, 0, 2 π, 0.1}],
Table[{x, BesselJ[8, 2 x]}, {x, 0, 2 π, 0.1}],
Table[{x, BesselJ[9, 3 x]}, {x, 0, 2 π, 0.1}]};
opts =
{Frame -> True, Axes -> False,
FrameLabel -> {"Time/s", "Pressure/Pa"}, ImageSize -> 280,
BaseStyle -> {FontFamily -> "Times", FontSize -> 12},
PlotTheme -> "Scientific"};
Result
Column[
{Row[
{Style["Data from experiment 5B", FontFamily -> "Times",
FontSize -> 12]}, Alignment -> Center, ImageSize -> (4 + 4 + 0.5) 72],
Row[
{ListLinePlot[data1, opts], Spacer[0.5 72], ListLinePlot[data2, opts]}],
Row[
{ListLinePlot[data3, opts], Spacer[0.5 72], ListLinePlot[data4, opts]}],
Row[
{LineLegend[{Red, Blue, Green},
{"x-direction", "y-direction ", "z-direction"},
LegendLayout -> "Row", LabelStyle -> {FontFamily -> "Times", FontSize -> 12}]},
Alignment -> Center, ImageSize -> (4 + 4 + 0.5) 72]}
]
My problem is that I have the wrong colors in the caption. How do I pick up the colors of the PlotTheme? I am using rows and columns rather than GraphicsGrid
because I need to use the "Get Coordinates" feature using the right mouse click. Graphics
grid does not permit this for individual graphs.
Answer
Use
colors=(("DefaultPlotStyle"/.(Method /.
Charting`ResolvePlotTheme["Scientific" , ListLinePlot]))/. Directive[x_,__]:>x)
to get the colors used in the "Scientific"
plot theme. Then use colors
as the first argument of LineLegend
:
Column[{
Row[{Style["Data from experiment 5B", FontFamily -> "Times",
FontSize -> 12]}, Alignment -> Center,
ImageSize -> (4 + 4 + 0.5) 72],
Row[{ListLinePlot[data1, opts], Spacer[0.5 72],
ListLinePlot[data2, opts]}],
Row[{ListLinePlot[data3, opts], Spacer[0.5 72],
ListLinePlot[data4, opts]}],
Row[{LineLegend[colors, {"x-direction", "y-direction ",
"z-direction"}, LegendLayout -> "Row",
LabelStyle -> {FontFamily -> "Times", FontSize -> 12}]},
Alignment -> Center, ImageSize -> (4 + 4 + 0.5) 72]
}]
Comments
Post a Comment