Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How best to create a simple, interactive, shareable plot with python?

I'm hoping someone can point me in the right direction. The python datavis landscape has now become huge and there are so many options that I'm a bit lost on what the best way to achieve this is.

I have an xarray dataset (but it could easily be a pandas dataframe or a list of numpy arrays).

I have 3 columns, A, B, and C. They contain 40 data points.

I want to plot a scatter plot of A vs B + scale*C where scale is determined from an interactive slider.

The more advanced version of this would have a dropdown where you can select a different set of 3 columns but I'll worry about that bit later.

The caveat on all of this is that I'd like it to be online and interactive for others to use.

There seem to be so many options:

  • Jupyter (I don't use notebooks so I'm not that familiar with them but with mybinder I assume this is easy to do)
  • Plotly
  • Bokeh Server
  • pyviz.org (this is the really interesting one but again, there'd seem to be so many options on how to accomplish this)

Any thoughts or advice would be much appreciated.

like image 781
Rob Avatar asked Oct 17 '22 06:10

Rob


1 Answers

There are indeed many options and i'm not sure what is best but i use bokeh a lot and am happy about it. The example below can get you started. To launch this open a cmd in the directory where you save the script and run "bokeh serve script.py --show --allow-websocket-origin=*".

from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.models.widgets import Slider
from bokeh.models import Row,ColumnDataSource

#create the starting data
x=[0,1,2,3,4,5,6,7,8]
y_noise=[1,2,2.5,3,3.5,6,5,7,8]
slope=1 #set the starting value of the slope
intercept=0 #set the line to go through 0, you can change this later
y= [slope*i + intercept  for i in x]#create the y data via a list comprehension

# create a plot
fig=figure() #create a figure
source=ColumnDataSource(dict(x=x, y=y)) #the data destined for the figure
fig.circle(x,y_noise)#add some datapoints to the plot
fig.line('x','y',source=source,color='red')#add a line to the figure

#create a slider and update the graph source data when it changes
def updateSlope(attrname, old, new):
    print(str(new)+" is the new slider value")
    y = [float(new)*i + intercept  for i in x]
    source.data = dict(x=x, y=y)   
slider = Slider(title="slope", value=slope, start=0.0, end=2.0,step=0.1)
slider.on_change('value', updateSlope)

layout=Row(fig,slider)#put figure and slider next to eachother
curdoc().add_root(layout)#serve it via "bokeh serve slider.py --show --allow-websocket-origin=*"

The allow-websocket-origin=* is to allow other users to reach out to the server and see the graph. The http would be http://yourPCservername:5006/ (5006 is the default bokeh port). If you don't want to serve from your PC you can subscribe to a cloud service like Heroku: example.

like image 159
Joris Avatar answered Oct 31 '22 14:10

Joris