# Plots GR axis break

**URL:** <https://discourse.julialang.org/t/plots-gr-axis-break/59322>\
**Category:** Visualization\
**Tags:** plotting, gr, plot\
**Created:** [April 15, 2021, 9:07am UTC](https://discourse.julialang.org/t/plots-gr-axis-break/59322 "2021-04-15T09:07:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [April 15, 2021, 9:07am UTC](https://discourse.julialang.org/t/plots-gr-axis-break/59322/1 "2021-04-15T09:07:10Z")

</div>

Is it possible to plot a figure containing axis break like [this](https://matplotlib.org/stable/gallery/subplots_axes_and_figures/broken_axis.html) using Plots.jl and GR backend?

Especially in boxplot.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/1/f125422f03505e5920bada1896f64959427af4fb.jpeg)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 15, 2021, 9:19am UTC](https://discourse.julialang.org/t/plots-gr-axis-break/59322/2 "2021-04-15T09:19:50Z")

</div>

Never saw such type of example done with `gr()`, but you may try to implement it using the same subplot trick as done [here](https://gist.github.com/gizmaa/7214002#gistcomment-2831191) by @ianshmean

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 17, 2021, 4:03pm UTC](https://discourse.julialang.org/t/plots-gr-axis-break/59322/3 "2021-04-17T16:03:41Z")

</div>

Using the subplot trick mentioned above, the following plots with breaks can be obtained using `gr()` back-end, but the code is a bit shaky:

Using gr’s ggplot2 theme with no breaks:  
 ![plot_no_breaks](https://global.discourse-cdn.com/julialang/original/3X/b/e/be9e12b83279fd2966f39e2f2bc41d83a5497b88.png)

Using gr’s ggplot2 theme with breaks:

```julia
xb = [(500,800), (1900,2100)]
yb = [(100,300), (500,650), (850,900)]

```

 ![plot_with_breaks_ggplot2_theme](https://global.discourse-cdn.com/julialang/original/3X/e/5/e5a718944e9cadac20b2ba24393199cad368e0ea.png)

Using gr’s dark theme with breaks:

 ![plot_with_breaks_dark_theme](https://global.discourse-cdn.com/julialang/original/3X/3/4/348e2703369d06b15438d4007f448376922573aa.png)

Using gr’s dark theme with breaks, but in log-log scale:

 ![plot_with_breaks_dark_theme_log10_log10_scale](https://global.discourse-cdn.com/julialang/original/3X/2/e/2e751cdb61cee7da713c02b9d5ca85c2ef02b9a2.png)

Using gr’s ggplot2 theme with 1 ybreak:

```julia
xb = [] # empty set for no breaks
yb = [(100,300)]

```

 ![plot_with_1_ybreak_ggplot2_theme](https://global.discourse-cdn.com/julialang/original/3X/1/c/1c8d55e6573f376cb0681919e2c95b5e1261f283.png)

Here below the code, with a user function `breakplot()` defined:

```julia
using Measures, Plots; gr()

function breakplot(X,Y,xb,yb; lc=:yellow,lw=3,ls=:solid,xlabel="X-axis",ylabel="Y-axis",ptheme=:dark,
                   xscale=:identity, yscale=:identity, size = (1000,700))
    # by @rafael.guerra

    if occursin("dark",lowercase(string(ptheme)))
        gc =:white
    else
        gc =:black
    end
    nx, ny = length(xb), length(yb)

    xb2 = [X[1]; collect(Iterators.flatten(xb)); X[end]]
    yb2 = [minimum(Y); collect(Iterators.flatten(yb)); maximum(Y)]

    w = [diff(xb2[2(i-1)+1:2*i])[1] for i in 1:nx+1]
    if occursin("log10",lowercase(string(xscale)))
        w = log10.(w)
    end
    w = w / sum(w)

    h = [diff(yb2[2(i-1)+1:2*i])[1] for i in 1:ny+1]
    if occursin("log10",lowercase(string(yscale)))
        h = log10.(h)
    end
    h = reverse(h) / sum(h)
    
    theme(ptheme)
    l = @layout [a{0.001w} [grid(ny+1,nx+1, heights=h, widths=w); b{0.001h}]] # first & last subplots for axes labels
    p = fill(plot( lw=lw,ls=ls), 1 + (ny+1) * (nx+1) + 1)

    p[1] = plot(ylabel=ylabel, guidefont=font(10,gc),guide_position=:right,showaxis=false,xticks=false,yticks=false,margin=5mm)
    for j in 1:ny+1
        jj = (ny+1) - j + 1
        ylj = yb2[2(j-1)+1:2*j]
        for i in 1:nx+1
            xli = xb2[2(i-1)+1:2*i]
            ix = (jj-1)*(nx+1) + i
            if (i == 1) & (j != 1)
                p[1+ix] = plot(X,Y, xlims=xli, ylims=ylj,framestyle=:default,legend=false,margin=0mm,lc=lc,tickfontsize=6,
                               xshowaxis=false,xticks=false,xscale=xscale,yscale=yscale, size=(size[1]*h[j],size[2]*w[i]))
            elseif (i != 1) & (j == 1) 
                showaxis=:x
                p[1+ix] = plot(X,Y, xlims=xli, ylims=ylj,framestyle=:default,legend=false,margin=0mm,lc=lc,tickfontsize=6,
                               yshowaxis=false,yticks=false,xscale=xscale,yscale=yscale, size=(size[1]*h[j],size[2]*w[i]))
            elseif (i == 1) & (j == 1)
                p[1+ix] = plot(X,Y, xlims=xli, ylims=ylj,framestyle=:default,legend=false,margin=0mm,lc=lc,tickfontsize=6,
                               xscale=xscale,yscale=yscale, size=(size[1]*h[j],size[2]*w[i]))
            else
                p[1+ix] = plot(X,Y, xlims=xli, ylims=ylj,framestyle=nothing,legend=false,margin=0mm,lc=lc,showaxis=false,
                               xticks=false,yticks=false,xscale=xscale,yscale=yscale, size=(size[1]*h[j],size[2]*w[i]))
            end
        end
    end
    p[end] = plot(xlabel=xlabel, guidefont=font(10,gc),guide_position=:top,showaxis=false,xticks=false,yticks=false,margin=5mm)

    plot(p..., layout=l, grid=false, size=size)
end

# Examples:
X = 1:2500.0
Y = 1000*(sin.(X/500)).^2 .+ 1

xb = [(500,800), (1900,2100)]
yb = [(100,300), (500,650), (850,900)]

theme(:default)
plot(X,Y, xlabel="Time [min]",ylabel="Trajectory altitude [m]", legend=false)

breakplot(X,Y,xb,yb; lc=:blue, xlabel="Time [min]",ylabel="Trajectory altitude [m]",ptheme=:ggplot2)

breakplot(X,Y,xb,yb; lc=:yellow, xlabel="Time [min]",ylabel="Trajectory altitude [m]")

breakplot(X,Y,xb,yb; lc=:yellow, xlabel="Time [min]",ylabel="Trajectory altitude [m]", xscale=:log10,yscale=:log10)

xb = [] # empty set for no breaks
yb = [(100,300)]
breakplot(X,Y,xb,yb; lc=:blue, xlabel="Time [min]",ylabel="Trajectory altitude [m]",ptheme=:ggplot2)

```

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [April 19, 2021, 12:03pm UTC](https://discourse.julialang.org/t/plots-gr-axis-break/59322/4 "2021-04-19T12:03:56Z")

</div>

It seems nice but I cannot found the example code.  
Can you share an example or any link?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 19, 2021, 2:48pm UTC](https://discourse.julialang.org/t/plots-gr-axis-break/59322/5 "2021-04-19T14:48:12Z")

</div>

@iHany, as requested the user function code and examples were updated above.  
As indicated, this subplot business in Plots.jl and `gr()` is a bit shaky…  
If you see things that can be obviously improved, please do share.

_NB: there was a bug on the list of vertical proportions which were in reverse order. Fixed now._

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [April 20, 2021, 12:48am UTC](https://discourse.julialang.org/t/plots-gr-axis-break/59322/6 "2021-04-20T00:48:53Z")

</div>

Ok…  
I think that your remedy works fine but it needs additional efforts as you said.  
It seems that there are already some issues reported in Plots.jl such as [this](https://github.com/JuliaPlots/Plots.jl/issues/3415), so subplot method would be the best for now.

Thanks for sharing your example 🙂
