PGFPlotsX a matrix of RGB

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 and this, but I’m not sure.

For the sake of a MWE:

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

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

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

1 Like

Opened an issue for discussion of supporting this directly:

https://github.com/KristofferC/PGFPlotsX.jl/issues/153

1 Like

That’s perfect, thank you!

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.

1 Like

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…

@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?

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

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))
2 Likes

Perfect, thanks*!

This feature was added with the latest (0.3.7) release of PGFPlotsX, with worked example in the manual.

1 Like

A post was split to a new topic: How to make a heatmap with pgfplotsx?