# How to plot a matrix of colors

**URL:** https://discourse.julialang.org/t/how-to-plot-a-matrix-of-colors/41920
**Category:** Visualization
**Created:** [June 23, 2020, 8:50am UTC](https://discourse.julialang.org/t/how-to-plot-a-matrix-of-colors/41920 "2020-06-23T08:50:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 23, 2020, 8:50am UTC](https://discourse.julialang.org/t/how-to-plot-a-matrix-of-colors/41920/1 "2020-06-23T08:50:49Z")

</div>

If I have a matrix with colors:

```julia
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

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

```

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

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 23, 2020, 9:09am UTC](https://discourse.julialang.org/t/how-to-plot-a-matrix-of-colors/41920/2 "2020-06-23T09:09:31Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 23, 2020, 9:36am UTC](https://discourse.julialang.org/t/how-to-plot-a-matrix-of-colors/41920/3 "2020-06-23T09:36:31Z")

</div>

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

```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

> [@PGFPlotsX a matrix of RGB](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709):
>
> I have an image – a matrix of RGBs. The number of pixels is relatively small (e.g. 20×20), so I want to plot it as an array of colored squares. This way the dimensions of the resulting figure won’t be dictated by the low resolution of the image. I think I need a combination of [this](https://kristofferc.github.io/PGFPlotsX.jl/stable/examples/coordinates/#Matrix-plot-1) and [this](https://tex.stackexchange.com/a/57201/12370), but I’m not sure. For the sake of a MWE: img = rand(RGB{Float64}, 20, 20)

here, the line

```julia
rand(RGB{Float64}, 20, 20)

```

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

---

<div class="post-metadata">

### Author: ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)
#### Post date: [June 23, 2020, 10:21am UTC](https://discourse.julialang.org/t/how-to-plot-a-matrix-of-colors/41920/4 "2020-06-23T10:21:13Z")

</div>

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](https://juliagraphics.github.io/Colors.jl/stable/)) 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
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)

```

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [June 23, 2020, 10:29am UTC](https://discourse.julialang.org/t/how-to-plot-a-matrix-of-colors/41920/5 "2020-06-23T10:29:29Z")

</div>

To transform `:red` to `RGB` you can use [Color parsing](https://juliagraphics.github.io/Colors.jl/stable/constructionandconversion/#Color-Parsing-1) functions of Colors.jl

for example

```julia
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)

```

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 23, 2020, 10:40am UTC](https://discourse.julialang.org/t/how-to-plot-a-matrix-of-colors/41920/6 "2020-06-23T10:40:59Z")

</div>

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

For future reference, either:

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

```

or

```julia
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.

---

<div class="post-metadata">

### Author: ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)
#### Post date: [June 23, 2020, 4:22pm UTC](https://discourse.julialang.org/t/how-to-plot-a-matrix-of-colors/41920/7 "2020-06-23T16:22:50Z")

</div>

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](https://mbaz.github.io/Gaston.jl/stable/2d-gallery/#Displaying-a-flag-with-bars-1)
