Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Meetup

153 views

Published on

Using the python plotly library in jupyter notebooks and web apps to build beautiful charts.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Meetup

  1. 1. Python & Plotly Giovanni Perna giovanni.perna@skyscanner.net
  2. 2. What is Plotly? •Plotly is an online analytics and visualisation tool, designed for sharing analyses •Plotly is also a set of data visualisation libraries for Javascript, Python, R, Matlab, Julia. They are all open-source and MIT licensed •The core library is written in Javascript on top of D3. Because of this charts are interactive
  3. 3. Why Plotly ? •Mainly because charts are interactive and look nice ◦ Many many different types of charts ◦ Zoom in and out along any axes ◦ Tooltips on mouse over ◦ Series can be switched on and off independently ◦ Layout and styles easily customisable •Same code can be easily used in Python scripts, jupyter notebooks and web applications
  4. 4. Offline Plotly We want to use the offline feature, which means not going through the Plotly analysis platform The trick is to use to use the module plotly.offline.iplot rather than plotly.plotly.iplot import plotly.offline as offline import plotly.graph_objs as go trace = go.Scatter( x = df[x], y = df[y], mode = 'markers' ) data = [trace] offline.iplot(data, show_link=“False”) import plotly.plotly as py import plotly.graph_objs as go trace = go.Scatter( x = df[x], y = df[y], mode = 'markers' ) data = [trace] py.iplot(data, filename='basic-scatter')
  5. 5. How can we use the Python library? We can use the Python library in different ways: •From command line • Charts are written to local HTML files •From Jupyter notebooks •In a web application
  6. 6. Jupyter notebook We need to import the offline module and initialise the notebook mode import plotly.graph_objs as go import plotly.offline as offline offline.init_notebook_mode() Some examples in this notebook
  7. 7. Plotly in web apps •Jupyter is good for prototyping, but not as good for repeatable analyses. •Taking a chart that we have built in Jupyter and moving it to a (Python) web app is trivial •We can use the PlotlyJSONEncoder •The chart is entirely defined on the server side •The client side code will only be responsible for retrieving the data and creating a new chart with that definition
  8. 8. Simple flask application app.py def prepare_graph(): df = pd.read_csv('static/san_francisco.csv') ... data = [go.Scatter( x=df.index, y=df[col], name=col, mode='lines', ) for col in df.columns] layout = dict(title='San Francisco temperature') return Figure(data=data, layout=layout) @app.route("/data") def data(): figure = prepare_graph() figureJSON = json.dumps(figure, cls=plotly.utils.PlotlyJSONEncoder) return Response(response=figureJSON, mimetype="application/json") index.html <script type="text/javascript"> $( document ).ready(function() { $.get( "data", function( data ) { Plotly.plot(document.getElementById('chart'), data, null, {showLink: false}); }, "json" ); }); </script>
  9. 9. Some links •Python library docs: https://plot.ly/python/ •Jupyter notebook: https://github.com/gioper86/offlineplotly/blob/master/notebooks/sanfrancisco_temp.ipynb •Github repo: https://github.com/gioper86/offlineplotly
  10. 10. THANK YOU!

×