logo

Specifying the Color of Graph Elements in Julia Plots 📂Julia

Specifying the Color of Graph Elements in Julia Plots

Overview

In Plots.jl, the keywords for specifying the color of each graph component are as follows.

KeywordFunction
markercolor, mcSpecify the marker’s inside color
markerstrokecolor, mscSpecify the marker’s border color
linecolor, lcSpecify the line color
fillcolor, fcSpecify the fill color
seriescolor, cSpecify the color of all components
KeywordFunction
markeralpha, ma, Specify the marker’s inside transparency
markerstrokealpha, msa, msαSpecify the marker’s border transparency
linealpha, la, Specify the line transparency
fillalpha, fa, Specify the fill transparency
seriesalpha, a, αSpecify the transparency of all components

Colors

In Plots.jl, the targets whose color can be changed are dots, lines, and areas. The keyword arguments for specifying each color are markercolor(=mc), linecolor(=lc), and fillcolor(=fc). The property specified by these keywords does not affect each other; hence, even if you input mc = :red after plotting a line graph, the line color will not apply as red. Indeed, checking the property of p = plot(rand(10), mc = :red) shows the following.

julia> p = plot(rand(10), mc = :red)

julia> p.series_list[1][:linecolor]
RGBA{Float64}(0.0,0.6056031611752245,0.9786801175696073,1.0)

julia> p.series_list[1][:markercolor]
RGBA{Float64}(1.0,0.0,0.0,1.0)

The color of the plotted line graph is still the default color, not red.

Thus, if you plot multiple subplots and specify colors with the three keywords above, each will be applied accordingly. If you color the dots (markers) in purple :purple, the line in dark green :darkgreen, and the area in sky blue :skyblue, it will look as follows.

st = [:line :scatter :barhist :steppre :scatterhist :bar]

x = rand(20)
y = repeat(x, outer = (1, length(st)))

plot(y, seriestype = st, layout = 6, 
    mc = :purple,
    lc = :darkgreen,
    fc = :skyblue
)

Transparency

The keyword for determining the transparency of a color is created by replacing color with alpha in the color specifying keyword name. It is also acceptable to use the Greek letter α directly.

Alternatively, you can input a color code that includes transparency, such as RGBA, into the color specifying keyword.

plot(rand(20, 3), layout = (3,1), seriestype = [:scatter :line :bar],
    mc = :red,
    lc = :green,
    fc = :blue
)

plot(rand(20, 3), layout = (3,1), seriestype = [:scatter :line :bar],
    markeralpha = 0.5, mc = :red,
    la = 0.5,          lc = :green,
    fα = 0.5,         fc = :blue
)

Environment

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

See Also