# Only plot a subset of x

**URL:** <https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485>\
**Category:** General Usage\
**Tags:** plotting, dates, plots\
**Created:** [June 7, 2021, 12:59am UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485 "2021-06-07T00:59:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [June 7, 2021, 12:59am UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/1 "2021-06-07T00:59:09Z")

</div>

```julia
using CSV, HTTP, DataFrames, Statistics, Plots, Dates

# Get Data 
link = "https://github.com/azev77/Synthetic_Control_in_Julia/raw/main/HP_q.csv"
r = HTTP.get(link)
df = CSV.read(r.body, DataFrame, missingstring="NA")

y = df[(df.loc .== "adelaide"), "HPGyy"]
time = unique(df.Date)
plot(legend = :topleft)
plot!(time, y, lab="adelaide", color="green") 
xaxis!(xrotation=45)

```

Gives  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/b/eb0946fd45cb41adbf767fbeffef3644ab3abee6.png)  
However, I only want the year, eg “2012” and not “01jun2012”.

The following does not work b/c Plots.jl doesn’t like repeated x-values

```julia
ttt = SubString.(time, 6, 9) # String of years for plots...
plot(legend = :topleft)
plot!(ttt, y, lab="adelaide", color="green") 
xaxis!(xrotation=45)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/b/6/b6a4aa942443fc92c5d0ff78b4b2683dbc1ba43c.png)

I lost a few hours on this, please help

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 7, 2021, 1:03am UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/2 "2021-06-07T01:03:27Z")

</div>

So you want all the data points, but only want the x ticks to be the year? Why not just modify `xticks`?

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [June 7, 2021, 1:03am UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/3 "2021-06-07T01:03:44Z")

</div>

yes. How?

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [June 7, 2021, 1:15am UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/4 "2021-06-07T01:15:49Z")

</div>

This gives it, I think:

```julia
plot(time, y, xticks = (1:4:41, ttt[1:4:41]), lab="adelaide", color="green")
plot!([14], seriestype = :vline, lab="Tax", color="black")

```

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

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [June 7, 2021, 1:46am UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/5 "2021-06-07T01:46:59Z")

</div>

There is a problem: the position of `xticks` do not correspond to the appropriate indexes:

```julia
plot(time, y, xticks = (1:4:41, ttt[1:4:41]), lab="adelaide", color="green")
plot!([1:4:41], seriestype = :vline, lab="1:4:41", color="black")
plot!([0:4:40], seriestype = :vline, lab="0:4:40", color="blue")
plot!([0.5:4:40.5], seriestype = :vline, lab="0.5:4:40.5", color="orange")

```

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

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 7, 2021, 8:34am UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/6 "2021-06-07T08:34:40Z")

</div>

Does it work if you put some filter of `time` in the place of the range in the definition of `xticks`?

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [June 7, 2021, 9:06am UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/7 "2021-06-07T09:06:39Z")

</div>

What do you mean?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 7, 2021, 9:27am UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/8 "2021-06-07T09:27:33Z")

</div>

> [@Albert\_Zevelev](#):
>
> `xticks = (1:4:41`

I can’t test now. But that range is supposed to define the positions of the ticks. Thus, make it a subset of your `time` vector, and the labels that subset converted to strings.

edit: I see now that you have an x-axis which is string-only. To play safe, I would suggest the following:

```julia
plot(legend = :topleft)
plot!(eachindex(time), y, lab="adelaide", color="green")
xaxis!(xrotation=45,xticks=(1:4:41,[time[i][end-3:end] for i in 1:4:41 ]))

```

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

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 7, 2021, 12:48pm UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/9 "2021-06-07T12:48:16Z")

</div>

My gut feeling is that it is more correct to use regexes, maybe

```julia
match.(r"\d{4}$", time)

```

It will give the same results, but feels somehow more correct, and will return `nothing` if the format is wrong.

Unfortunately, you have to access the actual matches by reading the fields of the match objects (`m.match`), which complicates broadcasting. If you create your own function

```julia
matchstring(m::RegexMatch) = m.match

```

you can do

```julia
years = matchstring.(match.(r"\d{2,4}$", time))

```

Too bad there are no default access methods for regex matches, it sets regexes apart from the rest of the base language.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 7, 2021, 1:17pm UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/10 "2021-06-07T13:17:15Z")

</div>

Don’t work with strings at all! Use the Dates.jl standard library

Also, the line

```julia
times = unique(df.Date)

```

is very scary. How do you know the data is already sorted?

Try this

```julia
using Chain, DataFramesMeta, CSV, Dates, HTTP, Plots # please put your usings in MWEs
# Get Data 
link = "https://github.com/azev77/Synthetic_Control_in_Julia/raw/main/HP_q.csv"
r = HTTP.get(link)
df = CSV.read(r.body, DataFrame, missingstring="NA")
p = @chain df begin 
    @transform Date = Dates.Date.(:Date, dateformat"dduuuyyyy")
    @where :loc .== "adelaide"
    @orderby :Date
    begin 
        y = _.HPGyy
        global t = _.Date
        plot(legend = :topleft)
        plot!(t, y, lab = "Adelaide", color = "green")
        xaxis!(xrotation = 45)
    end
end

```

It doesn’t solve your original problem entirely, since it doesn’t _just_ show the year. But the defaults are nicer when using an actual date type.

---

<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:** [June 7, 2021, 10:54pm UTC](https://discourse.julialang.org/t/only-plot-a-subset-of-x/62485/11 "2021-06-07T22:54:07Z")

</div>

@Albert_Zevelev, you can try the workflow herein.

```julia
using Dates, CSV, DataFrames, Plots; gr()
# link = "https://github.com/azev77/Synthetic_Control_in_Julia/raw/main/HP_q.csv"
df = CSV.read("HP_q.csv", DataFrame, missingstring="NA")
y = df[(df.loc .== "adelaide"), "HPGyy"]
t = df[(df.loc .== "adelaide"), "Date"]
t1 = Date.(t, dateformat"dduuuyyyy")
YrTick = year(minimum(t1)):year(maximum(t1))

plot(t1, y, lab="adelaide", color="green", xticks=false, legend=:topright)
plot!(xticks=(Date.(YrTick),YrTick),xtickfontsize=8,xlabel="Year", ylabel="HPGyy")
xaxis!(xrotation=45)

```

 ![Plot_gr_xaxis_Years_Dates](https://global.discourse-cdn.com/julialang/original/3X/2/1/21206cacbad9786adee909c7e2fe1fe67bd247ae.png)

**NB:**

- the years’ tick marks are placed at 01-Jan-YYYY, but this can be changed for any other preference.
- Simplified date parsing using `dateformat"dduuuyyyy"` as per @pdeffebach’s example above
