• This repository has been archived on 21/Oct/2022
  • Stars
    star
    111
  • Rank 303,872 (Top 7 %)
  • Language
    Julia
  • License
    Other
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Base package for defining transformation recipes on user types for Plots.jl

WARNING: RecipesBase now lives in https://github.com/JuliaPlots/Plots.jl/tree/master/RecipesBase, please open issues here and PRs here.

RecipesBase

Build Status project chat deps

Author: Thomas Breloff (@tbreloff)

This package implements handy macros @recipe and @series which will define a custom transformation and attach attributes for user types. Its design is an attempt to simplify and generalize the summary and display of types and data from external packages. With no extra dependencies and minimal code, package authors can describe visualization routines that can be used as components in more complex visualizations.

This functionality is primarily geared to turning user types and settings into the data and attributes that describe a Plots visualization, though it could be used for other purposes as well. Plots has extensive machinery to uniquely take advantage of the simplified recipe description you define. See the Plots documentation on recipes for more information.

The @recipe macro will process a function definition, use --> commands to define attributes, and pass the return value through for further processing (likely by Plots.jl).

Why should I care about this package?

Many packages have custom types and custom data. There is usually specialized structure, and useful methods of visualizing that structure and data. This package solves the difficult problem of how to build generic visualizations of user-defined data types, without adding bulky dependencies on complex graphics packages.

This package is as lightweight as possible. It exports two macros, and defines only a few internal methods. It has zero dependencies.

However, although it is lightweight, it enables a lot. The entirety of the Plots framework becomes available to any package implementing a recipe. This means that complex plots and subplots can be built with uber-flexibility using custom combinations of data types. Some examples of applications:

  • Distributions: overlayed density plots for non-normal fitted distributions.
  • DataFrames: "Grammar of Graphics"-style inputs using symbols.
  • Deep Learning: frameworks for visualization of neural network states and tracking of internal calculations.
  • Graphs: flexible, interactive graphs with easily customizable colors, etc.
  • Symbolic frameworks: sample from complex symbolic distributions.

Really there's very little that couldn't be mapped to a useful visualization. I challenge you to create the pictures that are worth a thousand words.

For more information about Plots, see the docs, and be sure to reference the supported keywords. For additional examples of recipes in the wild, see PlotRecipes. Ask questions on gitter or in the issues.

Hello world

This will build a spiky surface:

using Plots; gr()
struct T end
@recipe f(::T) = rand(100,100)
surface(T())

A real example

using RecipesBase

# Our user-defined data type
struct T end

# This is all we define.  It uses a familiar signature, but strips it apart
# in order to add a custom definition to the internal method `RecipesBase.apply_recipe`
@recipe function plot(::T, n = 1; customcolor = :green)
    markershape --> :auto        # if markershape is unset, make it :auto
    markercolor :=  customcolor  # force markercolor to be customcolor
    xrotation   --> 45           # if xrotation is unset, make it 45
    zrotation   --> 90           # if zrotation is unset, make it 90
    rand(10,n)                   # return the arguments (input data) for the next recipe
end

# ----------------------------

# Plots will be the ultimate consumer of our recipe in this example
using Plots
gr()

# This call will implicitly call `RecipesBase.apply_recipe` as part of the Plots
# processing pipeline (see the Pipeline section of the Plots documentation).
#   It will plot 5 line plots (a 5-column matrix is returned from the recipe).
#   All will have black circles:
#       - user override for markershape: :c == :circle
#       - customcolor overridden to :black, and markercolor is forced to be customcolor
#   If markershape is an unsupported keyword, the call will error.
#   By default, a warning will be shown for an unsupported keyword.
#   This will be suppressed for zrotation (:quiet flag).
plot(T(), 5; customcolor = :black, shape=:c)

In this example, we see a lot of the machinery in action. We create a new type T, which we will use for dispatch, and an optional argument n, which will be used to determine the number of series to display. User-defined keyword arguments are passed through, and the --> command can be trailed by flags:

  • quiet: Suppress unsupported keyword warnings
  • require: Error if keyword is unsupported
  • force: Don't allow user override for this keyword

Series

For complex visualizations, it can be beneficial to create many series inside a single recipe. The @series macro will make a copy of the attribute dictionary d, and add a new RecipeData object to the returned list. See the case studies for more details.

Generated code

For the example above, the following code is generated. In it, you can see the managing of the scope of the keyword args, creation of a definition for RecipesBase.apply_recipe, setting attributes, and creating the list of RecipeData objects:

function RecipesBase.apply_recipe(d::Dict{Symbol,Any},::T,n=1)
    if RecipesBase._debug_recipes[1]
        println("apply_recipe args: ",Any[:(::T),:(n=1)])
    end
    begin
        customcolor = get!(d,:customcolor,:green)
    end
    series_list = RecipesBase.RecipeData[]
    func_return = begin
            get!(d,:markershape,:auto)
            d[:markercolor] = customcolor
            get!(d,:xrotation,45)
            get!(d,:zrotation,90)
            rand(10,n)
        end
    if func_return != nothing
        push!(series_list,RecipesBase.RecipeData(d,RecipesBase.wrap_tuple(func_return)))
    end
    begin
        RecipesBase.is_key_supported(:customcolor) || delete!(d,:customcolor)
    end
    series_list
end

A humble request

If you build a recipe for your package, please let me know! I'd love to compile both a gallery and a listing of user-defined recipes, as well as the packages that are available for Plots visualizations.

More Repositories

1

Plots.jl

Powerful convenience for Julia visualizations and data analysis
Julia
1,794
star
2

UnicodePlots.jl

Unicode-based scientific plotting for working in the terminal
Julia
1,392
star
3

Makie.jl

High level plotting on the GPU.
Julia
1,378
star
4

StatsPlots.jl

Statistical plotting recipes for Plots.jl
Julia
425
star
5

PlotlyJS.jl

Julia library for plotting with plotly.js
Julia
404
star
6

AlgebraOfGraphics.jl

Combine ingredients for a plot
Julia
211
star
7

GraphRecipes.jl

Graph-related recipes to be used with Plots.jl
Julia
164
star
8

PlotThemes.jl

Themes for the Julia plotting package Plots.jl
Julia
117
star
9

PlotDocs.jl

Documentation for Plots.jl
92
star
10

GeoMakie.jl

Geographical plotting utilities for Makie.jl
Julia
87
star
11

AbstractPlotting.jl

An abstract interface for plotting libraries, part of the Makie ecosystem.
Julia
85
star
12

GraphMakie.jl

Plotting graphs with Makie
Julia
80
star
13

ExamplePlots.jl

Collection of examples and recipes for Plots.jl
Jupyter Notebook
79
star
14

MakieTeX.jl

TeX integration in Makie
Julia
76
star
15

WGLMakie.jl

WebGL backend for Makie
Julia
59
star
16

StatsMakie.jl

Statistical visualizations based on high performance plotting package Makie
Julia
48
star
17

MakieGallery.jl

Documentation and Example Gallery for Makie
Julia
47
star
18

GLMakie.jl

OpenGL backend for Makie
Julia
46
star
19

GGPlots.jl

Grammar of Graphics interface to Plots.jl
Julia
40
star
20

CairoMakie.jl

Cairo backend for Makie
Julia
37
star
21

PlotUtils.jl

Generic helper algorithms for building plotting components
Julia
34
star
22

VisualRegressionTests.jl

Automated integrated regression tests for graphics libraries
Julia
27
star
23

PlotlyKaleido.jl

Julia
17
star
24

RecipesPipeline.jl

Utilities for processing recipes
Julia
17
star
25

MakieThemes.jl

Themes for Makie
Julia
16
star
26

VennEuler.jl

Venn/Euler Diagrams for Julia
Julia
10
star
27

MakieRecipes.jl

Extending Makie to support plotting Plots.jl recipes
Julia
10
star
28

MakieCore.jl

The core recipe functions for Makie.jl - basically Makie light!
Julia
6
star
29

GRMakie.jl

GR backend for Makie
Julia
4
star
30

MakieDocumentation

Holds documentation builds generated from source files stored in AbstractPlotting.jl
HTML
2
star
31

MakieReferenceImages

Reference images for Makie.jl (generated by MakieGallery.jl)
HTML
2
star
32

juliaplots.github.io

Website for Plots.jl
HTML
1
star
33

PlotReferenceImages.jl

Julia
1
star