# Plot a column of 1D arrays from DataFrame using Gadfly

**URL:** <https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688>\
**Category:** General Usage\
**Tags:** gadfly\
**Created:** [August 19, 2021, 6:49pm UTC](https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688 "2021-08-19T18:49:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [August 19, 2021, 6:49pm UTC](https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688/1 "2021-08-19T18:49:29Z")

</div>

I have a DataFrame with columns `df.x` and `df.y`. Each row has a 1D array.

Can I use Gadfly.jl to plot this data simply? Seems I need to extract each row one-at-a-time first, as in

```julia
x = df.x[1]
y = df.y[1]
plot(x=x, y=y, Geom.line)

```

Is there something condensed similar to the following that could work to get each row in a plot?

```julia
plot(df, x=:x, y=:y, Geom.line)
```

---

<div class="post-metadata">

**Author:** ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)\
**Post date:** [August 19, 2021, 7:12pm UTC](https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688/2 "2021-08-19T19:12:29Z")

</div>

that’s not an idiomatic use of data frames, so no, gadfly does not support that. try flattening the data frame by putting the vector elements into separate rows.

---

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [August 19, 2021, 8:13pm UTC](https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688/3 "2021-08-19T20:13:18Z")

</div>

Thank you! It seems like that helps. I also have a column of ID codes for each row (`:id`). So the following works:

```julia
plot(flatten(df,[:x,:y]), x=:x, y=:y, color=:id, Geom.line)

```

Followup question:  
If I don’t specify color, it comes out a mess tracing a single line over all the data.

How can I get a separate line for each ID but all the same color?

---

<div class="post-metadata">

**Author:** ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)\
**Post date:** [August 19, 2021, 8:27pm UTC](https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688/4 "2021-08-19T20:27:20Z")

</div>

try `Scale.color_discrete_hue`. [Scales · Gadfly.jl](http://gadflyjl.org/stable/lib/scales/#Gadfly.Scale.color_discrete_hue)

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [August 19, 2021, 10:22pm UTC](https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688/5 "2021-08-19T22:22:16Z")

</div>

> How can I get a separate line for each ID but all the same color?

Try e.g. `group=:id, color=[colorant"black"]`

---

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [August 20, 2021, 3:02pm UTC](https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688/6 "2021-08-20T15:02:34Z")

</div>

That’s just what I needed, thanks!

I’ll also add that the color palettes from Plots.jl work with this. It seems palettes aren’t natively part of Gadfly.jl (e.g. Crameri color maps)? The commands that work best for me in the case where I have 3 columns representing y axes to plot are:

```julia
import Plots:palette
using Gadfly

pal = palette(:batlow,3)
cols = [:y1, :y2, :y3]
plot_stack = 
    [plot(flatten(df, [:x, cols[idx]]), x=:x, y=cols[idx], group=:id, color=[pal[idx]], Geom.line) for idx in 1:3]
title(vstack(plot_stack),"Title",fill(colorant"black"))

```

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [August 20, 2021, 9:26pm UTC](https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688/7 "2021-08-20T21:26:37Z")

</div>

In Julia, colorschemes come from the package [ColorSchemes.jl](https://juliagraphics.github.io/ColorSchemes.jl/stable/basics/).  
In Gadfly, we don’t add ColorSchemes as a dependency.  
ColorSchemes,jl doesn’t have a Plots-like palette function, but you can roll your own:

```julia
import ColorSchemes as cschemes
palette(cs, n) = cschemes.colorschemes[cs][range(0,1,n)]
# palette(:batlow, 3)

```

If your plot\_stack has the same x-axis, you could also use [Geom.subplot\_grid](http://gadflyjl.org/stable/gallery/geometries/#Gallery_Geom.subplot_grid) e.g.

```julia
using RDatasets

iris = dataset("datasets", "iris")[:,Not(4)]
iris[!,:x] = 10*rand(150)
df = stack(iris, Not([:x, :Species]))

plot(df, x=:x, y=:value, group=:Species, ygroup=:variable,color=:variable,
    Geom.subplot_grid(Geom.line, free_y_axis=true),
    Scale.color_discrete_manual(palette(:batlow, 3)...)
)

```

 ![iris](https://global.discourse-cdn.com/julialang/original/3X/2/6/26f6ac48eaab9dfd3f54caebab9d6706c0781e90.png)

---

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [August 23, 2021, 4:11pm UTC](https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688/8 "2021-08-23T16:11:00Z")

</div>

When I was browsing the docs, I came across the `title` method using `vstack` and `hstack` first before seeing the title guide. The method works well with the array comprehension over the flattened columns. `subplot_grid` looks good, too, but I’m not sure how to use it succinctly with my `df` that needs to be flattened over the `cols` to plot properly. Do you have a suggestion on how to use `subplot_grid` with this, so I can use just the bottom x-axis? Seems to me that I’d need to add all the flattened data from each of my y-columns to the same column, and label each row by its previous column name for `ygroup`.

I’m also interested in applying transparency to the lines. I see in an [older post](https://discourse.julialang.org/t/gadfly-how-to-change-width-and-opacity-of-lines-in-geom-density/37823), there was no alpha aesthetic for `Geom.line`. This is still the case? Is there a convenient way you’d recommend adding that with the named color scheme?

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [August 24, 2021, 1:17pm UTC](https://discourse.julialang.org/t/plot-a-column-of-1d-arrays-from-dataframe-using-gadfly/66688/9 "2021-08-24T13:17:54Z")

</div>

> Seems to me that I’d need to add all the flattened data from each of my y-columns to the same column, and label each row by its previous column name for `ygroup`.

Yes, and that’s what [stack](https://dataframes.juliadata.org/stable/lib/functions/#DataFrames.stack) (in DataFrames.jl) does (also used in my example above).

Unfortunately the `alpha` aesthetic has not yet been implemented for `Geom.line`. The issue lies not in Gadfly, but in the graphics package that Gadfly is built on (Compose.jl). See this [related post](https://github.com/GiovineItalia/Compose.jl/issues/386#issuecomment-586451902).

> Is there a convenient way you’d recommend adding that with the named color scheme?

If you mean to use with `Geom.line`, that wouldn’t work until `strokeopacity` in Compose.jl is fixed.
