# Correct way to save Makie plot to IOBuffer

**URL:** https://discourse.julialang.org/t/correct-way-to-save-makie-plot-to-iobuffer/87793
**Category:** Visualization
**Tags:** makie
**Created:** [September 25, 2022, 11:35pm UTC](https://discourse.julialang.org/t/correct-way-to-save-makie-plot-to-iobuffer/87793 "2022-09-25T23:35:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [September 25, 2022, 11:35pm UTC](https://discourse.julialang.org/t/correct-way-to-save-makie-plot-to-iobuffer/87793/1 "2022-09-25T23:35:32Z")

</div>

I am generating a web page including a data URL for a Makie plot. I would like to avoid saving the plot to a file if possible. I figured this out but I would like to know if there is a better way.

```julia
using Makie, CairoMakie, Base64

# test data 
t=0:0.1:2π
y=sin.(t)

# make a plot
CairoMakie.activate!(type="png")
fig = Figure()
ax = Axis(fig[1,1], title="test", ylabel="value", xlabel="time")
xlims!(ax, 0, t[end])
ylims!(ax, -1.1, 1.1)
lns = lines!(ax, t, y)
fig[1,2] = Legend(fig, [lns], ["lines"])

# save to IOBuffer
io = IOBuffer()
Makie.backend_show(Makie.current_backend[], io, MIME"image/png"(), fig.scene)
# extract bytes and create data uri
img_data_uri = "data:image/png;base64,$(base64encode(take!(io)))";

```

Specifically, `backend_show ` looks like something I shouldn’t be messing with.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [September 26, 2022, 12:03am UTC](https://discourse.julialang.org/t/correct-way-to-save-makie-plot-to-iobuffer/87793/2 "2022-09-26T00:03:52Z")

</div>

`show(io, MIME"image/png"(), fig)` should work I think

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [September 26, 2022, 1:02am UTC](https://discourse.julialang.org/t/correct-way-to-save-makie-plot-to-iobuffer/87793/3 "2022-09-26T01:02:18Z")

</div>

Huh, that works great in the REPL but fails when called in the full code with

```julia
MethodError: no method matching show(::IOBuffer, ::Type{MIME{Symbol("image/png")}}, ::Makie.Figure)

```

Adding a call to `methods` immediately before the call to `show` only makes me more confused, the method is right there!

```julia
methods(show, [IO, MIME"image/png", Any]) = # 2 methods for generic function "show":
[1] show(io::IO, m::MIME, figlike::Union{Makie.Figure, Makie.FigureAxisPlot, Makie.Scene}) in Makie at /home/user/.julia/packages/Makie/Ppzqh/src/display.jl:114
[2] show(io::IO, ::MIME{Symbol("image/png")}, surface::Cairo.CairoSurface) in Cairo at /home/user/.julia/packages/Cairo/smWIA/src/Cairo.jl:441

```

I’ll continue to investigate.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [September 26, 2022, 1:12am UTC](https://discourse.julialang.org/t/correct-way-to-save-makie-plot-to-iobuffer/87793/4 "2022-09-26T01:12:25Z")

</div>

Take care to do `MIME"..."()` with the parentheses, the dispatch is not defined for the Type so you need to instantiate the MIME object

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [September 26, 2022, 1:14am UTC](https://discourse.julialang.org/t/correct-way-to-save-makie-plot-to-iobuffer/87793/5 "2022-09-26T01:14:08Z")

</div>

That was it, thank you very much!

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [February 5, 2024, 4:40am UTC](https://discourse.julialang.org/t/correct-way-to-save-makie-plot-to-iobuffer/87793/6 "2024-02-05T04:40:00Z")

</div>

What about for svg?

```julia
    buf = IOBuffer()
    show(buf, MIME"image/svg"(), fig)
ERROR: Unsupported mime: image/svg
Stacktrace:
      ⋮ internal @ Base, CairoMakie, Makie
  [6] show(io::IOBuffer, m::MIME{Symbol("image/svg")}, figlike::Figure)
    @ Makie ~/.julia/packages/Makie/z2T2o/src/display.jl:258

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [February 5, 2024, 6:41am UTC](https://discourse.julialang.org/t/correct-way-to-save-makie-plot-to-iobuffer/87793/7 "2024-02-05T06:41:06Z")

</div>

The mime type of svg is “image/svg+xml”
