5
$\begingroup$

We have two plots.

g1 = Plot[{1 - 2 x, 1 - x}, {x, 0, 2}, PlotRange -> {0, 2}];
g2 = Plot[ 1 - 2 x,         {x, 0, 2}, PlotRange -> {0, 2}];
Grid[{
  {g1, g2}
  }]

This gives something like: enter image description here

What I need is something like the following (same scale but different range of horisontal axes): enter image description here

It is important to keep the same plot Range. And it would be nice to have it automatically, not playing manually with inches. Grid is not obligatory if there is more convenient way.

That sort of graphics often arises in economics when so called 'horizontal summation' takes place, i.e. demand function in essence is of the form x[y] in normal terms.

$\endgroup$
6
$\begingroup$

You can add AspectRatio -> Automatic, ImageSize -> {Automatic, height} for both plots:

opts = {PlotRange -> {0, 2}, AspectRatio -> Automatic, 
   ImageSize -> {Automatic, 200}};
g1 = Plot[{1 - 2 x, 1 - x}, {x, 0, 2}, Evaluate@opts];
g2 = Plot[1 - 2 x, {x, 0, 1}, Evaluate@opts];
Grid[{{g1, g2}}]

enter image description here

Then one can play around with ticks.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Oh, AspectRatio -> Automatic. How did I miss that. $\endgroup$ – Edmund Nov 15 '15 at 14:32
  • $\begingroup$ @Edmund It simple to miss because here the default value is not Automatic. $\endgroup$ – ybeltukov Nov 15 '15 at 15:18
4
$\begingroup$

The AspectRatio is determining the size of the axis in the plots. These can be set for each plot along with the ImageSize for the results you are seeking. Also, the PlotRange needs to set the x-axis separate from the y-axis for the second plot.

AspectRatio is a $\frac{\text{height}}{\text{width}}$ ratio so for the first plot we need $\frac{2}{2}$ and for the second we need $\frac{2}{1}$. For ImageSize we only need to match the heights since the AspectRatio will take care of the widths. Finally, the PlotRange of the second plot is set so that it matches the AspectRatio to the scale desired. Here the scale is 1:1 for these.

g1 = Plot[{1 - 2 x, 1 - x}, {x, 0, 2}, PlotRange -> {0, 2}, 
   AspectRatio -> 1, ImageSize -> {Automatic, 200}];
g2 = Plot[1 - 2 x, {x, 0, 2}, PlotRange -> {{0, 1}, {0, 2}}, 
   AspectRatio -> 2, ImageSize -> {Automatic, 200}];
Grid[{{g1, g2}}]

enter image description here

The ticks on the second plot are a bit bunched but can be set with Ticks if needed.

xticks = If[Mod[#, .5] == 0, {#, ToString@#, .04}, {#, ""}] & /@ 
  Range[0, 2, 0.1]

g1 = Plot[{1 - 2 x, 1 - x}, {x, 0, 2}, PlotRange -> {0, 2},
   Ticks -> {xticks, Automatic},
   AspectRatio -> 1, ImageSize -> {Automatic, 200}];
g2 = Plot[1 - 2 x, {x, 0, 2}, PlotRange -> {{0, 1}, {0, 2}}, 
   AspectRatio -> 2,
   Ticks -> {xticks, Automatic}, ImageSize -> {Automatic, 200}];
Grid[{{g1, g2}}]

enter image description here

You can also use GraphicsGrid instead of Grid.

| improve this answer | |
$\endgroup$
  • $\begingroup$ why are 'main' ticks on the right plot shorter than on the left? $\endgroup$ – garej Nov 15 '15 at 16:34
  • $\begingroup$ it seems like AspectRatio influences ticks marks size... $\endgroup$ – garej Nov 15 '15 at 16:51
  • $\begingroup$ @garej Tick mark size is uses Scaled by default and this is a function of the width of the graphic. I was trying to get the labeled ticks higher than the unlabeled ones. You don't have to specify tick size. $\endgroup$ – Edmund Nov 15 '15 at 18:36
1
$\begingroup$

Overlay second plot with white Rectangle

g1 =
 Plot[
  {1 - 2 x, 1 - x}, {x, 0, 2},
  GridLines -> Automatic,
  ImageSize -> 400,
  PlotLegends -> Placed["Expressions", Above],
  PlotRange -> {0, 2}];

g2 =
 Plot[1 - 2 x, {x, 0, 2},
  Epilog -> {White, Rectangle[{1., -0.1}, {2, 2.1}]},
  GridLines -> {{0.5, 1}, Automatic},
  ImageSize -> 400,
  PlotRange -> {0, 2},
  Ticks -> {Transpose[{#, PaddedForm[#, {2, 1}] & /@ #}] &[{0., 0.5, 1.}], Automatic}];

Grid[{{g1, g2}}]

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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