Guest User

Untitled

a guest
Jan 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. data1 = {{1, 1.1}, {2, 1.5}, {3, 0.9}, {4, 2.3}, {5, 1.1}};
  2. data2 = {{1, 1001.1}, {2, 1001.5}, {3, 1000.9}, {4, 1002.3}, {5, 1001.1}};
  3. ListPlot[data1, PlotRange -> All, Joined -> True, Mesh -> Full, PlotStyle -> Red]
  4. ListPlot[data2, PlotRange -> All, Joined -> True, Mesh -> Full, PlotStyle -> Blue]
  5.  
  6. Show[{
  7. ListPlot[data1, PlotRange -> {{1, 5}, {-100, All}}, Joined -> True, Mesh -> Full,
  8. PlotStyle -> Red, AxesOrigin -> {1, -50}],
  9. ListPlot[data2, Joined -> True, Mesh -> Full, PlotStyle -> Blue]
  10. }]
  11.  
  12. snip[pos_] := Arrowheads[{{Automatic, pos,
  13. Graphics[{BezierCurve[{{0, -(1/2)}, {1/2, 0}, {-(1/2), 0}, {0, 1/2}}]}]}}];
  14. getMaxPadding[p_List] := Map[Max, (BorderDimensions@
  15. Image[Show[#, LabelStyle -> White, Background -> White]] & /@ p)~Flatten~{{3}, {2}}, {2}] + 1
  16. p1 = ListPlot[data1, PlotRange -> All, Joined -> True, Mesh -> Full, PlotStyle -> Red,
  17. AxesStyle -> {None, snip[1]}, PlotRangePadding -> None, ImagePadding -> 30];
  18. p2 = ListPlot[data2, PlotRange -> All, Joined -> True, Mesh -> Full, PlotStyle -> Blue,
  19. Axes -> {False, True}, AxesStyle -> {None, snip[0]}, PlotRangePadding -> None, ImagePadding -> 30];
  20.  
  21. Column[{p2, p1} /. Graphics[x__] :>
  22. Graphics[x, ImagePadding -> getMaxPadding[{p1, p2}], ImageSize -> 400]]
  23.  
  24. data1 = {{1, 1.1}, {2, 1.5}, {3, 0.9}, {4, 2.3}, {5, 1.1}};
  25. data2 = {{1, 1001.1}, {2, 1001.5}, {3, 1000.9}, {4, 1002.3}, {5, 1001.1}};
  26.  
  27. p = ListLinePlot[{data1, data2}, PlotRange -> All,
  28. PlotLabel -> "Example of a compressed y axis",
  29. AxesLabel -> {"x", "y"}];
  30.  
  31. compressYAxis[p, {0, 3}, {999, 1003}]
  32.  
  33. Clear[compressYAxis];
  34. compressYAxis[plot_, range1_, range2_] :=
  35. Module[{ytick1, ytick2, epilog1, target},
  36. ytick1 = FindDivisions[range1, 5] /. y_?NumericQ :> {y, y} /. {y_?NumericQ, _} /; y >= range1[[2]] :> Sequence[];
  37. ytick2 = FindDivisions[range2, 5] /. y_?NumericQ :> {y - range2[[1]] + range1[[2]], y} /. {y_?NumericQ, _} /; y <= range1[[2]] :> Sequence[];
  38. epilog = Options[plot, Epilog][[1, 2]];
  39. target = Subtract @@ Reverse@range1/(Subtract @@ Reverse@range1 + Subtract @@ Reverse@range2);
  40. Show[plot /. {x_?NumericQ, y_?NumericQ /; y > range2[[1]]} :> {x, y - range2[[1]] + range1[[2]]},
  41. PlotRange -> {range1[[1]], range1[[2]] + Subtract @@ Reverse@range2},
  42. Ticks -> {Automatic, Join[ytick1, ytick2]},
  43. 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}]}]]
  44. ]
  45.  
  46. ClearAll[sf, isf, inset]
  47. sf[t1_, t2_, gap_: 1/10][x_] := Piecewise[{{x, x <= t1}, {t1 + gap/(t2 - t1) (x - t1),
  48. t1 <= x <= t2}, {t1 + gap + (x - t2), x >= t2}}]
  49. isf[t1_, t2_, gap_: 1/10][x_] := InverseFunction[sf[t1, t2, gap]][x]
  50. head = Graphics[{EdgeForm[None], FaceForm[White],
  51. Polygon[{{-1, -1/6}, {1, 5/6}, {1, 1/6}, {-1, -5/6}}], Black,
  52. CapForm["Butt"], AbsoluteThickness[1],
  53. Line[{{{-1, -5/6}, {1, 1/6}}, {{-1, -1/6}, {1, 5/6}}}]}];
  54. inset[pos_: Scaled[{0.005, .55}], size_: {1/3, 1/3}] := Inset[head, pos, Automatic, size]
  55.  
  56.  
  57. {t1, t2} = {Ceiling[#[[1, 2]]], Floor[#[[2, 1]]]} &@(CoordinateBounds[#][[2]]&/@ {data1, data2});
  58. {yrange1, yrange2} = {Floor[#, .5], Ceiling[#2, .5]} & @@@
  59. (CoordinateBounds[#][[2]] & /@ {data1, data2});
  60. ticks = Join[Charting`FindTicks[{0, 1}, {0, 1}][## & @@ yrange1],
  61. Charting`FindTicks[{0, 1}, {0, 1}][## & @@ yrange2]];
  62.  
  63. ListLinePlot[{data1, data2}, PlotStyle -> Thick,
  64. ScalingFunctions -> {"Linear", {sf[t1, t2], isf[t1, t2]}},
  65. Ticks -> {Automatic, ticks}, PlotRangeClipping -> False,
  66. Epilog -> inset[], ImageSize -> Medium, AspectRatio -> Automatic]
  67.  
  68. ListLinePlot[{data1, data2}, PlotStyle -> Thick,
  69. ScalingFunctions -> {"Linear", {sf[t1, t2], isf[t1, t2]}},
  70. Ticks -> {Automatic, ticks}, PlotRangeClipping -> False,
  71. ImageSize -> Medium, AspectRatio -> Automatic,
  72. AxesStyle -> {Automatic, Arrowheads[{{.03, .55, MapAt[
  73. {Antialiasing -> True, GeometricTransformation[#, RotationTransform[-Pi/2]]}&, head, {1}]}}]}]
  74.  
  75. ClearAll[snip2, inset2]
  76. head2 = Graphics[{Antialiasing -> True, FaceForm[White],
  77. Rectangle[{-1/3, -1/2}, {2/3, 1/2}],
  78. {#, Translate[#, {1/2, 0}]} &@BezierCurve[{{0, -(1/2)}, {1/2, 0}, {-(1/2), 0}, {0, 1/2}}]}];
  79. snip2[pos_] := Arrowheads[{{Automatic, pos, head2}}];
  80. inset2[pos_: Scaled[{0.005, .55}], size_: {1/3, 1/3}] := Inset[
  81. MapAt[GeometricTransformation[#, RotationTransform[Pi/2]] &, head2, {1}], pos, Automatic, size]
  82.  
  83. ListLinePlot[{data1, data2}, PlotStyle -> Thick,
  84. ScalingFunctions -> {"Linear", {sf[t1, t2], isf[t1, t2]}},
  85. Ticks -> {Automatic, ticks}, PlotRangeClipping -> False,
  86. Epilog -> inset2[], ImageSize -> Medium, AspectRatio -> Automatic]
  87.  
  88. ListLinePlot[{data1, data2}, PlotStyle -> Thick,
  89. ScalingFunctions -> {"Linear", {sf[t1, t2], isf[t1, t2]}},
  90. Ticks -> {Automatic, ticks}, PlotRangeClipping -> False,
  91. AxesStyle -> {Automatic, snip2[.55]}, ImageSize -> Medium,
  92. AspectRatio -> Automatic]
Add Comment
Please, Sign In to add comment