Way to get default color order in Plots.jl?

If you’re using pgfplots() through Plots.jl the colors are not the obvious ones you think they would be (i.e. :blue, :red, :green)?

Is there a way to get the default color order within the Plots.jl framework?


By picture, notice the two are different. I want to be able to pipe in the blue and red colors from the left one.

// edit: does it come from Colors.jl?


also can you do a matlab style hold on to keep the color from rotating so you can make multiple plot! calls?

1 Like

Closing this. For future readers, the answer is:

get_color_palette(:auto, plot_color(:white), 17)

As in,

cur_colors = get_color_palette(:auto, plot_color(:white), 17)
pp = linspace(0,1)

plot()
for (cur_index, cur_color) in enumerate(cur_colors[1:7])
    plot!(pp, 0+pp+cur_index-7)
end
for (cur_index, cur_color) in enumerate(cur_colors[1:7])
    plot!(pp, 1-pp+cur_index-7, color=cur_color)
end
plot!()

2 Likes

You can simply pass an integer i as the argument to color to select the ith default color. So this is equivalent to your code:

pp = [0,1]
plot()
for i = 1:7
    plot!(pp, 0+pp+i-7)
end
for i = 1:7
    plot!(pp, 1-pp+i-7, color=i)
end
plot!()

I think this is still undocumented.

8 Likes

Different PlotThemes have different color palettes. You get the the color palette of a theme with palette(thm::Symbol), so for the default theme this would be palette(:default).

5 Likes

This answer appears to be outdated. get_color_palette(:auto, plot_color(:white), 17) gives me

MethodError: no method matching get_color_palette(::Symbol, ::RGBA{Float64}, ::Int64)
Closest candidates are:
  get_color_palette(::Any, ::Any) at C:\Users\Max\.julia\packages\PlotUtils\es5pb\src\colorschemes.jl:325
  get_color_palette(::ColorGradient, ::Any) at C:\Users\Max\.julia\packages\PlotUtils\es5pb\src\colorschemes.jl:326

Stacktrace:
 [1] top-level scope at In[49]:9
 [2] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
1 Like

Looks like the solution is now

theme_palette(:auto)

as in

cur_colors = theme_palette(:auto)
pp = range(0,1,length=50)

plot()
for (cur_index, cur_color) in enumerate(cur_colors[1:3])
    plot!(pp, 0 .+ pp .+ cur_index .- 7)
end
for (cur_index, cur_color) in enumerate(cur_colors[1:3])
    plot!(pp, 1 .- pp .+ cur_index .-7, color=cur_color)
end
plot!()
1 Like

theme_palette(:auto) is working nicely.
But how can I get the actual names of the colors?
E.g. because I like this palette but want to assign selected colors to my plots.

It seems odd that the default colors in theme_palette(:default) (same as :auto) are not defined in the Colors.jl named colors list?

Indeed they are not in the list.
Extracting them is simple though (I am sure you already knew; posting this for reference).

mycolor = theme_palette(:auto).colors.colors[3]
plot([1,2],[1,2],color=mycolor)

It seems that one can write it more compactly:

mycolor3 = theme_palette(:auto)[3]
julia> mycolor == mycolor3
true