logo

Omitting DataFrame Names in Julia StatsPlots with Macro @df 📂Julia

Omitting DataFrame Names in Julia StatsPlots with Macro @df

Overview

In the StatsPlots package of Julia, the @df macro allows omitting the repeatedly mentioned dataframe name when plotting1. The syntax for using the macro, when using column a of dataframe X, is to specify which dataframe to use with @df X, followed immediately by passing the argument a as a symbol :a in the scope that follows, writing it as plot (:a). In summary, the code is written as @df X plot(:a).

Code

Below is a scatter plot using SepalLength and SepalWidth from the Iris data.

plot.svg

The following code scatter(iris.SepalLength, iris.SepalWidth) and @df iris scatter(:SepalLength, :SepalWidth) are equivalent.

using RDatasets
iris = dataset("datasets", "iris")

using StatsPlots
scatter(iris.SepalLength, iris.SepalWidth)
@df iris scatter(:SepalLength, :SepalWidth)

Environment

  • OS: Windows
  • julia: v1.8.3
  • StatsPlots v0.15.4