# How to preserve xformatter when adding a vline

**URL:** https://discourse.julialang.org/t/how-to-preserve-xformatter-when-adding-a-vline/92051
**Category:** Visualization
**Tags:** plots, recipe
**Created:** [December 23, 2022, 4:10pm UTC](https://discourse.julialang.org/t/how-to-preserve-xformatter-when-adding-a-vline/92051 "2022-12-23T16:10:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bbejanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbejanov/32/31047_2.png) [@bbejanov](https://discourse.julialang.org/u/bbejanov)
#### Post date: [December 23, 2022, 4:10pm UTC](https://discourse.julialang.org/t/how-to-preserve-xformatter-when-adding-a-vline/92051/1 "2022-12-23T16:10:50Z")

</div>

I have a series recipe that formats the x-axis ticks. When I add a vline, the formatting is reset to the default, i.e. my custom formatting disappears.

Is it possible for vline to respect and preserve the formatting of the xticks that already exists?  
And, more generally, why is xformatter an attribute of the series and not of the subplot or the axis?

Here is an example code to demonstrate this:

```julia
@recipe function _myseries(::Type{Val{:myseries}}, x, y, z)
    xformatter --> function (d)
        yr = floor(Int, d)
        qr = 1 + floor(Int, 4 * (d - yr))
        return "$(yr)Q$(qr)"
    end
    seriestype := :path
end

dates = collect(2021:0.25:2024-0.1)
yvals = rand(length(dates))
plt = plot(dates, yvals, label="y", seriestype=:myseries)

```

![plot_1](https://global.discourse-cdn.com/julialang/original/3X/c/b/cba1c68204f26e88f77bf9924ae2266e326806a0.png)

If I add the vline, I get this:

```julia
vline!(plt, [2022.5], label="2022Q3", lc=:black)

```

![plot_2](https://global.discourse-cdn.com/julialang/original/3X/5/0/505adf85bbf0dd22a8b13f27d2f47c8d2f9820fb.png)

I can fix the formatting by forcing the xformatter like this:

```julia
plt = plot(dates, yvals, label="y", seriestype=:myseries)
vline!(plt, [2022.5], label="2022Q3", lc=:black, xformatter=plt.series_list[1][:xformatter])

```

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

I am developing a library, so I don’t want my users to have to do this.

Thanks,  
Boyan

---

<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: [December 23, 2022, 7:09pm UTC](https://discourse.julialang.org/t/how-to-preserve-xformatter-when-adding-a-vline/92051/2 "2022-12-23T19:09:09Z")

</div>

Could you define the formatter function:

```julia
function xfmt(d)
    yr = floor(Int, d)
    qr = 1 + floor(Int, 4 * (d - yr))
    return "$(yr)Q$(qr)"
end

```

and then use it when calling `vline!()`?

```julia
vline!(plt, [2022.5], label="2022Q3", lc=:black, xformatter=xfmt)

```

---

<div class="post-metadata">

### Author: ![bbejanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbejanov/32/31047_2.png) [@bbejanov](https://discourse.julialang.org/u/bbejanov)
#### Post date: [December 28, 2022, 2:34pm UTC](https://discourse.julialang.org/t/how-to-preserve-xformatter-when-adding-a-vline/92051/3 "2022-12-28T14:34:15Z")

</div>

Thanks Rafael. My question is about vline!() respecting the existing xformatter without having to specify it again in the vline!() call. The reason I need this is because the formatter function is internal in my package and the users don’t have access to it. Plus, I think this is not something they should have to worry about.

---

<div class="post-metadata">

### Author: ![bbejanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbejanov/32/31047_2.png) [@bbejanov](https://discourse.julialang.org/u/bbejanov)
#### Post date: [December 28, 2022, 2:39pm UTC](https://discourse.julialang.org/t/how-to-preserve-xformatter-when-adding-a-vline/92051/4 "2022-12-28T14:39:02Z")

</div>

Ok, I just found out that there is an open issue about this:  
[[BUG] `vline!` overrides previously set `formatter` · Issue #4538 · JuliaPlots/Plots.jl (github.com)](https://github.com/JuliaPlots/Plots.jl/issues/4538)  
Cheers!
