Showing plots within DataFrames.jl + Plots.jl

Hi everyone!

I was playing around with Pluto.jl and found that you can display individual plots stored in a column within a DataFrame. This is pretty cool!
For example:

using Plots
using DataFrames

arrplots = [
	plot(1:10, rand(10), legend=false, size=(180,95)) for i in 1:3
];

dfplots = DataFrame(id=1:3, plots=arrplots)

However, when using the Jupyter notebook each plots just gets displayed with its type (e.g. Plot{Plots.GRBackend() n=1}).

Are there examples for how to implement a custom show() method within the Jupyter notebook to show the plots rather than the type? it would be cool to get the same view as in Pluto.jl.

Thanks!

-Daniel

1 Like

I don’t think this would be that easy. You can take a look at PrettyTables.jl (the backend for DataFrames printing) to see what exact method is called when printing.

But best would be to just write your own function for showing the plots in a data frame of plots.

PrettyTables look awesome! i’ll take a look at it. Thanks!

This sounds to me like an unexpected surprise rather than something I’d expect to actually work. When I was doing a lot of plotting in Jupyter, I remember just calling display on plots that were created in the same cell. You could maybe just broadcast display over the vector of plots?

I did try broadcasting display over the vector of plots (e.g. display.(arrplots)) and it works. I’m not sure how to do this with a DataFrame so I can display all of the columns.

I did play around with PrettyTables.jl as pdeffebach pointed to before and found workablesolution (although not displaying inline) using pretty_table and output to a HTML file.

using DataFrames
import PrettyTables

# kind of a hack to deal with a `Plot`. The `show()` method for a MIME("text/html") has newlines in it
function PrettyTables._parse_cell_html(cell::Plots.Plot{Plots.GRBackend}; kwargs...)
    replace(sprint(show, MIME("text/html"), cell),"\n"=>"")
end

arrplots = [
    plot(1:10, rand(10), size=(170,95), legend=false) for _ in 1:3
]

df = DataFrame(:A => rand(3), :B => arrplots)

open("plot_output.html", "w") do f
   PrettyTables.pretty_table(f, df, backend = :html, renderer = :show)
end

I got the following output:

I’ve mostly been working in Python in the past few years… but my recent experience working in Julia has been a lot of fun because of little things like this. It probably would’ve taken me longer for me to do this in Python!

3 Likes