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

Hi all,

I am using the source of historical data from: Stock Market Indices - Investing.com

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, the current price is 6800 , not below 1000. If it is below 1000 perhaps need to wait for another startup dotcom bubble edition III.

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.

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")


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

plot(dfjak.Date, dfjak.Price)

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

1 Like

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

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.

Try something like:

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 %"])

This is with .Date:

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")

this is with Dates.year:

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")

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"))

Try this:

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)
1 Like

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

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")

Try updated code further above.

1 Like