# Recipe to plot simple dataframes

**URL:** https://discourse.julialang.org/t/recipe-to-plot-simple-dataframes/64889
**Category:** General Usage
**Tags:** plotting, dataframes, recipe
**Created:** [July 18, 2021, 11:26pm UTC](https://discourse.julialang.org/t/recipe-to-plot-simple-dataframes/64889 "2021-07-18T23:26:23Z")
**Posts on this page:** 8
**Page:** 1

<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: [July 18, 2021, 11:26pm UTC](https://discourse.julialang.org/t/recipe-to-plot-simple-dataframes/64889/1 "2021-07-18T23:26:23Z")

</div>

Looking for a default recipe for plotting simple dataframes with `plot(df)`.

Two questions:

1. Does such recipe currently exist?
2. Is something like the code snippet below a valid Plots’ recipe?

```julia
using DataFrames, Plots; gr()
import RecipesBase.plot

plot(df::DataFrame) = Plots.plot(Matrix(df[:,2:end]), label=permutedims(names(df[:,2:end])), xlabel=names(df)[1])

t = 3*pi :-0.2:0
df = DataFrame(time=t, temperature= sin.(t), pressure=t.^2/30)
plot(df)

```

Thank you.

---

<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: [July 19, 2021, 5:36am UTC](https://discourse.julialang.org/t/recipe-to-plot-simple-dataframes/64889/2 "2021-07-19T05:36:50Z")

</div>

I’m not aware of any DataFrames recipes other than the ones in StatsBase. I think a recipe as simple as yours might not exist because it assumes a very specific structure of your data (all columns except the first numeric, first column holding x labels) which is probably not present in most situations.

---

<div class="post-metadata">

### Author: ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)
#### Post date: [July 19, 2021, 7:05am UTC](https://discourse.julialang.org/t/recipe-to-plot-simple-dataframes/64889/3 "2021-07-19T07:05:22Z")

</div>

There is one here: [https://github.com/viraltux/Forecast.jl/blob/main/src/plot\_DataFrame.jl](https://github.com/viraltux/Forecast.jl/blob/main/src/plot_DataFrame.jl)

In this case it expects the first column to be a time type; first it checks its format and if the first column is not a time type it adds a column to the DataFrame with a default time type. Then it proceeds to plot it.

If instead a time type we want labels in the first column, we could just do the same and add a default label column and then plot it, or just restrict the plot to those DataFrame values with the proper format and ignore the others.

---

<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: [July 19, 2021, 7:09am UTC](https://discourse.julialang.org/t/recipe-to-plot-simple-dataframes/64889/4 "2021-07-19T07:09:12Z")

</div>

Thanks Nils.  
For the 2nd question, wanted to know if that is all one need to do to create a method to plot dataframes. It needs further bells and whistles, for sure.

_NB:_  
_There are no labels on the first column, the simple dataframe example provided looks like this:_

```julia
 Row │ time temperature pressure   
     │ Float64 Float64 Float64    
─────┼───────────────────────────────────
   1 │ 9.42478 3.67394e-16 2.96088
   2 │ 9.22478 0.198669 2.83655
   3 │ 9.02478 0.389418 2.71489
   4 │ 8.82478 0.564642 2.59589
   5 │ 8.62478 0.717356 2.47956
   6 │ 8.42478 0.841471 2.3659
  ⋮ │ ⋮ ⋮ ⋮

```

---

<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: [July 19, 2021, 7:15am UTC](https://discourse.julialang.org/t/recipe-to-plot-simple-dataframes/64889/5 "2021-07-19T07:15:59Z")

</div>

@viraltux, thanks for the comprehensive example.  
It seems to call an external function `tots(df)`?  
More importantly, once the `@recipe function f()` has been defined, how do you use it, as simply as `plot(df)`?

---

<div class="post-metadata">

### Author: ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)
#### Post date: [July 19, 2021, 7:21am UTC](https://discourse.julialang.org/t/recipe-to-plot-simple-dataframes/64889/6 "2021-07-19T07:21:31Z")

</div>

`tots` stands for “TO Time Series” you can find its details here: [Forecast.jl/src/utils\_datetime.jl at main · viraltux/Forecast.jl · GitHub](https://github.com/viraltux/Forecast.jl/blob/main/src/utils_datetime.jl)

However if you are just to plot numeric values you don’t need to add anything.

> [@rafael.guerra](#):
>
> More importantly, once the `@recipe function f()` has been defined, how do you use it, as simply as `plot(df)` ?

Yes, you simply

```julia
plot(df)

```

and _Voilà_

---

<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: [July 19, 2021, 7:44am UTC](https://discourse.julialang.org/t/recipe-to-plot-simple-dataframes/64889/7 "2021-07-19T07:44:54Z")

</div>

@viraltux, it works like a charm.

Your `@recipe` allows providing additional plot arguments and customize the plot as usual, e.g.: `plot(df; lc=[:reds :blues], mc=[:red :blue])`

Brilliant, thanks!

 ![Plots_dataframes_recipe](https://global.discourse-cdn.com/julialang/original/3X/5/3/53d90ab7f520d9370c94b80679c03fd8c6009cb2.png)

---

<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: [July 19, 2021, 7:57am UTC](https://discourse.julialang.org/t/recipe-to-plot-simple-dataframes/64889/8 "2021-07-19T07:57:22Z")

</div>

FWIW this is actually reasonably well documented:

[http://juliaplots.org/RecipesBase.jl/stable/syntax/](http://juliaplots.org/RecipesBase.jl/stable/syntax/)

One thing to note is that I believe recipes should generally use `-->` to set plot kwargs, not `:=`: as stated in the docs, `:=` forces a certain attribute, which means the user can’t overwrite this, while `-->` sets a default which can be changed if the user passes that kwarg. Imho `:=` should therefore only be used where a plot doesn’t make sense if a certain attribute is changed, or if you absolutely want to enforce a specific look (e.g. writing a recipe for an organisation with a corporate color scheme or something)
