4
$\begingroup$

My question is rather to the point; I'm trying to reverse the x-axis on a ListPlot that has some objects drawn in it with Epilog. I looked around a bit and I found that the easiest method is to use ScalingFunctions->Reverse, which indeed neatly reverses the x-axis, but somehow ends up making the Epilog disappear. Does anyone know why this happens/how I can fix this?

Some sample code to illustrate it:

xs = Table[i, {i, 0, 1, 0.1}];
ys = xs^2;
{ListPlot[Transpose[{xs, ys}],(*ScalingFunctions\[Rule]{"Reverse",
  Identity},*)
  Epilog -> {{Directive[{Thick, Black}], 
     Line[{{0.4, 0}, {0.4, 1}}]}}], 
 ListPlot[Transpose[{xs, ys}], 
  ScalingFunctions -> {"Reverse", Identity}, 
  Epilog -> {{Directive[{Thick, Black}], 
     Line[{{0.4, 0}, {0.4, 1}}]}}]}

enter image description here

$\endgroup$
  • 1
    $\begingroup$ the reverse has effectively negated the coordinate system so you need to make your line Line[{{-0.4, 0}, {-0.4, 1}}] (sorry more of a work around than an explanation) $\endgroup$ – george2079 Jan 20 '17 at 18:32
  • $\begingroup$ ScalingFunctions -> Reverse just renames the ticks. If you increased your PlotRange to still include the right side of the x-axis I'm sure you'd see your epilog call. $\endgroup$ – user6014 Jan 20 '17 at 18:32
  • 1
    $\begingroup$ @user6014 it does quite a bit more than that. It transforms the data, fixes the PlotRange, the AxesOrigin, and the ticks. $\endgroup$ – rcollyer Jan 20 '17 at 18:34
7
$\begingroup$

The Epilog has not disappeared, it just is not visible with the PlotRange generated by ListPlot, e.g.

In[23]:= Options[plot, {PlotRange, Epilog}]
(*Out[23]= {PlotRange -> {{-1., 0}, {0, 1.}}, 
 Epilog -> {{Directive[{Thickness[Large], GrayLevel[0]}], 
  Line[{{0.4, 0}, {0.4, 1}}]}}}*)

where plot is the one with the ScalingFunctions. The difficulty here is that Epilog and Prolog exist in the Graphics coordinate system, not the coordinate system your data is in. The workaround is to transform the coordinates used to the data coordinates, i.e.

Epilog -> {{Directive[{Thickness[Large], GrayLevel[0]}], 
  Line[{{-0.4, 0}, {-0.4, 1}}]}}}

enter image description here

Similarly for the other ScalingFunctions, too.

$\endgroup$

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.