# Create Histogram of Monthly Return with CSV and DataFrames

**URL:** <https://discourse.julialang.org/t/create-histogram-of-monthly-return-with-csv-and-dataframes/94729>\
**Category:** General Usage\
**Tags:** dataframes, csv, plots\
**Created:** [February 16, 2023, 4:04pm UTC](https://discourse.julialang.org/t/create-histogram-of-monthly-return-with-csv-and-dataframes/94729 "2023-02-16T16:04:05Z")\
**Posts on this page:** 7\
**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 16, 2023, 4:04pm UTC](https://discourse.julialang.org/t/create-histogram-of-monthly-return-with-csv-and-dataframes/94729/1 "2023-02-16T16:04:06Z")

</div>

Hi all,

I am reading this:

> **[Chapter 3 Probability introduction | Data Science in Julia for Hackers](https://datasciencejuliahackers.com/probability-introduction.html)**
>
> Chapter 3 Probability introduction | Data Science in Julia for Hackers

and I want to plot a histogram for monthly return of a Shanghai Composite Index based on this code:

```julia
using CSV, DataFrames
    
#Read the CSV file and transform it into a DataFrame
    
rain_data = CSV.read("./csv/historico_precipitaciones.csv", DataFrame)
  
#Rename the columns 
colnames = ["Year", "Month", "mm", "Days"]
rename!(rain_data, Symbol.(colnames)) #Symbol is the type of object used to represent the labels of a dataset
    
#We use a dictionary to translate de Month names 
translate = Dict("Enero" => "January" ,"Febrero" => "February" ,"Marzo" => "March" ,"Abril" => "April" ,"Mayo" => "May" ,"Junio" => "June" ,"Julio" => "July" ,"Agosto" => "August" ,"Septiembre" => "September" ,"Octubre" => "October" ,"Noviembre" => "November" ,"Diciembre" => "December")
    
    for i in 1:length(rain_data[:,:Month])
        rain_data[i,:Month] = translate[rain_data[i,:Month]]
    end

# See the first 8 rows
# first(rain_data, 8)

begin
    histogram(rain_data[:,"mm"], bins=20, legend=false, size=(450, 300))
    title!("Monthly rainfall in Buenos Aires")
    xlabel!("Rainfall (mm)")
    ylabel!("Frequency")
end

```

But the csv is a monthly return that you can retrieve from 🙂

> **[Shanghai Composite Historical Rates (SSEC) - Investing.com](https://www.investing.com/indices/shanghai-composite-historical-data)**
>
> Get free historical data for the Shanghai Composite.

it is Shanghai Composite Index Data.

I want to know can the x axis have negative value too? Since the return can be negative, if the price falls from the previous month.

so just:

- y-axis = the frequency (the number of months the return at certain interval occuring)

- x-axis = the return in percent.

All is available in the [investing.com](http://investing.com) website.

Thanks!

---

<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 16, 2023, 4:56pm UTC](https://discourse.julialang.org/t/create-histogram-of-monthly-return-with-csv-and-dataframes/94729/2 "2023-02-16T16:56:14Z")

</div>

Why shouldn’t the x-axis be able to have negative values?

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

(Disclaimer: I just used `randn` for convenience, I am not implying returns are normally distributed, don’t sue me if you implement a trading strategy that assumes this!)

---

<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 16, 2023, 7:04pm UTC](https://discourse.julialang.org/t/create-histogram-of-monthly-return-with-csv-and-dataframes/94729/3 "2023-02-16T19:04:36Z")

</div>

You could use the linked site option to download the monthly data in CSV format and then use the customary tools:

```julia
using CSV, DataFrames, Plots
file = "Shanghai Composite Historical Data_Monthly.csv"
df = CSV.read(file, DataFrame)
df.Price .= parse.(Float64, replace.(df.Price, "," => ""))
histogram(df.Price)

```

_ **NB** : had to get rid of the comma separator for thousands_

---

<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, 4:39am UTC](https://discourse.julialang.org/t/create-histogram-of-monthly-return-with-csv-and-dataframes/94729/4 "2023-02-17T04:39:44Z")

</div>

No I won’t sue you, it is on almost all Finance books that assume the return has normal distribution.

---

<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, 5:01am UTC](https://discourse.julialang.org/t/create-histogram-of-monthly-return-with-csv-and-dataframes/94729/5 "2023-02-17T05:01:35Z")

</div>

Thanks @rafael.guerra

I was trying to make histogram from the return columns, the title is “Change %”

but it does not working:

```julia
using CSV, DataFrames, Plots

file = "./csv/Shanghai Composite Historical Data.csv"
df = CSV.read(file, DataFrame)
df.Change .= parse.(String)

histogram(df.Change, title="Shanghai Composite Index Histogram",
	label="", xlabel="Return (%)", ylabel="Frequency")

```

---

<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, 5:25am UTC](https://discourse.julialang.org/t/create-histogram-of-monthly-return-with-csv-and-dataframes/94729/6 "2023-02-17T05:25:04Z")

</div>

Another one,

I try to make multiple layouts, there are problems:

1. The resolution is too small, the ylabel is too big, thus the histograms becoming fat
2. Why the label are wrong even if I put `[s1 s2 s3 s4]` it only shows `s1 and s3` ?
3. Can I color the histogram? I use `color` or `hist_color` they don’t work.

```julia
using CSV, DataFrames, Plots

file = "./csv/Shanghai Composite Historical Data.csv"
filedj = "./csv/Dow Jones Industrial Average Historical Data.csv"
filenasdaq = "./csv/NASDAQ Composite Historical Data.csv"
filenikkei = "./csv/Nikkei 225 Historical Data.csv"

df = CSV.read(file, DataFrame)
df2 = CSV.read(filedj, DataFrame)
df3 = CSV.read(filenasdaq, DataFrame)
df4 = CSV.read(filenikkei, DataFrame)

df.Price .= parse.(Float64, replace.(df.Price, "," => ""))
df2.Price .= parse.(Float64, replace.(df2.Price, "," => ""))
df3.Price .= parse.(Float64, replace.(df3.Price, "," => ""))
df4.Price .= parse.(Float64, replace.(df4.Price, "," => ""))

p1 = histogram(df.Price, title="",
	label="", xlabel="", ylabel="")
p2 = histogram(df2.Price, title="",
	label="", xlabel="", ylabel="")
p3 = histogram(df3.Price, title="",
	label="", xlabel="", ylabel="")
p4 = histogram(df4.Price, title="",
	label="", xlabel="", ylabel="")

s1 = "Shanghai Composite Index (SSE)";
s2 = "Dow Jones Industrial Average";
s3 = "NASDAQ";
s4 = "Nikkei 225";

plot(p1, p2, p3, p4, layout = (4, 1), legend=:outerright,
     xaxis = "Price", yaxis = "Frequencies", label=[s1 s2 s3 s4])

```

![Capture d’écran_2023-02-17_12-22-09](https://global.discourse-cdn.com/julialang/original/3X/6/a/6af498954ec8c6aa8677f2bb98b1afe70c37d637.png)

---

<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, 8:49am UTC](https://discourse.julialang.org/t/create-histogram-of-monthly-return-with-csv-and-dataframes/94729/7 "2023-02-17T08:49:50Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> the title is “Change %”, but it does not work

When there are spaces in the dataframes’ column names, we need to use a different syntax:

```julia
df[!,"Change %"] .= parse.(Float64, replace.(df[!,"Change %"], "%" => ""))
histogram(df[!,"Change %"])

```

Regarding the new question about formatting subplots, I recommend you to play with figure size, dpi, font size, margins, and the like, to get the most suitable results. Look for many similar posts on this topic and if nothing works, open a separate thread. As for the labels, you can define them individually for each subplot at the source.
