# How do I make a log plot where the plot is logarithmic in the distance from the X-Axis (including negative values)?

For example, if I were to do a plot of Sin[x], I would get what looks like a Log plot of Sin[x], with another log plot of -Sin[x] that has been flipped upside down and placed underneath the first one, in this way, the plot is logarithmic in distance from the x-axis, and can show both positive and negative values.

-
You're aware of the ambiguity that appears with plotting, say, 2Sin[x] in this style? –  Rahul Aug 21 '12 at 5:54
@RahulNarain 2Sin[x] should be slightly larger in distance from the x-axis at its peak than Sin[x], right? –  Andrew Spott Aug 21 '12 at 20:09
I was thinking of what happens with belisarius's method when I wrote that comment. –  Rahul Aug 21 '12 at 22:04
Andrew, I keep meaning to return to this question to add logarithmic ticks to the logify method in my answer. If I don't do this in the next 48 hours please leave a comment to remind me. –  Mr.Wizard Aug 25 '12 at 21:12
@Mr.Wizard: Since you asked, and I would love for the answer, this is your 48 hour reminder (give or take). Thanks for the help. –  Andrew Spott Aug 27 '12 at 23:08

I interpret this question quite differently from the other responders.

I would get what looks like a Log plot of Sin[x], with another log plot of -Sin[x] that has been flipped upside down and placed underneath the first one.

We can do that rather literally like this:

p1 = LogPlot[Sin[x], {x, 0, 15}];
p2 = LogPlot[-Sin[x], {x, 0, 15}];

pivot = Last[AxesOrigin /. Options[p2]];

Show[
p1,
MapAt[Scale[#, {1, -1}, {0, pivot}] &, p2, 1],
PlotRange -> All
]


This is not an ideal method however and I shall be working on a better one.

Here is a second method based on manipulating the output of LogPlot. If the logarithmic ticks are not required this is overly complicated. The Red/Blue style is added only for illustration.

p = LogPlot[{Sin[x], -Sin[x]}, {x, 0, 15}, PlotStyle -> {Red, Blue}];

pivot = Last[AxesOrigin /. Options@p];

MapAt[# /. Line[x__] :> Line[{#, 2 pivot - #2} & @@@ x] &, p, {1, 1, 4}] /. {
(Ticks -> {xdat_, ydat_}) :>
Ticks -> {xdat, Join[ydat, {2 pivot - #, ##2} & @@@ ydat]},
(PlotRange -> {x_, {y_, Y_}}) :> PlotRange -> {x, {2 y, Y}}
}


If $y$ ticks are unimportant you might use something like this:

logify[off_][x_?Positive] := Max[0, (off + Re@Log@x)/off]
logify[off_][x_?Negative] := Min[0, (off + Re@Log@x)/-off]


The parameter off is a scaling function. Example of use:

Plot[
logify[1] /@ {Sin@x, Cos@x, E Sin[x], E^2 Sin[x]},
{x, 0, 15},
Axes -> {True, False},
Evaluated -> True
]


As above but with logify[5]:

-
Interesting interpretation. I think a drawing in the question would be much better than wording it. –  belisarius Aug 21 '12 at 12:23
@Mr.Wizard It seems you can avoid the jump at y=0 via say , p = LogPlot[{Sin[x], -Sin[x]}, {x, 0, 15}, PlotStyle -> {Red, Blue}, PlotRange -> {0.001, 1}]; –  chris Aug 21 '12 at 14:10
On the other hand, p = LogPlot[{Sin[x], -Sin[x]}, {x, 0, 15}, PlotStyle -> {Red, Blue}, PlotRange -> {0.001, 10}]; only extends the red axis (so to speak). –  chris Aug 21 '12 at 14:12
@Verde: Yes, this is actually the interpretation I was looking for, but apparently I wasn't clear... –  Andrew Spott Aug 21 '12 at 20:15
@Mr.Wizard As usual, you come through! With bonus points because I can use your answer to do the same thing with ListLogPlot, which was what I originally wanted. –  Andrew Spott Aug 21 '12 at 20:15

Something like :

f[x_] := Which[Sin[x] > 0, Log[Sin[x]], Sin[x] < 0, -Log[-Sin[x]]]


-
Since this is a mathematical function, Piecewise would be more appropriate than Which. Alternately, you can ditch the piecewise definitions and use Sign@# Log@Abs@# &@Sin[x] instead :) –  rm -rf Aug 21 '12 at 0:23
Plot[{Sin@x, Sign@Sin@x Log@Abs@Sin@x}, {x, 0, 10 Pi}, Exclusions -> {(Sin@x == 0)}]


Edit

Just a nice plot with Log@Sin@x

Framed@Plot[{Sign@Sin@x Log@Abs@Sin@x, Log@Tan[x + Pi], -Log@Tan[x - Pi/2]}, {x, 0, Pi},
Exclusions -> {(Sin@x == 0)}, PlotRange -> {Automatic, {-5, 5}},
Filling -> {1 -> {3}, 1 -> {2}},
PlotStyle -> {{Thick, Blue}, {Thick, Red}, {Thick, Red}},
AxesStyle -> Directive[Gray, 12]]

-