Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have two data sets, data1 and data2. For example:

data1 = {{1, 1.1}, {2, 1.5}, {3, 0.9}, {4, 2.3}, {5, 1.1}};
data2 = {{1, 1001.1}, {2, 1001.5}, {3, 1000.9}, {4, 1002.3}, {5, 1001.1}};
ListPlot[data1, PlotRange -> All, Joined -> True, Mesh -> Full, PlotStyle -> Red]
ListPlot[data2, PlotRange -> All, Joined -> True, Mesh -> Full, PlotStyle -> Blue]

ListPlot 1

Their $y$-values are in vastly different regimes, but their oscillations in $y$ are comparable, and I'd like to compare them visually using ListPlot. But if I simply overlay them, it is nearly impossible to see and compare their oscillations, because of the scaling:

Show[{
  ListPlot[data1, PlotRange -> {{1, 5}, {-100, All}}, Joined -> True, Mesh -> Full,
     PlotStyle -> Red, AxesOrigin -> {1, -50}],
  ListPlot[data2, Joined -> True, Mesh -> Full, PlotStyle -> Blue]
}]

ListPlot 2

Is there a way to "break" or "snip" the $y$ axis so that I can compare data1 and data2 on the same plot? There is no data in the range ~3 to ~1000, so I would like to snip this $y$-range out, if possible, and perhaps include a jagged symbol to show that this has been done.

share|improve this question
3  
Very very strongly related: mathematica.stackexchange.com/questions/627/1-plot-2-scale-axis –  Ajasja Jul 12 '12 at 15:28
3  
@Ajasja Yes, that is similar, but not identical to this question. In that question, they ask for two different axes/scales, one on each side. I am asking for a snipped axis. While the two approaches accomplish similar things, they are different, since I would like to show how the blue set of data (data2) are above the red set of data (data1). –  Andrew Jul 12 '12 at 15:35
1  
@Andrew Ajasja wasn't the one that voted to close, that was me — I didn't catch the snipped axes requirement at first. I agree that there is a subtle difference and not a duplicate. –  rm -rf Jul 12 '12 at 16:03
1  
I've got a function that does this, on my laptop at home. Will post this evening (although somebody will surely beat me to it :)) –  JxB Jul 12 '12 at 16:16
1  
Also, W|A does a snipped axis (see, e.g. this). I wonder if it'll be included as an option in MMA 9? –  Eli Lansey Jul 13 '12 at 17:14

2 Answers 2

up vote 27 down vote accepted

Here is a solution that uses a BezierCurve to indicate a "snipped" axes. The function snip[x] places the mark on the axes at relative position x (0 and 1 being the ends). The function getMaxPadding gets the maximum padding on all sides for both plots (based on this answer). The two plots are then aligned one over the other, with the max padding applied for both.

snip[pos_] := Arrowheads[{{Automatic, pos, 
     Graphics[{BezierCurve[{{0, -(1/2)}, {1/2, 0}, {-(1/2), 0}, {0, 1/2}}]}]}}];
getMaxPadding[p_List] := Map[Max, (BorderDimensions@
    Image[Show[#, LabelStyle -> White, Background -> White]] & /@ p)~Flatten~{{3}, {2}}, {2}] + 1
p1 = ListPlot[data1, PlotRange -> All, Joined -> True, Mesh -> Full, PlotStyle -> Red, 
    AxesStyle -> {None, snip[1]}, PlotRangePadding -> None, ImagePadding -> 30];
p2 = ListPlot[data2, PlotRange -> All, Joined -> True, Mesh -> Full, PlotStyle -> Blue, 
    Axes -> {False, True}, AxesStyle -> {None, snip[0]}, PlotRangePadding -> None, ImagePadding -> 30];

Column[{p2, p1} /. Graphics[x__] :> 
    Graphics[x, ImagePadding -> getMaxPadding[{p1, p2}], ImageSize -> 400]]

enter image description here

share|improve this answer
4  
Nice one, oh hypnotoad! –  Yves Klett Jul 12 '12 at 16:35
3  
@YvesKlett it sort of forces you to upvote him, doesn't it? All hail the Hypnotoad! –  rcollyer Jul 12 '12 at 17:58
2  
@rcollyer well ,here goes: ALL GLORY TO THE HYPNOTOAD! –  Yves Klett Jul 12 '12 at 19:29
2  
R.M. this looks nice but (for me) it only works at one particular size since if I resize the graphic it overlaps incorrectly. I'm not sure if that's what you're referring to at the bottom of the answer. Either way, can you fix it? –  Mr.Wizard Jul 13 '12 at 5:40
1  
@Mr.Wizard That's more an indictment of GraphicsColumn, which overlaps (eventually) when resizing if a particular negative spacing is set (alternately, widens for positive spacing). For example, try: GraphicsColumn[{#, #}, Spacings -> -10] &@Plot[, {x, 0, 1}]. I've used Column now, and you shouldn't have this issue if you use the ImageSize option to set the size –  rm -rf Jul 13 '12 at 15:13

This solution shifts data around and makes new ticks for the y axis.

compressYAxis[plot_,plotRange1_,plotRange2_] will modify the y axis of the supplied plot to exclude the region between the upper limit of plotRange1 and the lower limit of plotRange2. With your data, here is the plot with a compressed y axis:

data1 = {{1, 1.1}, {2, 1.5}, {3, 0.9}, {4, 2.3}, {5, 1.1}};
data2 = {{1, 1001.1}, {2, 1001.5}, {3, 1000.9}, {4, 1002.3}, {5, 1001.1}};

p = ListLinePlot[{data1, data2}, PlotRange -> All, 
 PlotLabel -> "Example of a compressed y axis", 
 AxesLabel -> {"x", "y"}];

compressYAxis[p, {0, 3}, {999, 1003}]

Mathematica graphics

You will have to fiddle with this if you want tick subdivisions or a different background colour; the compression marks could be improved too.

The definition is

Clear[compressYAxis];
compressYAxis[plot_, range1_, range2_] := 
  Module[{ytick1, ytick2, epilog1, target},
   ytick1 = FindDivisions[range1, 5] /. y_?NumericQ :> {y, y} /. {y_?NumericQ, _} /; y >= range1[[2]] :> Sequence[];
   ytick2 = FindDivisions[range2, 5] /. y_?NumericQ :> {y - range2[[1]] + range1[[2]], y} /. {y_?NumericQ, _} /; y <= range1[[2]] :> Sequence[];
   epilog = Options[plot, Epilog][[1, 2]];
   target = Subtract @@ Reverse@range1/(Subtract @@ Reverse@range1 + Subtract @@ Reverse@range2);
   Show[plot /. {x_?NumericQ, y_?NumericQ /; y > range2[[1]]} :> {x, y - range2[[1]] + range1[[2]]}, 
     PlotRange -> {range1[[1]], range1[[2]] + Subtract @@ Reverse@range2}, 
     Ticks -> {Automatic, Join[ytick1, ytick2]}, 
     Epilog -> Join[epilog, {White, Rectangle[Scaled[{-0.1, 0.98 target}], Scaled[{1.1, 1.02 target}]], Black, Text[Rotate["\\", \[Pi]/2], Scaled[{0, 0.98 target}], {-1.5, 0}], Text[Rotate["\\", \[Pi]/2], Scaled[{0, 1.02 target}], {-1.5, 0}]}]]
 ]
share|improve this answer
1  
@Andrew As promised. It's hawkish, and needs Mma v7 or greater. This approach could work quite nicely if (once?) AbsoluteOptions is fixed so that it returns suitable values for Ticks. As it stands, R.M's solution looks more robust. –  JxB Jul 13 '12 at 4:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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