# Is there a way to plot negative numbers on a log scale?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-plot-negative-numbers-on-a-log-scale/49674>\
**Category:** New to Julia\
**Created:** [November 6, 2020, 10:32am UTC](https://discourse.julialang.org/t/is-there-a-way-to-plot-negative-numbers-on-a-log-scale/49674 "2020-11-06T10:32:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [November 6, 2020, 10:32am UTC](https://discourse.julialang.org/t/is-there-a-way-to-plot-negative-numbers-on-a-log-scale/49674/1 "2020-11-06T10:32:02Z")

</div>

I know we are not supposed to be able to take the log of a negative number (or 0), but sometimes the scale is really useful.

```julia
using Plots
x = -10:0.01:10
y = x
plot(x, y)
plot(x, y, yaxis = log10.(y .+ 100))

```

![image](https://global.discourse-cdn.com/julialang/original/3X/9/d/9d44b490fb398cc0a7a0e272eac01b74ad695ed2.png)

It’s not what I thought would happen.

![image](https://global.discourse-cdn.com/julialang/original/3X/1/4/14838cb1c2384fe1b812393f83a7632153bd4410.png)

Do you have any suggestions on how to do this?

---

<div class="post-metadata">

**Author:** ![roble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roble/32/12303_2.png) [@roble](https://discourse.julialang.org/u/roble)\
**Post date:** [November 6, 2020, 10:53am UTC](https://discourse.julialang.org/t/is-there-a-way-to-plot-negative-numbers-on-a-log-scale/49674/2 "2020-11-06T10:53:07Z")

</div>

I am sorry to tell you, but log has infinite complex values as a solution for negative arguments (if i recall that right) . Is there a way to keep the argument positive?

Edit: Oh i see, you added a hundred. So that should not be the issue.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [November 6, 2020, 10:58am UTC](https://discourse.julialang.org/t/is-there-a-way-to-plot-negative-numbers-on-a-log-scale/49674/3 "2020-11-06T10:58:54Z")

</div>

I’m trying to keep it positive by adding 100 to each y value with `yaxis = log10.(y .+ 100)`. I thought it would work but the yaxis loses its numbers.

---

<div class="post-metadata">

**Author:** ![jagot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jagot/32/12217_2.png) [@jagot](https://discourse.julialang.org/u/jagot)\
**Post date:** [November 6, 2020, 11:30am UTC](https://discourse.julialang.org/t/is-there-a-way-to-plot-negative-numbers-on-a-log-scale/49674/4 "2020-11-06T11:30:37Z")

</div>

I don’t know if Plots.jl supports this, but PyPlot.jl has a nifty axis scaling that’s called `symlog`: [Symlog Demo — Matplotlib 3.1.0 documentation](https://matplotlib.org/3.1.0/gallery/scales/symlog_demo.html) In essence, it makes large numbers logarithmic, both for positive and negative numbers, but keeps a small interval around the zero crossing linear.

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [November 6, 2020, 11:46am UTC](https://discourse.julialang.org/t/is-there-a-way-to-plot-negative-numbers-on-a-log-scale/49674/5 "2020-11-06T11:46:26Z")

</div>

Often when working with logs only the absolute value of the argument is of interest. Other times I plot positive and negative values in different colors; eg (with pyplot, exploiting the fact that semilog silently ignores negative values) `julia> semilogy(x, f.(x)); semilogy(x, .-f.(x))`.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [November 6, 2020, 11:57am UTC](https://discourse.julialang.org/t/is-there-a-way-to-plot-negative-numbers-on-a-log-scale/49674/6 "2020-11-06T11:57:47Z")

</div>

A little hack with `yticklabels` in GR or GRUtils - probably there is something similar in Plots, I didn’t look at it.

```julia
using GR
x = -10:0.01:10
y = x
yticklabels(x -> x-11)
plot(x, y.+11, ylog=true)

```

```julia
using GRUtils
x = -10:0.01:10
y = x
plot(x, y.+11, ylog=true)
yticklabels(x -> x-11)

```

In either case:

 ![GRplot](https://global.discourse-cdn.com/julialang/original/3X/f/6/f6f26c3e22817142e0494f739514b341b9ec2d59.png)

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [November 6, 2020, 12:01pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-plot-negative-numbers-on-a-log-scale/49674/7 "2020-11-06T12:01:14Z")

</div>

@rbange at [https://gitter.im/tbreloff/Plots.jl](https://gitter.im/tbreloff/Plots.jl) suggested a [log-modulus transformation](https://blogs.sas.com/content/iml/2014/07/14/log-transformation-of-pos-neg.html), which works very nicely and preserves the signs and 0 values.

`plot(x, sign.(y).*log10.(abs.(y).+1))`

![image](https://global.discourse-cdn.com/julialang/original/3X/2/3/23969fcfee736a60c87bab5ac5811f93497135d8.png)

It would be ideal to have the yaxis show 10^ (like a regular log10 axis in Julia), but am not sure how to specify that.
