logo

How to Use Colors in Julia Plots 📂Julia

How to Use Colors in Julia Plots

Overview

The package that facilitates the convenient use of colors in Julia is Colors.jl. It can be used together just by importing the visualization package Plots.jl.

Symbols and Strings

The way to check the list of named colors is by entering Colors.color_names in the console window or checking the official documentation.

julia> using Plots

julia> Colors.color_names
Dict{String, Tuple{Int64, Int64, Int64}} with 666 entries:
  "darkorchid"      => (153, 50, 204)
  "chocolate"       => (210, 105, 30)
  "chocolate2"      => (238, 118, 33)
  "grey69"          => (176, 176, 176)
  "grey97"          => (247, 247, 247)
  "olivedrab3"      => (154, 205, 50)
  "deeppink2"       => (238, 18, 137)
  "mediumpurple2"   => (159, 121, 238)
  "ivory1"          => (255, 255, 240)
  ⋮                 => ⋮

Keywords that can designate colors basically allow the use of symbols and strings. Whether you enter the color name as a symbol or a string, the respective color is applied. It doesn’t matter what is entered as it is passed to Colors.parse(Colorant, color name), so the result is the same whether a symbol or string.

julia> Colors.parse(Colorant, :red)
RGB{N0f8}(1.0,0.0,0.0)

julia> Colors.parse(Colorant, "red")
RGB{N0f8}(1.0,0.0,0.0)

The results of designating colors in various graphs are as follows.

plot(randn(50, 6),
    seriescolor = [:red :hotpink1 :purple3 "blue" "lime" "brown4"],
    seriestype = [:line :scatter :histogram :shape :sticks :steppre],
    layout = (3,2)
)

RGB

RGB color codes can be used like colorant"rgb(255, 0, 0)". Only integers in $[0, 255]$ can be entered in rgb().

julia> colorant"rgb(255, 0, 0)"           # rgb() notation with integers in [0, 255]
RGB{N0f8}(1.0,0.0,0.0)

julia> colorant"rgba(0, 0, 255, 0.5)"     # with alpha in [0, 1]
RGBA{N0f8}(0.0,0.0,1.0,0.502)

plot(rand(20, 2),
    seriescolor = [colorant"rgb(255, 0, 0)" colorant"rgba(0, 0, 255, 0.5)"],
    layout = 2
    )

For more details on handling RGB color codes, refer to here.

HEX

6-digit HEX codes can be used like colorant"#FF0000", and 3-digit HEX codes like colorant"#00f".

julia> colorant"#FF0000"    # 6-digit hex notation
RGB{N0f8}(1.0,0.0,0.0)

julia> colorant"#00f"       # 3-digit hex notation
RGB{N0f8}(0.0,0.0,1.0)

julia> plot(rand(20, 2),
           seriescolor = [colorant"#FF0000" colorant"#00f"],
           layout = 2
       )

For more details on handling HEX color codes, refer to here.

Environment

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

See Also