# How to plot histogram subplots of a DataFrame?

**URL:** <https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355>\
**Category:** General Usage\
**Tags:** question, plotting, dataframes, plots, statsplots\
**Created:** [March 23, 2022, 9:08pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355 "2022-03-23T21:08:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![folleyz](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@folleyz](https://discourse.julialang.org/u/folleyz)\
**Post date:** [March 23, 2022, 9:08pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/1 "2022-03-23T21:08:24Z")

</div>

Hello Everybody!

I have a question, i’m refatoring my old python projects to Julia and i have a doubt in a situation.

Looks how i plot using python matplotlib:

```julia
df.hist(figsize=(12,10));

```

 ![hist](https://global.discourse-cdn.com/julialang/original/3X/7/1/71b80c1e3a07760160905d12d13ade3f2d8160c0.png)

And now using Julia StatsPlots

```julia
begin 	

	p1 = Plots.histogram(df.Age; 
		plot_title = "Age",plot_titlefontsize = 11)
	p2 = Plots.histogram(df.Balance; 
		plot_title = "Balance",plot_titlefontsize = 11)
	p3 = Plots.histogram(df.CreditScore; 
		plot_title = "CreditScore",plot_titlefontsize = 11)
	p4 = Plots.histogram(df.CustomerId; 
		plot_title = "CustomerId",plot_titlefontsize = 11)
	p5 = Plots.histogram(df.EstimatedSalary; 
		plot_title = "EstimatedSalary",plot_titlefontsize = 11)
	p6 = Plots.histogram(df.Exited; 
		plot_title = "Exited",plot_titlefontsize = 11)
	p7 = Plots.histogram(df.HasCrCard; 
		plot_title = "HasCrCard",plot_titlefontsize = 11)
	p8 = Plots.histogram(df.IsActiveMember; 
		plot_title = "IsActiveMember",plot_titlefontsize = 11)
	p9 = Plots.histogram(df.NumOfProducts; 
		plot_title = "NumOfProducts",plot_titlefontsize = 11)
	p10 = Plots.histogram(df.RowNumber; 
		plot_title = "RowNumber",plot_titlefontsize = 11)
	p11 = Plots.histogram(df.Tenure; 
		plot_title = "Tenure",plot_titlefontsize = 11)
	

	@df df Plots.plot(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11;
		layout = 11,
		size=(1000,600)
	)
end

```

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

How can i improve the Julia code to get that same result without writing a lot of code?

Thx for Help!

and sorry, I’m Julia newbie

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [March 23, 2022, 9:50pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/2 "2022-03-23T21:50:40Z")

</div>

Hey! Would using the legend as the column label work instead? That way we could just do something like this:

```julia
using DataFrames, StatsPlots

df = DataFrame(randn(100, 3), [:Tenure, :Balance, :CreditScore])

N = ncol(df)

@df df histogram(cols(1:N); layout=N)

```

![index](https://global.discourse-cdn.com/julialang/original/3X/6/4/645b0750f979c1ea2d772fabae5372fb9d72d9ef.png)

---

<div class="post-metadata">

**Author:** ![folleyz](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@folleyz](https://discourse.julialang.org/u/folleyz)\
**Post date:** [March 23, 2022, 10:03pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/3 "2022-03-23T22:03:45Z")

</div>

Thx @icweaver !

I tried that but returned an error.

```julia
begin
	N = ncol(df)

	@df df Plots.histogram(cols(1:N); layout=N)
end

```

```julia
Cannot convert Matrix{Any} to series data for plotting

    error(::String)@error.jl:33
    _prepare_series_data(::Matrix{Any})@series.jl:8
    _series_data_vector(::Matrix{Any}, ::Dict{Symbol, Any})@series.jl:27
    macro expansion@series.jl:127[inlined]
    apply_recipe(::AbstractDict{Symbol, Any}, ::Type{RecipesPipeline.SliceIt}, ::Any, ::Any, ::Any)@RecipesBase.jl:289
    _process_userrecipes!(::Any, ::Any, ::Any)@user_recipe.jl:36
    recipe_pipeline!(::Any, ::Any, ::Any)@RecipesPipeline.jl:70
    _plot!(::Plots.Plot, ::Any, ::Any)@plot.jl:208
    #plot#135@plot.jl:91[inlined]
    var"#histogram#419"(::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}}, ::typeof(Plots.histogram), ::Any)@RecipesBase.jl:410
    var"#add_label#19"(::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:layout,), Tuple{Int64}}}, ::typeof(StatsPlots.add_label), ::Vector{Matrix{Symbol}}, ::Function, ::Matrix{Any})@df.jl:155
    (::var"#405#406"{Module, Colon, Int64})(::DataFrames.DataFrame)@none:0
    top-level scope@Local: 4[inlined]

```

What’s my mystake?

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [March 23, 2022, 10:08pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/4 "2022-03-23T22:08:21Z")

</div>

That is very odd. Could you share a [MWE](https://stackoverflow.com/help/minimal-reproducible-example), including the `df` you are using?

Edit: whoops, linked to to the wrong example above. Should be updated now

---

<div class="post-metadata">

**Author:** ![folleyz](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@folleyz](https://discourse.julialang.org/u/folleyz)\
**Post date:** [March 23, 2022, 10:26pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/5 "2022-03-23T22:26:22Z")

</div>

Here’s a df sample:

```julia
first(df,5)

```

| | RowNumber | CustomerId | Surname | CreditScore | Geography | Gender | Age | Tenure | Balance | NumOfProducts | HasCrCard | IsActiveMember | EstimatedSalary | Exited |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| | Int64 | Int64 | String31 | Int64 | String7 | String7 | Int64 | Int64 | Float64 | Int64 | Int64 | Int64 | Float64 | Int64 |
| 1 | 1 | 15634602 | Hargrave | 619 | France | Female | 42 | 2 | 0.0 | 1 | 1 | 1 | 1,01E+10 | 1 |
| | | | | | | | | | | | | | | |
| 2 | 2 | 15647311 | Hill | 608 | Spain | Female | 41 | 1 | 83807.9 | 1 | 0 | 1 | 1,13E+10 | 0 |
| | | | | | | | | | | | | | | |
| 3 | 3 | 15619304 | Onio | 502 | France | Female | 42 | 8 | 1,60E+10 | 3 | 1 | 0 | 1,14E+10 | 1 |
| | | | | | | | | | | | | | | |
| 4 | 4 | 15701354 | Boni | 699 | France | Female | 39 | 1 | 0.0 | 2 | 0 | 0 | 93826.6 | 0 |
| | | | | | | | | | | | | | | |
| 5 | 5 | 15737888 | Mitchell | 850 | Spain | Female | 43 | 2 | 1,26E+10 | 1 | 1 | 1 | 79084.1 | 0 |
| | | | | | | | | | | | | | | |

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [March 23, 2022, 10:33pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/6 "2022-03-23T22:33:57Z")

</div>

Thanks, but I just meant a copy-and-pasteable code snippet that I can run to try and reproduce your error please =]

The package versions that you are using would also be really helpful! To work from a clean environment you can do the following:

```julia
julia> ]
pkg> activate --temp
(jl_jTP8xX) pkg> add <list of packages used here>
julia> <MWE here>

```

Then you can view the list of packages and the versions that were used by just typing `st` (short for status) into the packge repl (shown by the prompt that started with `pkg>`):

```julia
julia> ]
(jl_jTP8xX) pkg> st
<list of packages used should display here>

```

---

<div class="post-metadata">

**Author:** ![folleyz](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@folleyz](https://discourse.julialang.org/u/folleyz)\
**Post date:** [March 23, 2022, 10:47pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/7 "2022-03-23T22:47:40Z")

</div>

Code used

```julia
begin
	N = ncol(df)

	@df df Plots.histogram(cols(1:N); layout=N)
end

```

Error:

```julia
Cannot convert Matrix{Any} to series data for plotting

    error(::String)@error.jl:33
    _prepare_series_data(::Matrix{Any})@series.jl:8
    _series_data_vector(::Matrix{Any}, ::Dict{Symbol, Any})@series.jl:27
    macro expansion@series.jl:127[inlined]
    apply_recipe(::AbstractDict{Symbol, Any}, ::Type{RecipesPipeline.SliceIt}, ::Any, ::Any, ::Any)@RecipesBase.jl:289
    _process_userrecipes!(::Any, ::Any, ::Any)@user_recipe.jl:36
    recipe_pipeline!(::Any, ::Any, ::Any)@RecipesPipeline.jl:70
    _plot!(::Plots.Plot, ::Any, ::Any)@plot.jl:208
    #plot#135@plot.jl:91[inlined]
    var"#histogram#419"(::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}}, ::typeof(Plots.histogram), ::Any)@RecipesBase.jl:410
    var"#add_label#19"(::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:layout,), Tuple{Int64}}}, ::typeof(StatsPlots.add_label), ::Vector{Matrix{Symbol}}, ::Function, ::Matrix{Any})@df.jl:155
    (::var"#405#406"{Module, Colon, Int64})(::DataFrames.DataFrame)@none:0
    top-level scope@Local: 4[inlined]

```

Packages

```julia
(jl_tJabzV) pkg> st
      Status `C:\Users\Pedro Henrique\AppData\Local\Temp\jl_tJabzV\Project.toml`
  [336ed68f] CSV v0.10.3
  [a93c6f00] DataFrames v1.3.2
  [91a5bcdd] Plots v1.27.2
  [2913bbd2] StatsBase v0.33.16
  [f3b207a7] StatsPlots v0.14.33

```

I think i found the problem, and is in the non numerical columns.

```julia
begin
	N = ncol(df)

	@df df Plots.histogram(cols(7:N); layout=N)
end

```

![image](https://global.discourse-cdn.com/julialang/original/3X/8/3/83bcfe9aec902e7e26ddcd8093016d75fc152a22.png)

Above i used only columns with numerical values to test and works!

Sorry about all the trouble, I’m not in the area of computer science

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [March 23, 2022, 10:50pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/8 "2022-03-23T22:50:44Z")

</div>

No worries at all, great find!

---

<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:** [March 23, 2022, 11:25pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/10 "2022-03-23T23:25:51Z")

</div>

For the original question, you could also feed the titles to the `@df` macro to obtain:

 ![StatsPlots_subplots_titles](https://global.discourse-cdn.com/julialang/original/3X/5/6/56e01417e95b55ed9fbc7642d3a0fcea2e3ac390.png)

> **CODE**
>
> ```julia
> using DataFrames, StatsPlots
> 
> colnames = [:Age, :Balance, :CreditScore, :CustomerId, :EstimatedSalary, :Exited,
> :HasCrCard, :IsActiveMember, :NumOfProducts, :RowNumber, :tenure]
> N = length(colnames)
> df = DataFrame(randn(100, N), colnames)
> 
> @df df histogram(cols(1:N); layout=N, legend=false, title=permutedims(colnames),
> size=(1000,600), frame=:box, titlefontsize=11, c=:blues)
> 
> ```

---

<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:** [March 23, 2022, 11:28pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/11 "2022-03-23T23:28:16Z")

</div>

> [@folleyz](#):
>
> seaborn countplot

It is recommended to start a new topic when question is unrelated.

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [March 23, 2022, 11:31pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/12 "2022-03-23T23:31:25Z")

</div>

As @rafael.guerra mentioned, we would be more than happy to follow up with this in a new thread! It just helps others find the relevant bits of the discussion in the future =]

---

<div class="post-metadata">

**Author:** ![folleyz](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@folleyz](https://discourse.julialang.org/u/folleyz)\
**Post date:** [March 23, 2022, 11:40pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-subplots-of-a-dataframe/78355/13 "2022-03-23T23:40:54Z")

</div>

Nice @rafael.guerra and @icweaver !

With your help i learned a lot and created a very simple function who solves my question.

```julia
function plot_df(df::AbstractDataFrame,graph_type::Symbol)
	numerical_cols = Symbol.(names(df,Real))

	@df df Plots.plot(cols(numerical_cols); layout = length(numerical_cols), seriestype = graph_type,
	title=permutedims(numerical_cols),
	size=(1200,600),
	frame=:box)
end

```

```julia
plot_df(df,:histogram)

```

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

I really appreciate your collaboration, and i will make a new topic for the countplot!

Thx!
