Themes

Many parameters controlling the appearance of plots can be overridden by passing a Theme object to the plot function. Or setting the Theme as the current theme using push_theme (see also pop_theme and with_theme below).

The constructor for Theme takes zero or more named arguments each of which overrides the default value of the field.

The Theme stack

Gadfly maintains a stack of themes and applies theme values from the topmost theme in the stack. This can be useful when you want to set a theme for multiple plots and then switch back to a previous theme.

push_theme(t::Theme) and pop_theme() will push and pop from this stack respectively. You can use with_theme(f, t::Theme) to set a theme as the current theme and call f().

style

You can use style to override the fields on top of the current theme at the top of the stack. style(...) returns a Theme. So it can be used with push_theme and with_theme.

Parameters

These parameters can either be used with Theme or style

Examples

dark_panel = Theme(
    panel_fill=colorant"black",
    default_color=colorant"orange"
)

plot(x=rand(10), y=rand(10), dark_panel)
x 0.00 0.25 0.50 0.75 1.00 0.0 0.5 1.0 y

Setting the font to Computer Modern to create a LaTeX-like look, and choosing a font size:

Gadfly.push_theme(dark_panel)

p = plot(x=rand(10), y=rand(10),
     style(major_label_font="CMU Serif",minor_label_font="CMU Serif",
           major_label_font_size=16pt,minor_label_font_size=14pt))

# can plot more plots here...

Gadfly.pop_theme()
x 0.0 0.5 1.0 0.0 0.2 0.4 0.6 0.8 1.0 y

Same effect can be had with with_theme

Gadfly.with_theme(dark_panel) do

  plot(x=rand(10), y=rand(10),
       style(major_label_font="CMU Serif",minor_label_font="CMU Serif",
             major_label_font_size=16pt,minor_label_font_size=14pt))
end

or

Gadfly.push_theme(dark_panel)

Gadfly.with_theme(
       style(major_label_font="CMU Serif",minor_label_font="CMU Serif",
             major_label_font_size=16pt,minor_label_font_size=14pt)) do

  plot(x=rand(10), y=rand(10))

end

Gadfly.pop_theme()

Named themes

To register a theme by name, you can extend Gadfly.get_theme(::Val{:theme_name}) to return a Theme object.

Gadfly.get_theme(::Val{:orange}) =
    Theme(default_color=colorant"orange")

Gadfly.with_theme(:orange) do
  plot(x=[1:10;], y=rand(10), Geom.bar)
end
x 0 5 10 15 0.00 0.25 0.50 0.75 1.00 y

Gadfly comes built in with 2 named themes: :default and :dark. You can also set a theme to use by default by setting the GADFLY_THEME environment variable before loading Gadfly.

The Dark theme

This is one of the two themes the ship with Gadfly the other being :default. Here are a few plots that use the dark theme.

Gadfly.push_theme(:dark)
plot(dataset("datasets", "iris"),
    x="SepalLength", y="SepalWidth", color="Species", Geom.point)
SepalLength 4 5 6 7 8 setosa versicolor virginica Species 2.0 2.5 3.0 3.5 4.0 4.5 SepalWidth
using RDatasets

gasoline = dataset("Ecdat", "Gasoline")

plot(gasoline, x=:Year, y=:LGasPCar, color=:Country,
         Geom.point, Geom.line)
Year 1960 1965 1970 1975 1980 JAPAN NETHERLA NORWAY SPAIN SWEDEN SWITZERL TURKEY U.K. U.S.A. AUSTRIA BELGIUM CANADA DENMARK FRANCE GERMANY GREECE IRELAND ITALY Country 3 4 5 6 7 LGasPCar
using DataFrames

xs = 0:0.1:20

df_cos = DataFrame(
    x=xs,
    y=cos(xs),
    ymin=cos(xs) .- 0.5,
    ymax=cos(xs) .+ 0.5,
    f="cos"
)

df_sin = DataFrame(
    x=xs,
    y=sin(xs),
    ymin=sin(xs) .- 0.5,
    ymax=sin(xs) .+ 0.5,
    f="sin"
)

df = vcat(df_cos, df_sin)
p = plot(df, x=:x, y=:y, ymin=:ymin, ymax=:ymax, color=:f, Geom.line, Geom.ribbon)
x 0 5 10 15 20 cos sin f -2 -1 0 1 2 y
using Distributions

X = rand(MultivariateNormal([0.0, 0.0], [1.0 0.5; 0.5 1.0]), 10000);
plot(x=X[1,:], y=X[2,:], Geom.hexbin(xbincount=100, ybincount=100))
x -5 0 5 40 10 30 1 20 Count -4 -2 0 2 4 y
Gadfly.pop_theme()
Gadfly.Theme(RGB{N0f8}(0.996,0.263,0.396),0.9mm,0.3mm,nothing,RGB{N0f8}(0.133,0.157,0.192),nothing,1.0,RGB{N0f8}(0.133,0.157,0.192),5.0mm,RGB{N0f8}(0.341,0.341,0.341),Measures.Length{:mm,Float64}[0.5mm,0.5mm],RGB{N0f8}(0.627,0.627,0.627),0.2mm,"'PT Sans Caption','Helvetica Neue','Helvetica',sans-serif",2.822222222222222mm,RGB{N0f8}(0.631,0.631,0.631),"'PT Sans','Helvetica Neue','Helvetica',sans-serif",3.880555555555555mm,RGB{N0f8}(0.631,0.631,0.631),"'PT Sans Caption','Helvetica Neue','Helvetica',sans-serif",2.822222222222222mm,RGB{N0f8}(0.298,0.251,0.294),"'PT Sans','Helvetica Neue','Helvetica',sans-serif",3.880555555555555mm,RGB{N0f8}(0.631,0.631,0.631),"'PT Sans','Helvetica Neue','Helvetica',sans-serif",2.822222222222222mm,RGB{N0f8}(0.631,0.631,0.631),40,-0.05mm,1.0mm,3.0mm,Gadfly.default_stroke_color,0.3mm,Gadfly.border_color,Gadfly.default_continuous_highlight_color,Gadfly.lowlight_color,0.6,Gadfly.default_middle_color,0.6mm,:left,:square,:right,nothing,Function[Compose.circle,Gadfly.square,Gadfly.diamond,Gadfly.cross,Gadfly.xcross,Gadfly.utriangle,Gadfly.dtriangle,Gadfly.star1,Gadfly.star2,Gadfly.hexagon,Gadfly.octagon],2.0mm,1000,10.0,0.5,0.2,4,Gadfly.Scale.DiscreteColorScale(Gadfly.gen_dark_colors,nothing,nothing,true),Gadfly.Scale.ContinuousColorScale(Gadfly.Scale.f,Gadfly.Scale.ContinuousScaleTransform(identity,identity,Gadfly.Scale.identity_formatter),nothing,nothing))