Recipe to plot simple dataframes

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?
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.

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.

1 Like

There is one here: Forecast.jl/plot_DataFrame.jl at main · viraltux/Forecast.jl · GitHub

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.

2 Likes

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:

 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
  ⋮  │    ⋮           ⋮           ⋮

@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)?

tots stands for “TO Time Series” you can find its details here: Forecast.jl/utils_datetime.jl at main · viraltux/Forecast.jl · GitHub

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

Yes, you simply

plot(df)

and Voilà

2 Likes

@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!

1 Like

FWIW this is actually reasonably well documented:

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)

2 Likes