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: [mg103544] Re: How to plot with a reversed Y-axis?
  • From: Armand Tamzarian <mike.honeychurch at gmail.com>
  • Date: Sun, 27 Sep 2009 07:32:52 -0400 (EDT)
  • References: <h9i47h$9dh$1@smc.vnet.net> <h9kpgt$ms2$1@smc.vnet.net>

On Sep 26, 5:13 am, Armand Tamzarian <mike.honeychu... at gmail.com>
wrote:
> 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[Tick=
s],
>       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
>    ]
>   ]



Should have added some usage examples (incidentally you may notice
this function is relatively slow. Maybe others have ideas to improve
efficiency??):

data = Join[Table[{x, 7 x}, {x, -10, 0}], Table[{x, 0.5 x^3 + x^2 - 2
x}, {x, 0, 10}]];

data1 = Join[Table[{x, -3 x}, {x, -10, 0}], Table[{x, -3 x^3 + 15 x^2
+ 2 x}, {x, 0, 10}]];

ListLinePlot[{data, data1}, GridLines -> None, Axes -> True, Frame ->
True]

ReverseListPlot[{data1, data}, ReverseAxis -> "X", Frame -> True]

ReverseListPlot[{data, data1}, ReverseAxis -> "Y", GridLines -> None,
Frame -> True]

ReverseListPlot[{data1, data}, ReverseAxis -> "XY", Frame -> True]

ReverseListPlot[{data, data1}, ReverseAxis -> "X", GridLines ->
Automatic, Frame -> True]

ReverseListPlot[{data, data1}, ReverseAxis -> "XY", GridLines -> None,
Axes -> True, Frame -> False]

--Mike


  • Prev by Date: Re: Re: OneIdentity
  • Next by Date: BarChart Range
  • Previous by thread: Re: How to plot with a reversed Y-axis?
  • Next by thread: long module causes frontend to run slow