logo

How to Use Palettes in Julia Plots 📂Julia

How to Use Palettes in Julia Plots

Explanation

A palette refers to a board where paints are squeezed out in advance. Mathematically, it can be explained as a ‘set of colors’ or a ‘sequence of colors’. When drawing multiple graphs in one picture, the most common way is to distinguish them by using different colors. For this purpose, Julia has implemented a type called ColorPalette that collects various colors. It can be comfortably understood as a vector of colors. Indeed, if we load the default palette :default, it may look incredibly complicated, but if we look inside, it’s just a vector of colors.

When drawing heatmaps, gradients are used instead of palettes.

julia> using Plots

julia> palette(:default)
ColorPalette(ColorSchemes.ColorScheme{Vector{RGB{Float64}}, String, String}(RGB{Float64}[RG
B{Float64}(0.0,0.6056031611752245,0.9786801175696073), RGB{Float64}(0.8888735002725198,0.43
564919034818994,0.2781229361419438), RGB{Float64}(0.2422242978521988,0.6432750931576305,0.3
044486515341153), RGB{Float64}(0.7644401754934356,0.4441117794687767,0.8242975359232758), R
GB{Float64}(0.6755439572114057,0.5556623322045815,0.09423433626639477), RGB{Float64}(4.8211
81644776295e-7,0.6657589812923561,0.6809969518707945), RGB{Float64}(0.930767491919665,0.367
4771896571412,0.5757699667547829), RGB{Float64}(0.7769816661712932,0.5097431319944513,0.146
4252569555497), RGB{Float64}(3.8077343661790943e-7,0.6642678029460116,0.5529508754522481), 
RGB{Float64}(0.558464964115081,0.5934846564332882,0.11748125233232104), RGB{Float64}(5.9476
23898072685e-7,0.6608785231434254,0.7981787608414297), RGB{Float64}(0.6096707676128648,0.49
918492100827777,0.9117812665042642), RGB{Float64}(0.3800016049820351,0.5510532724353506,0.9
665056985227146), RGB{Float64}(0.942181647954218,0.37516423354097583,0.4518168202944593), R
GB{Float64}(0.8684020893043971,0.3959893639954845,0.7135147524811879), RGB{Float64}(0.42314
674364630817,0.6224954944199981,0.19877060252130468)], "", ""))

julia> palette(:default).colors.colors
16-element Array{RGB{Float64},1} with eltype RGB{Float64}:
 RGB{Float64}(0.0,0.6056031611752245,0.9786801175696073)
 RGB{Float64}(0.8888735002725198,0.43564919034818994,0.2781229361419438)
 RGB{Float64}(0.2422242978521988,0.6432750931576305,0.3044486515341153)
 RGB{Float64}(0.7644401754934356,0.4441117794687767,0.8242975359232758)
 RGB{Float64}(0.6755439572114057,0.5556623322045815,0.09423433626639477)
 RGB{Float64}(4.821181644776295e-7,0.6657589812923561,0.6809969518707945)
 RGB{Float64}(0.930767491919665,0.3674771896571412,0.5757699667547829)
 RGB{Float64}(0.7769816661712932,0.5097431319944513,0.1464252569555497)
 RGB{Float64}(3.8077343661790943e-7,0.6642678029460116,0.5529508754522481)
 RGB{Float64}(0.558464964115081,0.5934846564332882,0.11748125233232104)
 RGB{Float64}(5.947623898072685e-7,0.6608785231434254,0.7981787608414297)
 RGB{Float64}(0.6096707676128648,0.49918492100827777,0.9117812665042642)
 RGB{Float64}(0.3800016049820351,0.5510532724353506,0.9665056985227146)
 RGB{Float64}(0.942181647954218,0.37516423354097583,0.4518168202944593)
 RGB{Float64}(0.8684020893043971,0.3959893639954845,0.7135147524811879)
 RGB{Float64}(0.42314674364630817,0.6224954944199981,0.19877060252130468)

You can load or create a palette by entering the symbol of an already defined palette or by entering the color and length into the function palette(). When drawing a chart, you just need to assign it to the palette keyword of the plot() function.

Code

Symbols

Use it like palette(symbol). The symbol for the default palette is :default, and its colors are as follows.

When drawing multiple graphs in one picture, the above colors are applied in order. After using all the colors, it cycles back to the beginning.

using Plots

x = 0:0.01:2π
plot([x -> sin(x - a) for a in range(0, π, length = 5)], 0, 2π)

Palettes and gradients predefined in Plots.jl can be checked in the official documentation. (More diverse palettes and gradients can be found in the official documentation of the package ColorSchemes.jl.)

If drawn with :rainbow,

palette(:rainbow)

plot([x -> sin(x - a) for a in range(0, π, length = 5)], 0, 2π,
    palette = palette(:rainbow))

Custom Definition

You can define your palette directly with palette([start color, end color], length). Alternatively, you can interpolate colors using range().

palette([:blue, :orange], 10)

palette([RGB(0.5, 0.6, 0.2), RGB(1.0, 0.2, 0.9)], 10)

Environment

  • OS: Windows11
  • Version: Julia 1.9.4, Plots v1.39.0

See Also