8
$\begingroup$

Say that I want to reverse the x-axis of a plot, there are a few solutions on here (like those in the answers here and here) but most of them are valid only for a ListPlot variant and not a Plot variant.

The answer by MarcoB in the second link above uses the option ScalingFunctions to achieve the goal and I was interested in how to apply it to a Plot (or LogPlot or LogLogPlot, etc). It seems to work just fine in reversing the x-axis if the function being plotted is linear:

{Plot[2 (k - 2), {k, -5, 0}, PlotRange -> All],
 Plot[2 (k - 2), {k, -5, 0}, 
  ScalingFunctions -> {{-# &, -# &}, Identity}, PlotRange -> All]}

enter image description here

but it gives strange results when the function is nonlinear,

{Plot[2 (k - 2)^3, {k, -5, 0}, PlotRange -> All],
 Plot[2 (k - 2)^3, {k, -5, 0}, 
  ScalingFunctions -> {{-# &, -# &}, Identity}, PlotRange -> All]}

enter image description here

If I try the same thing on a ListPlot it works just fine,

list = {#, 2 (# - 2)^3} & /@ Range[-5, 0, .1];
{ListPlot[list, PlotRange -> All],
 ListPlot[list, ScalingFunctions -> {{-# &, -# &}, Identity},
  PlotRange -> All]}

enter image description here

Is there a way to do this with ScalingFunctions, or is it simply not supported and I need to use my own workaround?

$\endgroup$
  • $\begingroup$ Even simpler problematic case: Plot[0, {t, 0, 1}, ScalingFunctions -> {Identity, "Log"}] puts the line at y == 1 when there should be no line. $\endgroup$ – István Zachar Mar 17 '16 at 11:00
6
$\begingroup$

Examination of the two nonlinear Plots (from the second piece of code in the question) with InputForm shows that ScalingFunctions correctly modifies the Ticks option but retains only the first and last elements of Line . Based on this observation, a fairly simple work-around is

(Edit: code simplified)

Plot[2 (k - 2)^3, {k, -5, 0}, PlotRange -> All] /. 
    {Line[z_] :> Line[MapAt[-# &, z, {All, 1}]], 
     Rule[Ticks, {_, z_}] -> Rule[Ticks, {Charting`ScaledTicks[{-#1 &, -#1 &}], z}]}

enter image description here

Addendum

ScalingFunctions works fine with ParametricPlot, although apparently not with Plot. Thus,

ParametricPlot[{k, 2 (k - 2)^3}, {k, -5, 0}, ScalingFunctions -> {{-# &, -# &}, Identity}, 
    PlotRange -> All, AspectRatio -> 1/GoldenRatio]

also produces the plot above. This update is based on Mr. Wizard's answer to question 13253. Incidentally, "Reverse" has the same effect as {-# &, -# &}.

$\endgroup$
  • 2
    $\begingroup$ I particularly like the idea of using ParametricPlot as a stand-in for Plot when rescaling is needed, since it enjoys similar adaptive sampling etc. Very clever workaround! (+1) $\endgroup$ – MarcoB Feb 16 '16 at 0:07
  • $\begingroup$ I agree, brilliant to use ParametricPlot! Especially since I am continually confunded by Charting`ScaledTicks, and always resort to CustomTicks instead. $\endgroup$ – Jason B. Feb 16 '16 at 9:17
  • $\begingroup$ @JasonB Thank you for your acceptance and kind words. Do you feel that the behavior of ChartingScaledTicks` in Plot is a bug? Although it is not documented, this option is recognized by Plot and produces an incorrect answer. Also, according to the documentation of Plot and ParametricPlot, the former is a special case of the latter. $\endgroup$ – bbgodfrey Feb 16 '16 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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