3
$\begingroup$

I want to plot two functions. One should appear on top of the other. The x axis has the same values for both functions, but should appear twice (the second- upper I want to be dashed). y axis should change to dashed type (when it reaches a little more than the highest value for the first function), start from zero again and have a different frame label. Here is just a simple example how can I present the graphs:

Needs["PlotLegends`"]
Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, 
 PlotStyle -> {Red, {Black, Dashed, Thick}}, 
 BaseStyle -> {FontFamily -> "Times", FontSize -> 16}, 
 Frame -> {True, True, False, True}, 
 FrameLabel -> {"x", "y", "", "y1"}, 
 FrameStyle -> {Directive[18], Directive[18], Directive[18], Directive[18, Dashed, Thick]}, 
 PlotLegend -> {Style["Sin(x)", 14], Style["Cos(x)", 14]}, 
 LegendPosition -> {1.05, 0.1}, LegendSize -> {0.4, 0.3}, 
 LegendShadow -> 0.005, LegendTextSpace -> 4, ImageSize -> 550]

But I don't want the functions to be together. Let's say cos(x) in example should be above sin(x) and y1 should start above the end of y (at value y= 1.2). I also want to have another (dashed) x axis, which is at value y=1.2 (y1=0).

enter image description here

EDIT: Thank you for all answers. I tried all of them and come to my final code:

AA = Plot[barvilo, {\[Lambda], 337, 717}, PlotRange -> {0, 1.8}, 
   PlotStyle -> {Black, Dashed, Thick}, 
   BaseStyle -> {FontFamily -> "Times", FontSize -> 16}, 
   Frame -> {True, True, False, False}, ImageSize -> 1200, 
   FrameLabel -> {"", "Absorbanca [p.e.]"}, 
   FrameStyle -> {Directive[18, Dashed], Directive[18, Dashed]}, 
   PlotLegend -> Style["\[Alpha](N719)", 14], 
   LegendPosition -> {1.1, -0.15}, LegendSize -> {0.3, 0.2}, 
   LegendShadow -> 0.005, LegendTextSpace -> 2, 
   ImagePadding -> {{60, 30}, {50, 30}}];
BB = Plot[{a, b, c, d}, {\[Lambda], 337, 717}, PlotRange -> {0, 1.9}, 
   PlotStyle -> {Red, Orange, Black, Purple}, 
   BaseStyle -> {FontFamily -> "Times", FontSize -> 16}, 
   Frame -> {True, True, False, False}, 
   FrameLabel -> {"Valovna dolžina [nm]", 
     "Spektralna gostota sevanja [W/\!\(\*SuperscriptBox[\(m\), \
\(2\)]\)nm]"}, FrameStyle -> {Directive[18], Directive[18]}, 
   Filling -> {1 -> {{4}, {Lighter[Yellow], Opacity[0.02]}}, 
     4 -> {Axis, {Lighter[Yellow], Opacity[0.06]}}}, 
   ImagePadding -> {{60, 30}, {50, 30}}, 
   PlotLegend -> {Style["AM1,5G", 14], Style["AM1,5G + TrPt", 14], 
     Style["AM1,5G + TrEl", 14], Style["AM1,5G + TrPtEl", 14]}, 
   LegendPosition -> {1, 0.1}, LegendSize -> {0.4, 0.3}, 
   LegendShadow -> 0.005, LegendTextSpace -> 4, ImageSize -> 1200];
izracun = 
  Graphics[Text[
    StyleForm["269,37 W/\!\(\*SuperscriptBox[\(m\), \(2\)]\)", 
     FontSize -> 16], {550, -900}, {-1, -1}]];
izracun1 = 
  Graphics[Text[
    StyleForm["490,22 W/\!\(\*SuperscriptBox[\(m\), \(2\)]\)", 
     FontSize -> 16], {160, -970}, {-1, -1}]];
Show[GraphicsColumn[{AA, BB}, 
  Spacings -> {0, -380}], izracun, izracun1]

I am satisfied with it but I need to save it. Why is it not possible to save it, when I select it (Save selection as)? I can save like this just separate graphs (AA and/or BB). When I tried the code in the link bellow, the result is in the right form to save with Save graphic as. But this code is to complicated for me. https://mathematica.stackexchange.com/a/6882/6357

I can use print screen option .. Is there a better way to save it?

$\endgroup$
9
  • $\begingroup$ What version of Mma are you using? Version 9 includes PlotLegends. Anyway, just plot each plot separately, as in AA = Plot[ blah ...]; BB = Plot[bleh, ...]; Show[AA,BB] $\endgroup$
    – wolfies
    Apr 2, 2013 at 17:56
  • 1
    $\begingroup$ I guess it will be better if you show a plot with your desired result format $\endgroup$ Apr 2, 2013 at 18:04
  • $\begingroup$ @wolfies It's not what I want, belisarius I think I can not add a picture .. $\endgroup$
    – Luka
    Apr 2, 2013 at 18:06
  • $\begingroup$ What does "One should appear on top of the other" mean? $\endgroup$
    – DavidC
    Apr 2, 2013 at 18:08
  • $\begingroup$ @Luka Upload the image elsewhere and we shall paste it here $\endgroup$ Apr 2, 2013 at 18:08

3 Answers 3

4
$\begingroup$

Similar to the other answers, you need Grid or GraphicsGrid or their Column equivalent. Grid has the advantage of being more flexible, especially around inter-item spacing; GraphicsGrid forces items to be the same width.

Grid[{{Plot[Sin[x], {x, 0, 2 Pi}, AxesStyle -> Dashed, 
    AxesOrigin -> {0, -1}, 
    ImagePadding -> {{20, 20}, {0, 10}}]}, {Plot[Cos[x], {x, 0, 2 Pi},
     AxesOrigin -> {0, -1}, PlotRange -> {-1, 1.2}, 
    FrameLabel -> {{"x"}, {"y", None}}, 
    ImagePadding -> {{20, 20}, {10, 0}}]}}, Spacings -> {0.1, -0.2}]

enter image description here

Notice that the ImagePadding option is set so that the bottom of the top graph and the top of the bottom graph butt up against each other, but I've left space for the tick labels on other sides.

If you are determined to have the labels sitting in the positions you showed in your drawing, then you will need to use Frame as per Jagra's answer. If I used AxesLabel instead, the y-axis label would be above the axis line, which isn't going to be visible for the lower graph.

You will probably also want to change the setting for Ticks on the x-axis of the upper graph.

You might be interested in this question which shows how to construct nice grids of graphs that all butt up against each other, for arbitrary numbers of rows and columns in the grid.

$\endgroup$
2
$\begingroup$

Looking for something like this?

Grid[{{Plot[
    Sin[x], {x, 0, 2 \[Pi]},
    PlotStyle -> Red,
    BaseStyle -> {FontFamily -> "Times", FontSize -> 16},
    Frame -> {True, True, False, True},
    FrameLabel -> {"", "y", "", ""},
    FrameTicks -> {True, True, None, True},
    FrameTicksStyle -> {White, Automatic, Automatic, Automatic},
    FrameStyle -> Directive[18],
    PlotLegend -> {Style["Sin(x)", 14]},
    LegendPosition -> {1.05, 0.1},
    LegendSize -> {0.4, 0.3},
    LegendShadow -> 0.005,
    LegendTextSpace -> 4,
    ImageSize -> 550,
    AspectRatio -> .5]},
  {Plot[
    Cos[x], {x, 0, 2 \[Pi]},
    PlotStyle -> {Black, Dashed, Thick},
    BaseStyle -> {FontFamily -> "Times", FontSize -> 16},
    Frame -> True,
    FrameTicks -> {True, True, None, True},
    FrameLabel -> {"x", "y1", "", ""},
    FrameStyle -> Directive[18],
    PlotLegend -> {Style["Cos(x)", 14]},
    LegendPosition -> {1.05, 0.1},
    LegendSize -> {0.4, 0.3},
    LegendShadow -> 0.005,
    LegendTextSpace -> 4,
    ImageSize -> 550,
    AspectRatio -> .5]}},
 Spacings -> {0, -.70}
 ]

enter image description here

I've used the basic idea of simply stacking two charts in a Grid. Column might work as well, but Grid can give you a bit more flexibility with formatting if you add other things to this solution.

You might also try working with ImagePadding too tighten up the distance between the two plots.

$\endgroup$
2
$\begingroup$

I am not sure what funcs you are plotting, but it seems this does the basic idea ...

AA = Plot[Sin[x] + 1/2, {x, 0, Pi}, AxesOrigin -> {0, 0}, AxesLabel -> {"x", "y"}];
BB = Plot[Cos[x] + 1, {x, 0, Pi}, AxesOrigin -> {0, 0}, AxesStyle -> Dashed, PlotStyle -> Red, AxesLabel -> {"x", "y1"}];
GraphicsColumn[{BB, AA}, Spacings -> 0]

enter image description here

You can modify the axis labelling and tick marks as you desire, for each plot separately. You can also set, Spacings to be negative, e.g. Spacings -> -10, which will make the vertical axes merge into each other :)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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