# PGFPlotsX a matrix of RGB

**URL:** <https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709>\
**Category:** Visualization\
**Tags:** question, pgfplotsx\
**Created:** [May 28, 2019, 10:05pm UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709 "2019-05-28T22:05:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 28, 2019, 10:05pm UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/1 "2019-05-28T22:05:53Z")

</div>

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:

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

```

---

<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:** [May 29, 2019, 5:49am UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/2 "2019-05-29T05:49:35Z")

</div>

> [@yakir12](#):
>
> 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.

I am not sure what you mean by a combination. If you are asking for section 4.6.7 (Surface Plots with Explicit Color) in the pgfplots manual, this we don’t currently support. You can work around it with eg

```julia
using PGFPlotsX, Colors
img = rand(RGB{Float64}, 20, 20)
xy = Pair.(1:20, (1:20)')
col2tex(c) = "rgb=$(red(c)),$(green(c)),$(blue(c))"
c = Coordinates(vec(first.(xy)), vec(last.(xy));
                meta = [col2tex(c) for c in vec(img)])
@pgf Axis({ enlargelimits = false },
          PlotInc({ matrix_plot, no_marks, "mesh/color input" = "explicit",
                    "mesh/cols" = 20 },
                  c))

```

 ![test](https://global.discourse-cdn.com/julialang/original/3X/e/8/e8af38c1e92e27ad4b4dec39b16acba619c7942f.png)

---

<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:** [May 29, 2019, 5:53am UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/3 "2019-05-29T05:53:52Z")

</div>

Opened an issue for discussion of supporting this directly:

[https://github.com/KristofferC/PGFPlotsX.jl/issues/153](https://github.com/KristofferC/PGFPlotsX.jl/issues/153)

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 29, 2019, 7:17am UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/4 "2019-05-29T07:17:32Z")

</div>

> [@Tamas\_Papp](#):
>
> work around

That’s perfect, thank you!

---

<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:** [May 29, 2019, 7:32am UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/5 "2019-05-29T07:32:33Z")

</div>

I am happy that it solves your immediate problem. I also plan to add this feature soon, after some discussion.

pgfplots (the LaTeX package) is so feature-rich (with a 500+ page manual) that we have not covered all possible constructs in PGFPlotsX, but try to add them on demand.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 29, 2019, 7:47am UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/6 "2019-05-29T07:47:21Z")

</div>

I personally find PGFPlots really useful for publication-quality figures. The fact it’s so feature rich means I can do almost anything I want, which is also horrible in its own right… PGFPlotsX is therefore ultra useful, I bet adjusting a Julia-dialect API for PGFPlots and TikZ is really tricky…

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 29, 2019, 12:03pm UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/7 "2019-05-29T12:03:03Z")

</div>

@Tamas_Papp, I can’t get rid of the flipped y-axis, I tried with `y_dir=normal` (and `reverse` just in case) and it’s still flipped… I guess that _does_ fit with the standard of images where the top left corner is `(0,0)`, but… Any way I can have the y-axis (and corresponding data) go from small to large?

---

<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:** [May 29, 2019, 12:33pm UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/8 "2019-05-29T12:33:14Z")

</div>

Please refer to the pgfplots manual, in particular `matrix plot*` in 4.6.12. Eg

```julia
using PGFPlotsX, Colors
img = rand(RGB{Float64}, 20, 20)
xy = Pair.(1:20, (1:20)')
col2tex(c) = "rgb=$(red(c)),$(green(c)),$(blue(c))"
c = Coordinates(vec(first.(xy)), vec(last.(xy));
                meta = [col2tex(c) for c in vec(img)])
@pgf Axis({ enlargelimits = false },
          PlotInc({ "matrix_plot*", no_marks,
                    "mesh/color input" = "explicit",
                    "mesh/cols" = 20},
                  c))

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 29, 2019, 12:35pm UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/9 "2019-05-29T12:35:10Z")

</div>

Perfect, thanks\*!

---

<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 7, 2019, 11:58am UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/10 "2019-06-07T11:58:44Z")

</div>

This feature was added with the latest (0.3.7) release of PGFPlotsX, with [worked example](https://kristofferc.github.io/PGFPlotsX.jl/dev/examples/juliatypes/#Explicit-colors-in-surface-plots-1) in the manual.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 24, 2022, 2:11pm UTC](https://discourse.julialang.org/t/pgfplotsx-a-matrix-of-rgb/24709/11 "2022-01-24T14:11:59Z")

</div>

A post was split to a new topic: [How to make a heatmap with pgfplotsx?](https://discourse.julialang.org/t/how-to-make-a-heatmap-with-pgfplotsx/75128)
