ankane/ruby-polars

Noob question about plotting

iovis opened this issue · 3 comments

iovis commented

Hi!

I sometimes use pandas + matplotlib in Python to do quick and dirty data visualizations and I wanted to give polars a try!

With matplotlib I can do plt.plot(...); plt.show() and it shows a little window with the plot. I was trying to do the same with polars and vega and got this far:

require "polars-df"
require "vega"

df = Polars.read_csv("data.csv")

File.write("index.html", df.plot("a", "b"))  # I saw `.plot` spit out an HTML fragment
system("open index.html")

I'm guessing what I'm missing is embedding the HTML fragment in a webpage that bundles the vega libraries? I saw there were some CDN links in the Vega repo, so I'm guessing I could leverage those?

Would it make sense to have an API that would spit out a full doc, maybe including the CDN includes, for people like me that would like to do some quick data visualization? I'm a complete noob at this, so I can believe I'm weird and there's better ways to do this haha.

I am using bridgetown for this purpose

just install the vega-packages via yarn,
include the require-statements in config/initializers and you are done.

Configuration is done in the frontmatter part and visualization happens by including the erb in the website

~~ruby
{ 
  layout:  :home,
  title:  'Übersicht',
  strategie: Arcade::Strategie.find(symbol: 'Still')
}
~~~



## header
<figure>
<%= data.strategie.plot_result  %>
<figcaption>
  some caption  
</figcaption>
</figure>

in plot_result just put the vega-code

module Arcade
class Strategie
def plot_result what: :all
 hole_value = Polars::DataFrame.new( ...)
 hole_result = hole_value.join( hole_theta, on: 'datum', how: 'outer' )
  ::Vega.lite
        .title( "" )
        .data( hole_result )
        .layer( [
 (...)

It simply works

ankane commented

@iovis It's an interesting idea and should work if you include the Vega libraries, but I'd try using Jupyter Notebook + IRuby for this.

@topofocus Thanks for sharing your approach as well.

iovis commented

I managed to make it work with Jupyter! A bit more involved that I usually do (it's usually a quick and dirty script just to do a quick visualization, so having to do build steps or a jupyter notebook is a bit more work), but it works! I'll probably use Jupyter for these from now on.

If you're open to suggestions, having some optional API to just pop up a plot would definitely be welcomed! For instance, I love how singed lets me just wrap some part of my code with flamegraph { ... } and it pops up a browser with the flamegraph without having to worry about tooling.

Thank you for all your help!