# reversing the direction of the X axis in Plot and ListPlot

Discussion in 'Mathematica' started by Stern, Oct 24, 2008.

1. ### SternGuest

I'm graphing some bond values where the natural flow is for the x axis,
which represents days to maturity, to run from from big numbers to small
ones. I can achieve this by expressing my function in terms of days past
(which run small to big) and then using Ticks to label the axis the other
direction (as though it were days to maturity), but this feels like a
complex solution to a simple problem. Is there an easy way to Plot a
function from 180 to 0, rather than 0 to 180?

Thanks,

Michael

Stern, Oct 24, 2008

2. ### David ParkGuest

Yes, you do have to write a Ticks specification. I don't know if this makes
it a lot easier, but the Presentation package has a CustomTicks command that
allows you to specify any tick value transformation that is a 1-1 map to the
underlying coordinate.

Needs["Presentations`Master`"]

xticks = CustomTicks[180 - # &, {0, 180, 30, 3}];
Plot[100 Exp[-x/90], {x, 0, 180},
PlotRange -> {0, 110},
Frame -> True,
FrameTicks -> {{Automatic, Automatic}, {xticks,
xticks // NoTickLabels}},
FrameLabel -> {"Days to Maturity", "Value \$"},
PlotLabel ->
"Value of U.S. Treasuries After 10th Trillon Dollar Bailout",
BaseStyle -> {FontSize -> 14},
ImageSize -> 600]

David Park, Oct 25, 2008

3. ### JanukGuest

Hi Michael,

You could use a ParametricPlot to do the reversal for you. It would
then simply be a matter of reversing the tick mark labels. You might

SetAttributes[PlotReverse, HoldAll]
PlotReverse[func_, {t_, tmin_, tmax_}, opts___] :=
Module[{pl1, ticks, t1},
pl1 = ParametricPlot[
Evaluate[{tmax - t1, func /. t -> t1}],
{t1, tmin, tmax},
opts];
ticks = Ticks /. AbsoluteOptions[pl1];
ticks[[1, All, 2]] =
ticks[[1, All, 2]] /. {x_?NumericQ :> tmax - x};

Show[pl1, Ticks -> ticks]
]

f[x_] := Exp[x/40]
PlotReverse[f[x], {x, 0, 180}, AspectRatio -> 1/GoldenRatio]
Plot[f[x], {x, 0, 180}, AspectRatio -> 1/GoldenRatio]

I hope that helps,
Januk

Januk, Oct 27, 2008
4. ### JanukGuest

Hi Michael,

You could use a ParametricPlot to do the reversal for you. It would
then simply be a matter of reversing the tick mark labels. You might

SetAttributes[PlotReverse, HoldAll]
PlotReverse[func_, {t_, tmin_, tmax_}, opts___] :=
Module[{pl1, ticks, t1},
pl1 = ParametricPlot[
Evaluate[{tmax - t1, func /. t -> t1}],
{t1, tmin, tmax},
opts];
ticks = Ticks /. AbsoluteOptions[pl1];
ticks[[1, All, 2]] =
ticks[[1, All, 2]] /. {x_?NumericQ :> tmax - x};

Show[pl1, Ticks -> ticks]
]

f[x_] := Exp[x/40]
PlotReverse[f[x], {x, 0, 180}, AspectRatio -> 1/GoldenRatio]
Plot[f[x], {x, 0, 180}, AspectRatio -> 1/GoldenRatio]

I hope that helps,
Januk

Januk, Oct 28, 2008