How to plot a matrix of colors

If I have a matrix with colors:

color_matrix = [:red :red; :red :blue]

how do I do to plot this in a 2d plot, showing the corresponding colors?

E.g., if I had

color_matrix = [:blue :white :red; :blue :white :red]

I would want to get an output looking like a French flag.

If you are using PGFPlotsX, you can modify this example:

https://kristofferc.github.io/PGFPlotsX.jl/dev/examples/coordinates.html#Matrix-plot-1

Running the last part of that example gives me an error:

The latex command `lualatex jl_FXPARL.tex` failed
error(::String) at error.jl:33
savepdf(::String, ::TikzDocument; latex_engine::PGFPlotsX.LaTeXEngine, buildflags::Array{String,1}, run_count::Int64, tmp::String) at tikzdocument.jl:197
savepdf at tikzdocument.jl:158 [inlined]
savesvg(::String, ::TikzDocument; latex_engine::PGFPlotsX.LaTeXEngine, buildflags::Array{String,1}, keep_pdf::Bool) at tikzdocument.jl:267
savesvg at tikzdocument.jl:266 [inlined]
save(::String, ::TikzDocument; include_preamble::Bool, latex_engine::PGFPlotsX.LaTeXEngine, buildflags::Array{String,1}, dpi::Int64, showing_ide::Bool) at tikzdocument.jl:84
(::PGFPlotsX.var"#save##kw")(::NamedTuple{(:showing_ide,),Tuple{Bool}}, ::typeof(PGFPlotsX.save), ::String, ::TikzDocument) at tikzdocument.jl:73
#save#60 at tikzpicture.jl:34 [inlined]
save at tikzpicture.jl:34 [inlined]
#save#55 at axislike.jl:45 [inlined]
(::PGFPlotsX.var"#save##kw")(::NamedTuple{(:showing_ide,),Tuple{Bool}}, ::typeof(PGFPlotsX.save), ::String, ::Axis) at axislike.jl:45
show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::MIME{Symbol("image/svg+xml")}, ::Axis) at tikzdocument.jl:289
show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::String, ::Axis) at multimedia.jl:109
displayinplotpane(::Axis) at showdisplay.jl:67
displayandrender(::Axis) at showdisplay.jl:131
(::Atom.var"#208#213"{String})() at eval.jl:136
#invokelatest#1 at essentials.jl:712 [inlined]
invokelatest at essentials.jl:711 [inlined]
macro expansion at dynamic.jl:24 [inlined]
eval(::String, ::Int64, ::String, ::String, ::Bool) at eval.jl:113
invokelatest(::Any, ::Any, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at essentials.jl:712
invokelatest(::Any, ::Any, ::Vararg{Any,N} where N) at essentials.jl:711
macro expansion at eval.jl:41 [inlined]
(::Atom.var"#188#189")() at task.jl:358

Searching on “matrix plot” I found another example at

here, the line

rand(RGB{Float64}, 20, 20)

seems to plot a random matrix, but I am not sure how to convert from :red to RGB{Float64}.

Normally RGB has 3 values: red, green and blue. Those range from 0 to 255. If you want to have an RGB value for a completely red pixel, this would result in (255, 0, 0). These values need to be normalised between 0 and 1 (= 256 discrete values) to work with Colors.jl. This can easily be done by dividing the RGB integer value between 0-255 by 255, resulting in a floating point number between 0 and 1.

For the above example to work, you need to add Colors (Colors.jl docs) and use it. Then go ahead and run the example img = rand(RGB{Float64}, 20, 20), this will result in a randomly colored matrix. You can then plot this with your desired tools (e.g. PGFPlotsX as in the post you linked). I’ve just tested with Plots.jl ( plot(img) )and it worked perfectly fine.

Edit: And just to make this fully clear, here’s your initial example:

julia> using Colors

julia> using Plots

julia> color_matrix = [RGB(255/255,0,0) RGB(255/255,0,0); RGB(255/255,0,0) RGB(0,0,255/255)]
2Ă—2 Array{RGB{Float64},2} with eltype RGB{Float64}:
 RGB{Float64}(1.0,0.0,0.0)  RGB{Float64}(1.0,0.0,0.0)
 RGB{Float64}(1.0,0.0,0.0)  RGB{Float64}(0.0,0.0,1.0)

julia> plot(color_matrix)

To transform :red to RGB you can use Color parsing functions of Colors.jl

for example

julia> color_matrix = [:blue :white :red; :blue :white :red]
julia> parse.(Colorant, string.(color_matrix))
# 2Ă—3 Array{RGB{N0f8},2} with eltype RGB{FixedPointNumbers.Normed{UInt8,8}}:
#  RGB{N0f8}(0.0,0.0,1.0)  RGB{N0f8}(1.0,1.0,1.0)  RGB{N0f8}(1.0,0.0,0.0)
#  RGB{N0f8}(0.0,0.0,1.0)  RGB{N0f8}(1.0,1.0,1.0)  RGB{N0f8}(1.0,0.0,0.0)
1 Like

Thank you all for the help, it works well now.

For future reference, either:

color_matrix = [:blue :white :red; :blue :white :red]
parse.(Colorant, string.(color_matrix))

or

color_matrix = [RGB{Float64}(0,0,1) RGB{Float64}(1,1,1) RGB{Float64}(1,0,0); RGB{Float64}(0,0,1) RGB{Float64}(1,1,1) RGB{Float64}(1,0,0)]

will both give a french flag.

Just to point out an alternative, here’s an example with Gaston.jl:

https://mbaz.github.io/Gaston.jl/stable/2d-gallery/#Displaying-a-flag-with-bars-1