Plots.jl Documentation

repository·v2·Indexed 23 days ago

https://github.com/juliaplots/plots.jl

A comprehensive plotting ecosystem for the Julia programming language providing a unified API for visualizations. The ecosystem includes PlotsBase for core components, RecipesBase and RecipesPipeline for defining custom plot transformations via the @recipe macro, PlotThemes for visual styling, and specialized packages like GraphRecipes for network visualization and StatsPlots for statistical recipes and DataFrame integration using the @df macro.

Tokens
34.8K
Snippets
127
Records
191
Agent score
83%

What's inside Plots.jl

  1. Overview of the Plots.jl monorepo

    v2
    The Plots.jl monorepo contains the core Plots Julia package and its associated ecosystem. This includes essential dependencies and subpackages like RecipesBase, RecipesPipeline, and PlotsBase, as well as high-level tools built on top of the core framework such as GraphRecipes and StatsPlots.
  2. Overview of Plots.jl

    v2
    Plots is a plotting API and toolset for the Julia programming language. It is designed to provide a consistent, intuitive, and concise interface for creating complex visualizations. Instead of committing to a single graphics backend, Plots allows you to use your favorite plotting packages through a unified API, aiming to make complex visualizations easy to produce with minimal code.
  3. What is RecipesPipeline

    v2

    RecipesPipeline is a package designed to provide the machinery for translating plot recipes into full plot specifications. It was factored out of Plots.jl so that any plotting package can utilize the recipe pipeline.

    It works in conjunction with RecipesBase.jl, which is a lightweight package used to define "recipes" (plot specifications for user-defined types or custom plot types). RecipesPipeline.jl handles the actual translation of those recipes into something a plotting backend can understand.

    It is currently used by:

    • Plots.jl (v.1.1.0 and above)
    • MakieRecipes.jl (which bridges RecipesBase recipes to Makie.jl)
  4. What is RecipesPipeline and how does it relate to Plots.jl?

    v2

    RecipesPipeline is a standalone implementation of the recipe pipeline originally found in Plots.jl. It is designed to be used by any plotting package to handle the translation of plot recipes into full plot specifications.

    To use this system, you typically interact with two layers:

    1. RecipesBase: A lightweight package used to define "recipes" (plot specifications for user-defined types or custom plot types).
    2. RecipesPipeline: The machinery that takes those recipes and translates them into complete, actionable plot specifications.
  5. What is RecipesBase and how does it work with Plots.jl?

    v2

    RecipesBase is a lightweight, dependency-free package used to define custom visualizations via the @recipe macro. It acts as an interface layer that allows package developers to define how custom data types should be plotted without requiring a direct dependency on Plots.jl.

    When you define a recipe using @recipe, it creates a new method for RecipesBase.apply_recipe. Plots.jl calls this method recursively during its argument processing pipeline to handle custom series types, complex visualizations, or new plot types (such as histograms or bar plots).

  6. Understand the JuliaPlots Organization and Package Roles

    v2

    The JuliaPlots ecosystem is composed of several specialized packages. When contributing new functionality, aim to place it in the most appropriate package rather than adding everything to the core Plots package. This helps reduce the scope of the core library.

    Package Responsibilities:

    • Plots: The core package. Contains plot/plot! definitions, the core processing pipeline, base recipes (e.g., scatter, bar), generic output/layout/animation methods, and core types (Plot, Subplot, Axis, Series).
    • Backends: Code linking Plots to specific engines (like GR or Plotly). Backend-specific code should be contributed to the Plots/src/backends directory in the core Plots package.
    • RecipesBase: The essential foundation for creating third-party recipes. Use this if you want to define new recipes for custom types.
    • PlotUtils: Contains generic, reusable components like color conversions, color gradients, and tick computation.
    • PlotThemes: Manages visual themes (attribute defaults like "dark" or "orange").
    • StatsPlots: An extension for statistical plotting and tabular data (e.g., histograms, densities, correlation plots, and DataFrame support).
    • GraphRecipes: An extension of StatsPlots for graphs, maps, and related visualizations.
    • PlotDocs: The home for documentation, built using Documenter.jl.
  7. Use the @df macro for table-like data

    v2

    The @df macro allows you to pass columns of table-like structures (such as DataFrames, IndexedTables, or DataStreams) as symbols. This enables column manipulation inside the plot call as if they were normal arrays.

    Key features:

    • Column selection: Use symbols for single columns or arrays of symbols for multiple columns.
    • cols() utility: Use cols(range) to refer to a range of columns or cols(symbol) to refer to a specific column via a variable.
    • Escaping ambiguity: If a symbol is not a column name but is used in a plot argument (like colour), escape it using ^().
    • Query.jl integration: You can append @df to the end of a Query.jl pipeline.
    • Grouping: Compatible with Plots.jl grouping. Use a tuple of symbols for multiple columns. Use the curly bracket syntax group = {Name = :col} to provide custom legend names.
    using DataFrames, IndexedTables
    df = DataFrame(a = 1:10, b = 10 .* rand(10), c = 10 .* rand(10))
    @df df plot(:a, [:b :c], colour = [:red :blue])
    @df df scatter(:a, :b, markersize = 4 .* log.(:c .+ 0.1))
    
    t = table(1:10, rand(10), names = [:a, :b]) # IndexedTable
    @df t scatter(2 .* :b)
    
    # Using cols()
    @df df plot(:a, cols(2:3), colour = [:red :blue])
    s = :b
    @df df plot(:a, cols(s))
    
    # Escaping non-column symbols
    df[:red] = rand(10)
    @df df plot(:a, [:b :c], colour = ^([:red :blue]))
    
    # Query.jl integration
    using Query, StatsPlots
    df |>
        @filter(_.a > 5) |>
        @map({_.b, d = _.c-10}) |>
        @df scatter(:b, :d)
    
    # Grouping
    using RDatasets
    school = dataset("mlmRev", "Hsb82")
    @df school density(:MAch, group = :Sx)
    @df school density(:MAch, group = (:Sx, :Sector), legend = :topleft)
    @df school density(:MAch, group = {Sex = :Sx, :Sector})
  8. What are Plot Recipes and how to use them

    v2

    Recipes are extensions to the Plots.jl framework that add new functionality. They fall into four categories:

    1. User Recipes: Interpret plotting commands on new data types (e.g., the @df macro in StatsPlots.jl for DataFrames).
    2. Type Recipes: Provide default interpretations for specific Julia types (e.g., plotting Distributions.jl objects directly).
    3. Plot Recipes: Add new high-level plotting commands (e.g., marginalhist in StatsPlots.jl).
    4. Series Recipes: Add new types of series (e.g., violin or boxplot in StatsPlots.jl).

    To use these, you typically just need to using the library that provides the recipes (like using StatsPlots).

  9. Understand the Plots.jl ecosystem and recipe design

    v2

    The power of Plots.jl comes from its ecosystem, which is built on the design of RecipesBase. This architecture allows disparate packages to bind together into a cohesive user experience.

    Packages in the ecosystem typically interact with Plots.jl in two ways:

    1. Implementing Recipes: Packages create custom recipes to visualize their own specialized types (e.g., PhyloTrees.jl for phylogenetic trees or DifferentialEquations.jl for solver solutions).
    2. Extending Base Types: Packages extend the functionality of Plots.jl for standard Julia Base types.

    This design allows users to use a consistent plotting API regardless of whether they are visualizing machine learning models, differential equations, or biological data.

  10. Customize plots using keyword arguments

    v2

    Keyword arguments allow customization of plots, subplots, axes, and series.

    Key behaviors:

    • Aliases: Many arguments have short aliases (e.g., c for color, m for marker).
    • Matrix-type arguments: If an argument is a matrix, each column maps to a series. A vector is treated as an $n imes 1$ matrix.
    • Flexible types: Arguments like color accept strings/symbols (color names), Colors.Colorant objects, ColorScheme objects, ColorGradient symbols, or vectors of these types.
  11. Use Magic Arguments (Tuples) for bulk attribute setting

    v2

    Some arguments in Plots.jl act as "Magic Arguments." Instead of passing many individual keywords, you can pass a single Tuple of values. The library uses type checking and multiple dispatch to distribute these values to the correct underlying attributes.

    axis (and xaxis, yaxis, zaxis)

    Passing a tuple to an axis argument allows you to define xlabel, xlims, xticks, xscale, xflip, and xtickfont in one go.

    line (alias l)

    Sets attributes for a series line, including seriestype, linestyle, arrow, linealpha, linewidth, and linecolor.

    fill (aliases f, area)

    Sets attributes for a series fill area, including fillrange, fillalpha, and fillcolor.

    marker (aliases m, mark)

    Sets attributes for a series marker, including markershape, markersize, markeralpha, markercolor, markerstrokewidth, markerstrokealpha, markerstrokecolor, and markerstrokestyle.