3
$\begingroup$

I want to have these 3 plots together vertically

P1 = Plot[{0, If[Sin[x π] >= 0, 1000]}, {x, 0, 50}, PlotStyle -> {Directive[White, CapForm["Butt"], Opacity[-1], Thickness[0]], Directive[Red, CapForm["Butt"], Opacity[1000], Thickness[.04]]}, PlotPoints -> 50, MaxRecursion -> 6, AspectRatio -> 1/40, Axes -> {True, False}, Ticks -> {{0, 50}, {0, 0}}] P2 = Plot[{0, If[Cos[x π] >= 0, 1000]}, {x, 50, 100}, PlotStyle -> {Directive[White, CapForm["Butt"], Opacity[-1], Thickness[0]], Directive[Blue, CapForm["Butt"], Opacity[1000], Thickness[.04]]}, AspectRatio -> 1/40, Axes -> {True, False}, Ticks -> {{50, 100}, {0, 0}}] P3 = Plot[{0, If[Sin[x π] < 0, 1000]}, {x, 100, 150}, PlotStyle -> {Directive[White, CapForm["Butt"], Opacity[-1], Thickness[0]], Directive[Green, CapForm["Butt"], Opacity[1000], Thickness[.04]]}, AspectRatio -> 1/40, Axes -> {True, False}, Ticks -> {{100, 150}, {0, 0}}] 

I use this code

Legended[GraphicsGrid[{{P1}, {P2}, {P3}}], Placed[LineLegend[{Red, Blue, Green}, {Style["a", 10], Style["b", 10], Style["c", 10]}, LegendLayout -> "Row"], Below]] 

and I expect the result to be like

enter image description here

But what I see is

enter image description here

How can I save the result of this code as the first picture?

$\endgroup$
1
  • 1
    $\begingroup$ If you add the option ItemAspectRatio->1/10 to your GraphicsGrid it seems to work out. $\endgroup$ Commented Nov 9, 2020 at 19:45

1 Answer 1

3
$\begingroup$

Try Column in place GraphicsGrid.

Legended[Column[{P1, P2, P3}], Placed[LineLegend[{Red, Blue, Green}, {Style["a", 10], Style["b", 10], Style["c", 10]}, LegendLayout -> "Row"], Below]] 

I get:

stacked plots

Update: Use ImageSizeMultipliers style option

Normally, display functions such as Column, Grid and Row automatically reduce the size of graphics. However, there's a simple option, ImageSizeMultipliers, that adjusts the size of the graphics. I've added BaseStyle -> ImageSizeMultipliers -> {1., 1.}] to increase the size of the graphics.

Legended[ Column[{P1, P2, P3}, BaseStyle -> ImageSizeMultipliers -> {1., 1.}], Placed[LineLegend[{Red, Blue, Green}, {Style["a", 10], Style["b", 10], Style["c", 10]}, LegendLayout -> "Row"], Below]] 

with ImageSizeMultipliers option

Using GraphicsGrid

Alternatively, some modifications to the plot options: add PlotRange, adjust If values, (Opacity value is 0 to 1), set AspectRatio to 1/20. Then add Show[P1, ImageSize -> Full] to GraphicsGrid.

P1 = Plot[{0, If[Sin[x \[Pi]] >= 0, 1]}, {x, 0, 50}, PlotStyle -> {Directive[White, CapForm["Butt"], Opacity[1], Thickness[0]], Directive[Red, CapForm["Butt"], Opacity[1], Thickness[.04]]}, PlotPoints -> 50, MaxRecursion -> 6, PlotRange -> {1, 1.1}, AspectRatio -> 1/20, Axes -> {True, False}, Ticks -> {{0, 50}, {0, 0}}]; P2 = Plot[{0, If[Cos[x \[Pi]] >= 0, 1]}, {x, 50, 100}, PlotStyle -> {Directive[White, CapForm["Butt"], Opacity[1], Thickness[0]], Directive[Blue, CapForm["Butt"], Opacity[1], Thickness[.04]]}, PlotRange -> {1, 1.1}, AspectRatio -> 1/20, Axes -> {True, False}, Ticks -> {{50, 100}, {0, 0}}]; P3 = Plot[{0, If[Sin[x \[Pi]] < 0, 1]}, {x, 100, 150}, PlotStyle -> {Directive[White, CapForm["Butt"], Opacity[1], Thickness[0]], Directive[Green, CapForm["Butt"], Opacity[1], Thickness[.04]]}, PlotRange -> {1, 1.1}, AspectRatio -> 1/20, Axes -> {True, False}, Ticks -> {{100, 150}, {0, 0}}]; Legended[GraphicsGrid[{{Show[P1, ImageSize -> Full]}, {Show[P2, ImageSize -> Full]}, {Show[P3, ImageSize -> Full]}}], Placed[LineLegend[{Red, Blue, Green}, {Style["a", 12], Style["b", 12], Style["c", 12]}, LegendLayout -> "Row"], Below]] 

stacked plot revised

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.