17
$\begingroup$

I am trying to create a plot using ListLinePlot in which the x-axis is represented in a logarithmic scale, and is reversed (so larger values are on the right side).

I have found that ScalingFunctions -> {"Reverse"} will reverse the axis and ScalingFunctions -> {"Log"} will scale the axis to a log scale, but the two commands will not work together.

Does anyone have any ideas?

$\endgroup$
4
  • $\begingroup$ In Mathematica 8, ScalingFunctions is not an option for ListLinePlot. Are you using a different version of Mathematica? $\endgroup$ Sep 6, 2012 at 4:13
  • 5
    $\begingroup$ @CarlMorris actually, they are not documented to do so, but they do (see also here). $\endgroup$
    – Verbeia
    Sep 6, 2012 at 4:25
  • $\begingroup$ @Verbeia It seems "Reverse" does not work for ListLogLinearPlot $\endgroup$ Sep 6, 2012 at 4:53
  • $\begingroup$ @Verde I know, but I got tangled in my answer and hadn't posted it until now. $\endgroup$
    – Verbeia
    Sep 6, 2012 at 5:19

3 Answers 3

10
$\begingroup$

If you want to stick with ScalingFunctions then you just have to use a pure function where you specify the transformation you would like to have. Simple example:

ListLinePlot[Table[{x, x^2 + 10}, {x, 0, 10, 1}],
 ScalingFunctions -> {None, {Sqrt[# - 10] &, #^2 + 10 &}}
]

So what you have to do is to supply a function for the x-axis which reverses and takes the log and you need to supply the inverse transformation for that.

$\endgroup$
1
  • 4
    $\begingroup$ Take a look at this : #[Table[{n, Sqrt[n]}, {n, 1000}], ScalingFunctions -> {{-Log@# &, E^-# &}, None}] & /@ {ListPlot, ListLinePlot} $\endgroup$ Sep 6, 2012 at 11:42
8
$\begingroup$
tick = AbsoluteOptions[ListLogLinearPlot[Table[{n, Sqrt[n]}, {n, 1000}]], Ticks][[1,2]];
t1 = {tick[[1]] /. {x_, y_, z_, w_} -> {-x, y, z, w}, tick[[2]]};
ListPlot[Table[{-Log@n, Sqrt[n]}, {n, 1000}], Ticks -> t1]

Mathematica graphics

(The usual credit to @Heike for her torn[] function)

$\endgroup$
3
$\begingroup$

There doesn't seem to be a way you can get both log and reversed x-axes out of the box.

Consider some data that might warrant such a presentation:

data = Sort@RandomVariate[LogNormalDistribution[0.2, 2.], {40, 2}];

ListLogLinearPlot gives a log x-axis and normal y-axis

ListLogLinearPlot[data, Joined -> True, PlotRange -> All]

enter image description here

The ScalingFunction option isn't meant to work for ListLinePlot, but it actually does.

ListLinePlot[data, Joined -> True, 
  ScalingFunctions -> {"Reverse", None}]    

enter image description here

However, it doesn't work for ListLogLinearPlot.

So, the answer is to manipulate data and ticks much in the same way as Verde's answer suggests.

You can write a custom tick function that takes the maximum and minimum data points and builds tick functions from that. I am not going to pretend this is quite the function you want (I got tangled and don't have time to fix it now), but you can see how to build a general tick function that doesn't require specific knowledge of the data upfront.

 loggedticks[min_, max_] :=
   Table[{i, ScientificForm[10^i]}, {i, Floor@Log[10, min],Ceiling@Log[10, max]}] 

ListLogLinearPlot[data, Joined -> True, 
  Ticks -> {loggedticks, Automatic}]
$\endgroup$
1
  • $\begingroup$ There is a way to get both things with ScalingFunctions. See halirutan's answer and my comment below. Really unexpected. $\endgroup$ Sep 6, 2012 at 11:46

Your Answer

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

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