logo

How to Specify Graph Colors for Each Subplot in Julia Plots 📂Julia

How to Specify Graph Colors for Each Subplot in Julia Plots

Overview

This section introduces three methods for specifying graph colors for each subplot. To learn how to specify colors for graph elements, refer here.

Method 1

The first way to specify the graph color for a subplot is to predefine the color when defining each subplot. In Julia, since a picture is an object itself, you can define multiple pictures with different attributes and then combine them into one plot.

p₁ = plot(rand(10),    lc = :red)
p₂ = scatter(rand(10), mc = :blue)
p₃ = bar(rand(10),     fc = :green)

plot(p₁, p₂, p₃,
     layout = (3, 1),
     title = ["p₁" "p₂" "p₃"],
)

Method 2

The second method involves entering the colors as a row vector when defining the subplots in the overall plot through keyword arguments. Note that it must be a row vector, not a column vector.

p₄ = plot(rand(10))
p₅ = plot(rand(10))
p₆ = plot(rand(10))

plot(p₄, p₅, p₆,
     layout = (3, 1),
     linecolor = [:brown :purple :orange],
     title = ["p₄" "p₅" "p₆"],
)

Method 3

The third method changes the property value of each subplot after defining the overall plot. The property .series_list is a vector of dictionaries containing series attributes information of each subplot. That is, p.series_list[1] returns the series attributes dictionary of the first subplot. By entering the :linecolor key and changing its value in this dictionary, the line color of the first subplot changes.

p₇ = plot(rand(10))
p₈ = scatter(rand(10))
p₉ = bar(rand(10))

p = plot(p₇, p₈, p₉,
        layout = (3, 1),
        title = ["p₇" "p₈" "p₉"],
)

p.series_list[1][:linecolor]   = :goldenrod1
p.series_list[2][:markercolor] = :olivedrab3
p.series_list[3][:fillcolor]   = :hotpink3

Environment

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

See Also