Introduction to Plotly and Dash

Technocrat
CoderHack.com
Published in
2 min readSep 15, 2023

--

Plotly is an open source graphing library that makes interactive, publication-quality graphs. Dash is an open source framework for building analytical web applications like dashboards, data exploration apps, model interfaces, and intelligence dashboards.

Dash apps have two components:

  1. A Dash layout made up of Dash components like graphs, tables, dropdowns, sliders, etc.
  2. A Flask server that returns data from your Python code to the layout.

Getting started with Plotly

To install Plotly, run:

pip install plotly

To import Plotly, use:

import plotly.graph_objects as go

Here’s a simple example Plotly figure:

fig = go.Figure(data=[go.Bar(y=[2, 3, 1])])

screenshot from Authors notebook

Plotly also has a Plotly Express API for quick dashboards and charts. Import it with:

import plotly.express as px

Working with Charts in Plotly

Here are some examples of charts you can make in Plotly:

Line charts:

fig = px.line(df, x="...", y="...")

Bar charts:

fig = px.bar(df, x="...", y="...")

Scatter plots:

fig = px.scatter(df, x="...", y="...")

Bubble charts:

df = pd.DataFrame({"x":[1,2,3],"y":[34,23,42],  "color":['blue','red','sand']})
fig = px.scatter(df, x="x", y="y", size="size", color="color")

Box plots:

fig = px.box(df, x="...", y="...")

Histogram:

fig = px.histogram(df, x="...")

Heatmaps:

fig = px.density_heatmap(df, x="...", y="...")

Contour plots:

fig = px.density_contour(df, x="...", y="...")

3D scatter plots:

fig = px.scatter_3d(df, x="...", y="...", z="...")

Building Dash Apps

To build a Dash app, you define a layout using Dash components like:

  • dcc.Dropdown
  • dcc.Slider
  • dcc.Graph
  • etc.

You then define callback functions that update components when there are changes to the app’s input components. Some examples of Dash callbacks are:

  • Update a graph based on a dropdown selection
  • Update a slider based on a graph selection
  • Update summary statistics based on checkbox state
  • Update a progress bar based on process completion
  • etc.

Deploying Dash Apps

You can deploy Dash apps in a few ways:

  • Locally by running app.py
  • On a server using Gunicorn + Flask or Heroku
  • On Dash Enterprise, Plotly’s commercial platform for Dash apps with a free trial

In summary, Plotly and Dash are powerful tools for building and deploying advanced data visualization and analytical web applications in Python. Let me know if you have any other questions!

I hope to expand this further, there are 3D plots which are quite intruiging. Please let me know if you would like me to elaborate on any part of this outline further. I can also provide sample code and details for all the examples mentioned.

--

--