# Gadfly - non-logarithmic number format for axis in logarithmic scale

**URL:** <https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944>\
**Category:** Visualization\
**Tags:** gadfly\
**Created:** [April 21, 2020, 5:09am UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944 "2020-04-21T05:09:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 21, 2020, 5:09am UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944/1 "2020-04-21T05:09:14Z")

</div>

Something that I missed from Excel chart. Notice the y axis is logarithmic scale, but the number label is non logarithmic.  
 ![Excel_ylog10](https://global.discourse-cdn.com/julialang/original/3X/1/4/142ff0971261307d451089e3480ebef79ab2aa38.png)

Below is the best I got from Gadfly. The method to set axis limit is also not that friendly. `Coord.Cartesian(ymin=log10(0.1), ymax=log10(1000), xmin=0, xmax=0.5),`

 ![Gadfly_ylog10](https://global.discourse-cdn.com/julialang/original/3X/f/d/fdd52af8599e0809c8bafed93b0ea2a8b9adfefc.png)  
A default y\_log10 below.  
 ![Gadfly_ylog10_2](https://global.discourse-cdn.com/julialang/original/3X/9/4/949ba711bc643debb61522f05cf47e94d5677564.png)

Is there a way to mimic Excel approach, i.e. linear number for the axis label? Second priority is the major line (with number) and minor line (non-numbered)? Thank you.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [April 21, 2020, 9:09am UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944/2 "2020-04-21T09:09:47Z")

</div>

You can set the labels manually :

```julia
Scale.y_log10(labels = x -> "$(10^x)"),

```

You can also put a condition in there, for example when x is not an integer return an empty string. You might have to round the number too.

It’s indeed not super convenient but you should be able to get what you want with that.

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 21, 2020, 9:25am UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944/3 "2020-04-21T09:25:34Z")

</div>

Yes, that does the trick. Nice!  
Would you be able to do the minor lines that shouldn’t have label?

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [April 21, 2020, 9:31am UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944/4 "2020-04-21T09:31:54Z")

</div>

I think this should work, set an empty label when x isn’t an integer :

```julia
Scale.y_log10(labels = x -> mod(x,1) == 0.0 ? "$(10^x)" : "") 

```

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 21, 2020, 9:37am UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944/5 "2020-04-21T09:37:31Z")

</div>

No change. It is still with major lines only.

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [April 21, 2020, 9:47am UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944/6 "2020-04-21T09:47:31Z")

</div>

```julia
fn1(x) = (x in -1:x) ? string(10^x) : ""

plot(x=[0, 0.5], y=[0.1, 100], Geom.blank,
    Scale.y_log10(labels=fn1),
    Guide.yticks(ticks=-1:0.1:2)
)

```

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 21, 2020, 9:58am UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944/7 "2020-04-21T09:58:41Z")

</div>

The code gives linear minor lines. Modified the code as below.

```julia
using Gadfly

fn1(x) = (x in -1:x) ? string(10^x) : ""

ylog = []
for x in -1:3
    for y in 1:10
        push!(ylog, round(log10((y * exp10(x))), digits=1))
    end
end

p1 = Gadfly.plot(x=[0, 0.5], y=[0.1, 100], Geom.blank,
    #Scale.y_log10(labels=fn1),
    Scale.y_log10(labels = x -> mod(x,1) == 0.0 ? "$(10^x)" : ""), 
    #Guide.yticks(ticks=-1:0.1:2)
    Guide.yticks(ticks=ylog, label=true),
)

draw(PDF("logy.pdf", 10inch, 5inch), p1)

```

Sample plot below.

![Gadfly_ylog10_3](https://global.discourse-cdn.com/julialang/original/3X/9/3/9325d521fab5647475ef68dba09623c8821b0943.png)

Thanks all.

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 21, 2020, 10:08am UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944/8 "2020-04-21T10:08:35Z")

</div>

```julia
ylog = []
for x in -1:3
    for y in 1:10
        push!(ylog, round(log10((y * exp10(x))), digits=1))
    end
end

```

There must a way to make this code more elegant.

---

<div class="post-metadata">

**Author:** ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)\
**Post date:** [April 21, 2020, 11:10am UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944/9 "2020-04-21T11:10:07Z")

</div>

ylog = [round(log10((y \* exp10(x))), digits=1) for x in -1:3 for y in 1:10]

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 21, 2020, 11:34pm UTC](https://discourse.julialang.org/t/gadfly-non-logarithmic-number-format-for-axis-in-logarithmic-scale/37944/10 "2020-04-21T23:34:54Z")

</div>

Thanks @bjarthur. That’s a nice one liner.
