# How to create matrix of rgb values from a plot object

**URL:** <https://discourse.julialang.org/t/how-to-create-matrix-of-rgb-values-from-a-plot-object/76500>\
**Category:** Visualization\
**Tags:** images, plots\
**Created:** [February 15, 2022, 5:41pm UTC](https://discourse.julialang.org/t/how-to-create-matrix-of-rgb-values-from-a-plot-object/76500 "2022-02-15T17:41:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rik](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@Rik](https://discourse.julialang.org/u/Rik)\
**Post date:** [February 15, 2022, 5:41pm UTC](https://discourse.julialang.org/t/how-to-create-matrix-of-rgb-values-from-a-plot-object/76500/1 "2022-02-15T17:41:33Z")

</div>

I’ve been trying to go from a plot to a matrix of rgb values for a while now and the best solution I could come up with makes use of saving the plot as png and then loading it as an image. This is of course a very dirty workaround so I’m wondering if there is a more neat, direct way to do this?  
Here is a script that does what I want (in a very ugly way)

```julia
using Plots, Images

function main()
    x = 1:100
    y = sin.(x)
    plot(x, y)
    fn = "myPlot.png"

    # I'm looking for a function which has the same result as the two steps below that doesn't require saving to file.
    savefig(fn)
    myImage = load(fn)
end

main()

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 15, 2022, 8:02pm UTC](https://discourse.julialang.org/t/how-to-create-matrix-of-rgb-values-from-a-plot-object/76500/2 "2022-02-15T20:02:56Z")

</div>

Fyi, Tim Holy seems to recommend exactly that in [another post.](https://discourse.julialang.org/t/save-the-rgb-data-as-array-in-plots-jl/49016/2)

---

<div class="post-metadata">

**Author:** ![Rik](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@Rik](https://discourse.julialang.org/u/Rik)\
**Post date:** [February 16, 2022, 8:33am UTC](https://discourse.julialang.org/t/how-to-create-matrix-of-rgb-values-from-a-plot-object/76500/3 "2022-02-16T08:33:45Z")

</div>

I would expect that `savefig()` (and `png()` since that is essentially the same function when saveing to png) would need to do exactly what I would like to do in order to save a plot as a png file. So it seems to me that this workaround is not necessary.  
I briefly looked at the inner workings of `png()` but was not able to find out exactly how it works. Maybe someone with more experience on the implementation of `savefig()` or `png()` would know how this image is created and whether that method can also be used in other code without having to immediately save the image.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 16, 2022, 10:57am UTC](https://discourse.julialang.org/t/how-to-create-matrix-of-rgb-values-from-a-plot-object/76500/4 "2022-02-16T10:57:57Z")

</div>

FWIW, this seems to do the same thing but avoids saving a file to disk:

```julia
using FileIO, Plots; gr()
p = plot(sin)
io_buf = IOBuffer()
show(io_buf, MIME("image/png"), p)
# io_buf.data

myImage = load(io_buf) # Array{RGB{N0f8},2} with eltype RGB{N0f8}
plot(myImage, axis=nothing)

```

---

<div class="post-metadata">

**Author:** ![Rik](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@Rik](https://discourse.julialang.org/u/Rik)\
**Post date:** [February 16, 2022, 2:34pm UTC](https://discourse.julialang.org/t/how-to-create-matrix-of-rgb-values-from-a-plot-object/76500/5 "2022-02-16T14:34:20Z")

</div>

Awesome! Thank you so much!

---

<div class="post-metadata">

**Author:** ![Rik](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@Rik](https://discourse.julialang.org/u/Rik)\
**Post date:** [February 16, 2022, 2:39pm UTC](https://discourse.julialang.org/t/how-to-create-matrix-of-rgb-values-from-a-plot-object/76500/6 "2022-02-16T14:39:49Z")

</div>

At the same time I also found a different solution based on [this](https://stackoverflow.com/questions/7821518/matplotlib-save-plot-to-numpy-array) stackoverflow question which in my opinion is less clean than the one proposed by @rafael.guerra. I will post the solution here anyway because it might be relevant for some people.

```julia
using Images
import PyPlot; const plt = PyPlot

function figToImage(fig)
    fig.canvas.draw()
    figureSize = round.(Integer, fig.get_size_inches() .* fig.dpi)
    invalidRGBString = fig.canvas.tostring_rgb()

    io = IOBuffer()
    write(io, invalidRGBString)
    UIntRGBVector = take!(io)

    pixelvalues = permutedims(reinterpret.(N0f8, reshape(UIntRGBVector, 3, figureSize...)), [1, 3, 2])

    return dropdims(mapslices(x -> RGB(x...), pixelvalues, dims=[1]), dims=1)
end

function main()
    x = 1:10
    y = sin.(x)
    fig = plt.figure()
    p = plt.plot(x, y)

    display(figToImage(fig))
    return
end

main()

```
