Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reversing the axis of a plot

773 views
Skip to first unread message

JOHN C ERB

unread,
Dec 16, 1998, 3:00:00 AM12/16/98
to
Click,

Perhaps a misunderstanding.
I wish to reverse one of the axes, rather than to switch axes. For
example, the y-axis would remain the y-axis, and would range from 0 to
5, and the x-axis would remain the x-axis; however, the x-axis would
now range from 5 to 0 (with the plot reversed), as one goes from left
to right along the axis.

John C. Erb

-----Original Message-----
From: Click Work <cw...@xs4all.nl>
Subject: Re: Reversing the axis of a plot


>Why don't you just use a ParametricPlot?
>
>>use: Plot[3x^2+5x+2,{x,0,5}];
>
>ParametricPlot[{x,3x^2+5x+2},{x,0,5}]
>
>Now reverse the axes :
>
>ParametricPlot[{3x^2+5x+2,x},{x,0,5}]
>
>
>Hope this helps you out....
>
>-----Oorspronkelijk bericht-----
>Van: John C. Erb, Ph.D. <JOHN_...@prodigy.net>
>Nieuwsgroepen: comp.soft-sys.math.mathematica
>Datum: Wednesday, December 09, 1998 10:01 AM
>Onderwerp: Reversing the axis of a plot
>
>
>>
>>Occasionally, I have would like to make a plot where one of the axes is
>>reversed; Mathematica does not seem to have this capability built in.
>>
>>Wolfram support shows how to reverse the order of ticks in a plot (see
>>www.wolfram.com/support/Graphics/Axes/Ticks/Reverse.html); however it
>>does not reverse the plot,just the ticks on the axis.
>>
>>The best workaround I've come up is given in the following example,
>>which makes use of reversing the ticks.
>>
>>I want to reverse the x-axis of the
>>plot of 3x^2+5x+2.
>>
>>To get a normal plot I could
>>use: Plot[3x^2+5x+2,{x,0,5}];
>>
>>To reverse the plot for the x-axis,
>>I would do the following.
>>---------------------------------------------------------
>>Needs["ExtendGraphics`Ticks`"];
>>
>>tickreverse[start_,finish_]:=
>> TickFunction[start,finish,TextFunction->Reverse];
>>
>>this=Table[{x,3x^2+5x+2},{x,0,5,0.25}]; {x,y}=Transpose[this];
>>that=Transpose[{Reverse[x],y}];
>>ListPlot[that,Ticks->{tickreverse,Automatic},PlotJoined->True];
>>---------------------------------------------------------- NOTE: For
>>some reason, the above needs to be in 3 separate cells to function
>>properly. Does anyone have a more eloquent solution or any comments?
>>
>>John C. Erb
>>
>>email: John_...@prodigy.net
>>
>

BobH...@aol.com

unread,
Dec 17, 1998, 3:00:00 AM12/17/98
to

In a message dated 12/16/98 8:28:40 AM, JOHN_...@prodigy.net writes:

>Perhaps a misunderstanding.
>
>I wish to reverse one of the axes, rather than to switch axes. For
>example, the y-axis would remain the y-axis, and would range from 0 to
>5, and the x-axis would remain the x-axis; however, the x-axis would
>now range from 5 to 0 (with the plot reversed), as one goes from left
>to right along the axis.
>

John,

This is an approach
_______________________

reversePlot[func_, {var_Symbol, val1_?NumericQ,
val2_?NumericQ}, opts___] :=
Module[{x, varmin = Min[val1, val2], varmax = Max[val1, val2]},
Plot[func /. var :> (-x + varmin + varmax),
{x, varmin, varmax},
Ticks -> {Table[{x, ToString[N[varmax+varmin-x]]},
{x, varmin, varmax, (varmax-varmin)/5}], Automatic},
opts]]

Plot[3x^2+2x+5, {x, 2, 4}, PlotStyle -> RGBColor[1, 0, 0]];

reversePlot[3x^2+2x+5, {x, 2, 4}, PlotStyle -> RGBColor[1, 0, 0]];

Plot[Exp[x], {x, -1, 2}, PlotStyle -> RGBColor[1, 0, 0]];

reversePlot[Exp[x], {x, -1, 2}, PlotStyle -> RGBColor[1, 0, 0]];
_________________________

The tick labeling could use some work.

Bob Hanlon


MJE

unread,
Dec 17, 1998, 3:00:00 AM12/17/98
to
I want the same capability for ListDensityPlot, ListContourPlot,
DensityPlot, ContourPlot. It's often undesirable to have the array
indices going up instead of down. Especially when one is examining an
image. It plots upside down. You can use Reverse on the data but then
the y ticks are invalid. You can define a tick function but what a
pain.

There should perhaps be a simple option (ReverseAxes->{False,True} in my
case).

Mark
remove ".nospam" to reply


JOHN C ERB wrote:
>
> Click,


>
> Perhaps a misunderstanding.
> I wish to reverse one of the axes, rather than to switch axes. For
> example, the y-axis would remain the y-axis, and would range from 0 to
> 5, and the x-axis would remain the x-axis; however, the x-axis would
> now range from 5 to 0 (with the plot reversed), as one goes from left
> to right along the axis.
>

Thomas Wachtler

unread,
Dec 18, 1998, 3:00:00 AM12/18/98
to

John,

I had the same problem, and I tried to write a function that
would generate a reversed plot from a given Graphics object.
It turned out to be a pain, without knowing what goes on
inside Show[]. The result (see below) works in many cases, but
would have to be extended to account for all possible Options
you might want to use.

I think the only good solution to this can be to incorporate
this feature as a Graphics Option. I would appreciate comments
from the developers on the list as to whether anything like that
is intended for a future version.

Thomas


(* Code for plots with reversed axis *) (* the basic idea is to map the
coordinate values of the graphics
primitives as well as the Tick marks back to the same interval,
but in reversed order. *)

(* In order to get reversed Tick marks, I found it necessary to
modify the definition of TickFunction *) (* add the lines marked with
(**) to the code in `ExtendGraphics`Ticks *)

Options[ TickFunction] =
{
MajorLength -> {0.00625, 0},
MinorLength -> {0.003125, 0},
MajorStyle -> {Thickness[0.002]},
MinorStyle -> {Thickness[0.001]},
TextFunction -> Automatic,
TickLabels -> Automatic,
TickNumbers -> {8, 32},
(**) Reverse -> False
}

TickFunction[ x0_, x1_, opts___] :=
Block[{maj, min, majlen, minlen, opt, tnums,
majstyle, minstyle, textfun, labs (**) , rev
},
opt = Join[ {opts}, Options[ TickFunction]] ;
majlen = MajorLength /. opt ;
minlen = MinorLength /. opt ;
majstyle = MajorStyle /. opt ;
minstyle = MinorStyle /. opt ;
textfun = TextFunction /. opt ;
labs = TickLabels /. opt ;
(**) rev = Reverse /. opt ;
tnums = CheckNumbers[ TickNumbers /. opt] ;
If[ textfun === Automatic, textfun = TrimDecimal] ;
maj = TickPosition[ x0, x1, First[ tnums]] ;
min = TickPosition[ x0, x1, Last[ tnums]] ;
min = Complement[ min, maj] ;
maj = If[ MatrixQ[ labs],
Map[ {#, ""}&, maj],
Transpose[ {maj, textfun[ maj]}]] ;
maj = Map[ {#[[1]], #[[2]], majlen, majstyle}&, maj] ;
If[ Apply[ Plus, minlen] =!= 0,
min = Map[ {#, "", minlen, minstyle}&, min] ;
maj = Join[ maj, min]] ;
If[ MatrixQ[ labs],
maj = Join[ maj, Map[ Join[#, {{0,0}}]&, labs]]] ; (**)
If[ NumberQ[ rev],
(**) Map[ Join[ {rev - #[[1]]}, Drop[ #, 1]]&, maj ],
maj
(**) ]
]


(* functions to reverse Graphics primitives - extend for other
primitive as needed *)

LineReverseX[ lst_, xr_] := Line[Map[{xr-#[[1]], #[[2]]}&, lst]];
PointReverseX[ {x_,y_}, xr_] := Point[{xr-x,y}];


(* ShowReverseX reverses a plot along the x axis *)

ShowReverseX[ gr_Graphics, opts___] := Module[
{xr,tcks,frmtcks,pltrng,axorig,tckfct,revrule,newgr},
xr = Plus@@{Min[#],Max[#]}&[
Join[Cases[Flatten[ gr[[1]]], _Line], Cases[Flatten[ gr[[1]]],
_Point]] /.
{Line -> ((Transpose[#][[1]])&), Point -> ((#[[1]])&)}];
tckfct[x_] := TickFunction[ #1, #2, Reverse->x ]&;
tcks = Ticks /. Join[{opts}, gr[[2]]];
If[ Length[tcks] < 2, tcks = {tcks,tcks}];
tcks = {tckfct[xr], tcks[[2]]};
pltrng = PlotRange /. Join[{opts}, gr[[2]]];
If[Length[pltrng] == 2, pltrng = { xr-pltrng[[1]], pltrng[[2]] },
Null];
axorig = AxesOrigin /. Join[{opts}, gr[[2]]];
If[Length[axorig] == 2, axorig = {xr, 0} + {-1,1}*axorig, Null];
Return[ Show[ Graphics[ gr[[1]] /.
{ Line -> (LineReverseX[#, xr]&),
Point -> (PointReverseX[#,xr]&) },
Join[{Ticks->tcks, AxesOrigin->axorig,
PlotRange->pltrng},{opts},gr[[2]]]
]]];
];


(* Example: *)
(*


>>I want to reverse the x-axis of the
>>plot of 3x^2+5x+2.

*)

ShowReverseX[ Plot[ 3 x^2 + 5 x + 2, {x,1,5},
DisplayFunction->Identity],
DisplayFunction->$DisplayFunction]

0 new messages