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?
3 Likes
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!()
3 Likes
yha
July 25, 2018, 8:01pm
3
You can simply pass an integer i
as the argument to color
to select the i
th 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.
11 Likes
daschw
July 25, 2018, 9:56pm
4
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)
.
6 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!()
2 Likes
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
Shuhua
January 10, 2022, 12:11pm
11
If one only wants to use the i -th color in the default palette (with no need of its name), then the simplest way is still color=i
.
plot(;size=(400, 230))
for i = 1:5
plot!(rand(10); color=i % 2 + 1, label="y$i")
end
savefig("default_color.png")
current()
3 Likes