# Plots.jl - Plot groups of data on different subplots

**URL:** <https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477>\
**Category:** Visualization\
**Tags:** question, dataframes, plots, statsplots\
**Created:** [January 12, 2022, 10:18am UTC](https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477 "2022-01-12T10:18:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cripcate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cripcate/32/34170_2.png) [@cripcate](https://discourse.julialang.org/u/cripcate)\
**Post date:** [January 12, 2022, 10:18am UTC](https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477/1 "2022-01-12T10:18:29Z")

</div>

Consider this sample data:

```julia
using DataFrames, Plots, Random

sample_data = DataFrame([rand(10), sample(['A', 'B', 'C'], 10)], ["value", "group"])

```

```julia
10×2 DataFrame
 Row │ value group 
     │ Float64 Char  
─────┼─────────────────
   1 │ 0.247725 C
   2 │ 0.638047 A
   3 │ 0.644689 A
   4 │ 0.285569 A
   5 │ 0.29109 B
   6 │ 0.719382 C
   7 │ 0.208312 C
   8 │ 0.405915 B
   9 │ 0.6137 B
  10 │ 0.129875 A

```

I can easily plot this data grouped by the `group` column in different colors within the same plot using:

```julia
 plot(sample_data.value, group=sample_data.group)

```

![different_colors](https://global.discourse-cdn.com/julialang/original/3X/e/0/e007193ac1d4caf9f132e97b636a3dea8fb53e74.png)

But if I want to plot the groups of data over different subplots, the shortest option I’ve found so far is this one:

```julia
plots = []
for group_data in groupby(sample_data, :group)
    push!(plots, plot(group_data.value))
end

plot(plots...)

```

![different_subplots](https://global.discourse-cdn.com/julialang/original/3X/4/f/4f14a5c17eb8642baca35a82ccdf93ef42026202.png)

This seems a bit overly complicated. I’m still new to Julia, so atm I don’t know if this can be improved. Python for example has within the `seaborn` package `sns.plot(sample_data, x='value', col='group')`. But I couldn’t find something similar for Plots.jl yet.

Perhaps someone knows how my solution to plotting datafarme groups over different subplots can be shortened or put into a similar one-liner as above.

**EDIT:** I already found a slightly better solution. List comprehensions of course!

```julia
plot([plot(group_data.value) for group_data in groupby(sample_data, :group)]...)

```

---

<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:** [January 12, 2022, 10:30am UTC](https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477/2 "2022-01-12T10:30:18Z")

</div>

I would have suggested the list comprehension as well - I don’t know of anything inbuilt in Plots (although that doesn’t mean it doesn’t exist!), so unless others chime in you can consider opening an issue over at the Plots.jl repo.

FWIW I don’t think it’s necessary to add this feature given how short and explicit the solution based on composing existing building blocks (list comprehension, splatting, `groupby` in DataFrames) is.

---

<div class="post-metadata">

**Author:** ![cripcate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cripcate/32/34170_2.png) [@cripcate](https://discourse.julialang.org/u/cripcate)\
**Post date:** [January 12, 2022, 11:54am UTC](https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477/3 "2022-01-12T11:54:18Z")

</div>

I also don’t think it’s necessary to add that feature. With the list comprehension method you can also easily change the arrangement using the `layout` argument, or add multiple list comprehensions or other plot calls easily. So it is quite flexible.

Only drawback I can see is that the code might get a bit overwhelming when adding different formatting and titles etc. for each plot.

---

<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:** [January 12, 2022, 12:22pm UTC](https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477/4 "2022-01-12T12:22:34Z")

</div>

Agreed - I think at this point you might be better off with the loop where you can be more explicit about things like this:

```julia
for (n, g) ∈ pairs(groupby(sample_data, :group))
    push!(plots, 
        plot(group_data.value, label = $(n.group), ...)
    )
end

```

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [October 27, 2022, 8:47am UTC](https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477/5 "2022-10-27T08:47:44Z")

</div>

> [@cripcate](#):
>
> `sample_data = DataFrame([rand(10), sample(['A', 'B', 'C'], 10)], ["value", "group"])`

FWIWthere is a built in version in Statsplots:

```julia
julia> using StatsPlots

julia> @df sample_data plot(:value, group = :group, layout = 3)

```

 ![Screenshot 2022-10-27 at 10.47.25](https://global.discourse-cdn.com/julialang/original/3X/0/9/09467e12822492804d62f58ca9480b89765ab3ed.png)

---

<div class="post-metadata">

**Author:** ![Pascal\_Buhler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal_buhler/32/45582_2.png) [@Pascal\_Buhler](https://discourse.julialang.org/u/Pascal_Buhler)\
**Post date:** [October 7, 2024, 9:10am UTC](https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477/6 "2024-10-07T09:10:07Z")

</div>

Could it be that it not works in Pluto?

@df plot(CHFadj(:timestamp, group = :adjclose, layout = 3))

---

<div class="post-metadata">

**Author:** ![Pascal\_Buhler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal_buhler/32/45582_2.png) [@Pascal\_Buhler](https://discourse.julialang.org/u/Pascal_Buhler)\
**Post date:** [October 28, 2024, 7:09am UTC](https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477/7 "2024-10-28T07:09:58Z")

</div>

Is there a way to make stacked plots of Time series of currencies like in Scilab?

---

<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:** [October 28, 2024, 8:22am UTC](https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477/8 "2024-10-28T08:22:31Z")

</div>

Yes almost certainly, but it would be really good if you could read

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

and take it to heart, specifically:

> 1. Avoid extending the original question that has already been answered with many new questions. It is better to start a new thread.

and

> 1. If you have a plotting (style) question, include a **plot with the desired output** (possibly generated with other software).

and

> 1. Do your best to make your example **self-contained** (“minimal working example”, **MWE** ), so that it runs (or gets to the error that you want help with) as is. […]

and

> 1. **Post** [**quoted code**](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) by enclosing code blocks in triple-backticks `````:

---

<div class="post-metadata">

**Author:** ![Pascal\_Buhler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal_buhler/32/45582_2.png) [@Pascal\_Buhler](https://discourse.julialang.org/u/Pascal_Buhler)\
**Post date:** [October 28, 2024, 8:47am UTC](https://discourse.julialang.org/t/plots-jl-plot-groups-of-data-on-different-subplots/74477/9 "2024-10-28T08:47:14Z")

</div>

👍
