MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to plot with a reversed Y-axis?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg103521] Re: How to plot with a reversed Y-axis?
  • From: Armand Tamzarian <mike.honeychurch at gmail.com>
  • Date: Sat, 26 Sep 2009 06:13:36 -0400 (EDT)
  • References: <h9i47h$9dh$1@smc.vnet.net>

On Sep 25, 4:58 am, Mather <sdzbb... at hotmail.com> wrote:
> Hi,guys
> I want to plot with a reversed Y-axis, that is to say, along the Y-
> axis direction the numbers become smaller.
> For example:
>
> ListLinePlot[Table[{i, i^2}, {i, {1, 2, 3, 4, 5, 6}}],
>  PlotRange -> {{0, 8}, {50, 0}}]
>
> However it does not work.
> Can anybody help with this?
>
> Thanks in advance!


Clear[ReverseListPlot];

ReverseListPlot::usage =
  "ReverseListPlot[{list1, list2, \[Ellipsis]}, options] will \
generate a ListLinePlot with the \!\(\*FormBox[\"x\",
TraditionalForm]\), \!\(\*FormBox[\"y\",
TraditionalForm]\), or both \!\(\*FormBox[\"x\",
TraditionalForm]\) and \!\(\*FormBox[\"y\",
TraditionalForm]\), axes reversed. ReverseAxis \[Rule] \"X\", \
ReverseAxis \[Rule] \"Y\", ReverseAxis \[Rule] \"XY\" are specific \
options. Other options are the same as for ListLinePlot.";

SyntaxInformation[
   ReverseListPlot] = {"ArgumentsPattern" -> {_, OptionsPattern[]}};

Options[ReverseListPlot] =
  Join[Options[ListLinePlot], {ReverseAxis -> "X"}];

ReverseListPlot[data__List, opts : OptionsPattern[]] :=
 Module[{p1, frameticks, grids, ticks, opts1, reverseOpts, data1,
   newXGrids, newYGrids, newXTicks, newXBottomTicks, newXTopTicks,
   newYTicks, newYLeftTicks, newYRightTicks},

  (* make framed plot otherwise make axes plot.
  If Axes and Frame are both True no Ticks are produced in \
ListLinePlot--only FrameTicks are produced,
  so need to produce these separately *)

  If[OptionValue[Frame] == True,
   p1 = ListLinePlot[data, Axes -> False, Frame -> True,
     GridLines -> OptionValue[GridLines],
     FrameTicks -> OptionValue[FrameTicks],
     FrameTicksStyle -> OptionValue[FrameTicksStyle],
     PlotRange -> OptionValue[PlotRange]];
   frameticks = AbsoluteOptions[p1, FrameTicks][[1, 2]];
   ticks = {{}, {}};,
   p1 = ListLinePlot[data, Axes -> True, Frame -> False,
     GridLines -> OptionValue[GridLines],
     PlotRange -> OptionValue[PlotRange], Ticks -> OptionValue[Ticks],
      TicksStyle -> OptionValue[TicksStyle]];
   ticks = AbsoluteOptions[p1, Ticks][[1, 2]];
   frameticks = {{}, {}, {}, {}};
   ];

  grids = AbsoluteOptions[p1, GridLines][[1, 2]];
  opts1 = FilterRules[{opts}, Options[ListLinePlot]];
  reverseOpts =
   FilterRules[Options[ReverseListPlot], Options[ListLinePlot]];

  Switch[OptionValue[ReverseAxis],
   "X",
   (* using map in case of multiple lists of data *)

   data1 = (# /. {x_, y_} -> {-x, y} &) /@ data;
   newXGrids = grids[[1]] /. {x_?NumericQ, z__} -> {-x, z};
   newYGrids = grids[[2]];
   newXTicks = ticks[[1]] /. {x_?NumericQ, y_, z__} -> {-x, y, z};
   newYTicks = ticks[[2]];
   {newXBottomTicks, newXTopTicks} =
    frameticks[[{1, 3}]] /. {x_?NumericQ, y_, z__} -> {-x, y, z};
   {newYLeftTicks, newYRightTicks} = frameticks[[{2, 4}]];,
   "Y",
   data1 = (# /. {x_, y_} -> {x, -y} &) /@ data;
   newXGrids = grids[[1]];
   newYGrids = grids[[2]] /. {x_?NumericQ, z__} -> {-x, z};
   newXTicks = ticks[[1]];
   newYTicks = ticks[[2]] /. {x_?NumericQ, y_, z__} -> {-x, y, z};
   {newXBottomTicks, newXTopTicks} = frameticks[[{1, 3}]];
   {newYLeftTicks, newYRightTicks} =
    frameticks[[{2, 4}]] /. {x_?NumericQ, y_, z__} -> {-x, y, z};,
   "XY",
   data1 = (# /. {x_, y_} -> {-x, -y} &) /@ data;
   newXGrids = grids[[1]] /. {x_?NumericQ, z__} -> {-x, z};
   newYGrids = grids[[2]] /. {x_?NumericQ, z__} -> {-x, z};
   newXTicks = ticks[[1]] /. {x_?NumericQ, y_, z__} -> {-x, y, z};
   newYTicks = ticks[[2]] /. {x_?NumericQ, y_, z__} -> {-x, y, z};
   {newXBottomTicks, newXTopTicks} =
    frameticks[[{1, 3}]] /. {x_?NumericQ, y_, z__} -> {-x, y, z};
   {newYLeftTicks, newYRightTicks} =
    frameticks[[{2, 4}]] /. {x_?NumericQ, y_, z__} -> {-x, y, z};
   ];

  ListLinePlot[data1,
   FrameTicks ->
    Join[{newXBottomTicks, newYLeftTicks, newXTopTicks,
      newYRightTicks}],
   Ticks -> Join[{newXTicks, newYTicks}],
   GridLines -> Join[{newXGrids, newYGrids}],
   opts1,
   reverseOpts
   ]
  ]


  • Prev by Date: Re: OneIdentity
  • Next by Date: Re: How to solve this equation?
  • Previous by thread: Re: How to plot with a reversed Y-axis?
  • Next by thread: Re: How to plot with a reversed Y-axis?