10
$\begingroup$

I have been playing with ScalingFunctions in Mathematica 11.0. I wanted to produce a logarithmic scaled plot with the direction reversed. I have had success with accomplishing my goals, but I have no understanding of why it works.

I am able to reverse the axis and/or create a logarithmic axes with out a problem.

Plot[x, {x, 1, 3}, 
 PlotRange -> {{1, 3}, {1, 3}},
 ScalingFunctions -> "Reverse"
 ]

Mathematica graphics

Plot[x, {x, 1, 3}, 
 PlotRange -> {{1, 3}, {1, 3}},
 ScalingFunctions -> "Log"
 ]

Mathematica graphics

In the documentation it talks about using

Mathematica graphics

as a scaling function and gives an example of

{-Log[#] &, Exp[-#] &}

I decided to try it.

Plot[x, {x, 1, 3}, 
 PlotRange -> {{1, 3}, {1, 3}},
 ScalingFunctions -> {None, {-Log[#] &, Exp[-#] &}}
 ]

Mathematica graphics

It produced what I wanted and in one sense that makes this a success. However, I am completely confused by what is going on.

The question is why does supplying ScalingFunctions with the negative of a the log and its inverse produce a logarithmic scaled plot with the direction reversed?

$\endgroup$

1 Answer 1

4
$\begingroup$

I've been curious about what the two different functions do in ScalingFunctions. I think we can get a clue by these two plots

Plot[x, {x, 1, 3}, PlotRange -> {{1, 3}, {1, 3}}, 
   ScalingFunctions -> {None, #}] & /@ {{# &, 
   Exp[-#] &}, {-Log[#] &, # &}}

Mathematica graphics

So the first function is applied to the points, and the second function somehow used to construct the tick marks.

Let's look at your example,

{regularPlot = Plot[x, {x, 1, 3}, PlotRange -> {{1, 3}, {1, 3}}],
 reverseLogPlot = 
  Plot[x, {x, 1, 3}, PlotRange -> {{1, 3}, {1, 3}}, 
   ScalingFunctions -> {None, {-Log[#] &, Exp[-#] &}}]}

We can extract the plotted points from the first, apply -Log[#]& to them, and compare that to the points from the scaled plot,

list = Cases[regularPlot, Line[x__] :> x, Infinity] // 
  First; reverseLogList = 
 reverseLogPlot // Cases[#, Line[x__] :> x, Infinity] & // First;
ListPlot /@ {reverseLogList, {#1, -Log[#2]} & @@@ list}

Mathematica graphics

and this confirms our suspicions about the first function.

I'm not sure exactly how the second function is applied to the tick marks though. This isn't quite right:

ListLinePlot[{#1, -Log[#2]} & @@@ list,
 Ticks -> {Automatic, {#, NumberForm[Exp[-#], 2]} & /@ 
    Range[-1, 0, .1]}] 

Mathematica graphics

Edit

Actually, I have no idea how these functions seem to work, why do these simple examples do nothing?

Plot[x, {x, -1, 3}, PlotRange -> {{1, 3}, {1, 3}}, 
 ScalingFunctions -> {None, {# + 2 &, # - 2 &}}]
Plot[x, {x, -1, 3}, PlotRange -> {{1, 3}, {1, 3}}, 
 ScalingFunctions -> {None, {2 # &, #/2 &}}]

Mathematica graphics

And why does removing the PlotRange option make the first one almost work, but the second one fails mysteriously?

Plot[x, {x, -1, 3}, ScalingFunctions -> {None, {# + 2 &, # - 2 &}}]
Plot[x, {x, -1, 3}, ScalingFunctions -> {None, {2 # &, #/2 &}}]

Mathematica graphics

$\endgroup$
7
  • $\begingroup$ This is helpful, but I am still confused by the interface. If the scaling function is monotone, the user should not need to provide the inverse. Is there a clear use case where being able to provide both arguments is desirable? $\endgroup$
    – Alan
    Commented Sep 14, 2016 at 13:47
  • $\begingroup$ @Alan - actually, now that I look further, I have no idea what these functions do, and I might just delete this answer - why do scaling functions like {2 # &, #/2 &} and {2 + # &, #-2 &} not work at all? To answer your question - it would be useful to be able to independently scale the plot and its tick marks. $\endgroup$
    – Jason B.
    Commented Sep 14, 2016 at 13:55
  • $\begingroup$ @JasonB I am convinced you are on the right track. As you pointed out the y values in reverseLogPlot are the log of the data. Applying Cases[reverseLogPlot, Rule[Ticks, x_List] -> x, Infinity] results in {{Automatic, Charting`ScaledTicks[{-Log[#1] &, Exp[-#1] &}]. It needs to apply the inverse in order to get the ticks to come out right. It apparently uses some sophistication as it uses a package function Charting`ScaledTicks. $\endgroup$ Commented Sep 14, 2016 at 14:55
  • $\begingroup$ @JackLaVigne - I remember trying to figure out ScaledTicks as well, and giving up. I wanted, just like here, to start at the simplest example: {2#, #/2} or {# +2, # -2}. And they failed there also $\endgroup$
    – Jason B.
    Commented Sep 14, 2016 at 15:53
  • 1
    $\begingroup$ Your guess for the usage of the last syntax of ScalingFunctions is correct, and your last 4 examples have worked as expected actually. Try Plot[x, {x, -1, 3}, PlotRange -> {{1, 3}, {1, 3}}, ScalingFunctions -> {None, {2 # &, #/2 &}}, AspectRatio -> Automatic] and you'll know what I mean. $\endgroup$
    – xzczd
    Commented Jun 27, 2018 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.