# DataFrames Plot: Plot the year for the x-tick at x-axis

**URL:** <https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782>\
**Category:** General Usage\
**Tags:** dates, dataframes, plots\
**Created:** [February 17, 2023, 9:52am UTC](https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782 "2023-02-17T09:52:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [February 17, 2023, 9:52am UTC](https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782/1 "2023-02-17T09:52:56Z")

</div>

Hi all,

I am using the source of historical data from: [Stock Market Indices - Investing.com](https://www.investing.com/indices/)

I want to plot the price (graph plot) from the csv I downloaded (JKSE / Jakarta Stock Composite Index).

But, the x-tick is number 1-400, **I want to show year if possible like 1990 1995 2000 for the x-tick**

and another problem:

They get it upside down. The first one is the top row, thus the graph is mirrored with the real graph from yahoo finance or [investing.com](http://investing.com), the current price is 6800 , not below 1000. If it is below 1000 perhaps need to wait for another startup dotcom bubble edition III.

 ![Capture d’écran_2023-02-17_16-48-14](https://global.discourse-cdn.com/julialang/original/3X/0/0/00e7e9fc3555c20ca37e0e9de7b28024f330cdcd.png)

**Edit:**

This code works with data at x-axis but it is in reverse, the right one should be newer date. This is a new kind of chart, mirroring.

```julia
using CSV, DataFrames, Plots, Plots.PlotMeasures

filejak = "./csv/Jakarta Stock Exchange Composite Index Historical Data.csv"

dfjak = CSV.read(filejak, DataFrame)

dfjak.Price .= parse.(Float64, replace.(dfjak.Price, "," => ""))
dfjak[!,"Change %"] .= parse.(Float64, replace.(dfjak[!,"Change %"], "%" => ""))

p1 = plot(dfjak.Date, dfjak.Price, title="",
	label="Jakarta Stock Exchange (Price)", xlabel="", ylabel="")
p2 = plot(dfjak.Date, dfjak[!,"Change %"], title="",
	label="Jakarta Stock Exchange (% Change)", xlabel="", ylabel="")

plot(p1, p2, size=(1200,800), layout = (2, 1), 
	legend=:outerright, left_margin=10mm, bottom_margin=5mm,
     	xaxis = "", yaxis = "Frequencies")

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 17, 2023, 10:17am UTC](https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782/2 "2023-02-17T10:17:55Z")

</div>

You need to supply values for the x-axis if you don’t just want your plot to start from zero, so something like

```julia
plot(dfjak.Date, dfjak.Price)

```

(assuming that there is a column `Date` in your data which gives the trading day).

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [February 17, 2023, 11:42am UTC](https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782/3 "2023-02-17T11:42:53Z")

</div>

yes, but still the order is still in reverse. It works with `dfjak.Date`

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 17, 2023, 11:57am UTC](https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782/4 "2023-02-17T11:57:44Z")

</div>

Well it plots in whatever order you provide the data to `plot` - if you want to have older dates first you need to `sort` your data on `:Date`.

---

<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:** [February 17, 2023, 12:49pm UTC](https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782/5 "2023-02-17T12:49:51Z")

</div>

Try something like:

```julia
using Dates
df.year = Dates.year.(Date.(df.Date, "mm/dd/yyyy"))
# sort(df, :year) # Sort dataframe by year if needed
plot(df.year, df[!,"Change %"])

```

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [February 17, 2023, 2:26pm UTC](https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782/6 "2023-02-17T14:26:00Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> df.year = Dates.year.(Date.(df.Date, "mm/dd/yyyy"))
> # sort(df, :year) # Sort dataframe by year if needed
> plot(df.year, df[!,"Change %"])
> 
> ```

This is with `.Date`:

```julia
using Dates, CSV, DataFrames, Plots, Plots.PlotMeasures

filejak = "./csv/Jakarta Stock Exchange Composite Index Historical Data.csv"

dfjak = CSV.read(filejak, DataFrame)

dfjak.Price .= parse.(Float64, replace.(dfjak.Price, "," => ""))
dfjak[!,"Change %"] .= parse.(Float64, replace.(dfjak[!,"Change %"], "%" => ""))
dfjak.year = Dates.year.(Date.(dfjak.Date, "mm/dd/yyyy"))
# sort(dfjak, :year) # Sort dataframe by year if needed

p1 = plot(dfjak.Date, dfjak.Price, title="",
	label="Jakarta Stock Exchange (Price)", xlabel="", ylabel="")
p2 = plot(dfjak.Date, dfjak[!,"Change %"], title="",
	label="Jakarta Stock Exchange (% Change)", xlabel="", ylabel="")

plot(p1, p2, size=(1200,800), layout = (2, 1), 
	legend=:outerright, left_margin=10mm, bottom_margin=5mm,
     	xaxis = "", yaxis = "Frequencies")

```

 ![Capture d’écran_2023-02-17_21-23-52](https://global.discourse-cdn.com/julialang/original/3X/8/c/8cc422d494501204d838cf3098997acda9462e8e.png)

this is with `Dates.year`:

```julia
using CSV, Dates, DataFrames, Plots, Plots.PlotMeasures

filejak = "./csv/Jakarta Stock Exchange Composite Index Historical Data.csv"

dfjak = CSV.read(filejak, DataFrame)

dfjak.Price .= parse.(Float64, replace.(dfjak.Price, "," => ""))
dfjak[!,"Change %"] .= parse.(Float64, replace.(dfjak[!,"Change %"], "%" => ""))
dfjak.year = Dates.year.(Date.(dfjak.Date, "mm/dd/yyyy"))
# sort(dfjak, :year) # Sort dataframe by year if needed

p1 = plot(dfjak.year, dfjak.Price, title="",
    label="Jakarta Stock Exchange (Price)", xlabel="", ylabel="")
p2 = plot(dfjak.Date, dfjak[!,"Change %"], title="",
    label="Jakarta Stock Exchange (% Change)", xlabel="", ylabel="")

plot(p1, p2, size=(900,800), layout = (2, 1), 
    legend=:outerright, left_margin=10mm, bottom_margin=5mm,
     xaxis = "", yaxis = "Frequencies")

```

 ![Capture d’écran_2023-02-17_21-25-01](https://global.discourse-cdn.com/julialang/original/3X/7/4/74dfd50f5197af5c0a8ab4877052c3f74a3ba6e2.png)

I like the graph when using `plot(dfjak.Date, dfjak.Price)` but for xtick I want to only show the year like when using `Dates.year.(Date.(df.Date, "mm/dd/yyyy"))`

---

<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:** [February 17, 2023, 3:15pm UTC](https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782/7 "2023-02-17T15:15:07Z")

</div>

Try this:

```julia
using Dates
df.Dates = Date.(df.Date, "mm/dd/yyyy")
tick_years = Date.(unique(Dates.year.(df.Dates)))
tick_years = minimum(tick_years):Year(5):maximum(tick_years)
DateTick = Dates.format.(tick_years, "yyyy")
xlims = extrema([tick_years; df.Dates])
plot(df.Dates, df[!,"Change %"], xticks=(tick_years,DateTick), xlims=xlims)

```

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [February 17, 2023, 3:26pm UTC](https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782/8 "2023-02-17T15:26:33Z")

</div>

> [@rafael.guerra](#):
>
> `xticks=(tick_years,DateTick), xlims=xlims`

How to make the interval between xtick? so after 1990 it will be 1995 then 2000 so it won’t print every year from 1990 1991 1992 1993 1994 … till 2023

```julia
using CSV, Dates, DataFrames, Plots, Plots.PlotMeasures

filejak = "./csv/Jakarta Stock Exchange Composite Index Historical Data.csv"

dfjak = CSV.read(filejak, DataFrame)

dfjak.Price .= parse.(Float64, replace.(dfjak.Price, "," => ""))
dfjak[!,"Change %"] .= parse.(Float64, replace.(dfjak[!,"Change %"], "%" => ""))

dfjak.Dates = Date.(dfjak.Date, "mm/dd/yyyy")
tick_years = Date.(unique(Dates.year.(dfjak.Dates)))
DateTick = Dates.format.(tick_years, "yyyy")
xlims = extrema([tick_years; dfjak.Dates])

p1 = plot(dfjak.Dates, dfjak.Price, title="",
    xticks=(tick_years,DateTick), xlims=xlims,
    label="Jakarta Stock Exchange (Price)", xlabel="", ylabel="")
p2 = histogram(dfjak.Date, dfjak[!,"Change %"], title="",
    label="Jakarta Stock Exchange (% Change)", xlabel="", ylabel="")

plot(p1, p2, size=(1200,800), layout = (2, 1), 
    legend=:outerright, left_margin=10mm, bottom_margin=5mm,
     xaxis = "", yaxis = "Frequencies")

```

---

<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:** [February 17, 2023, 4:00pm UTC](https://discourse.julialang.org/t/dataframes-plot-plot-the-year-for-the-x-tick-at-x-axis/94782/9 "2023-02-17T16:00:54Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> How to make the interval between xtick? so after 1990 it will be 1995 then 2000 so it won’t print every year from 1990 1991 1992 1993 1994 … till 2023

Try updated code further above.
