liveBook · Manning

Plotly

As explained in Build Python Web Apps with Streamlit

Plotly is a visualization library for creating interactive charts and graphs.

A dashboard is easier to scan when its metrics become charts rather than tables of numbers. Plotly is a Python visualization library for making interactive charts; Plotly Graph Objects (`go`) lets you build and customize each chart incrementally. Prepare the data with pandas, create a Plotly figure, then display it in Streamlit. section 6.5 section 6.5.1

Choose the chart to match the question:

  • For change over time, group a metric by day. Add a `go.Scatter` trace to a `go.Figure`, using days for x-values and metric values for y-values; `mode='lines+markers'` shows both the trend and its data points. section 6.5.1
  • For a breakdown, group the metric by a chosen dimension and add a `go.Pie` trace. Its `labels` identify the groups, `values` set their sizes, and `hole=0.4` makes a donut chart. section 6.5.2
  • Pass the finished figure to `st.plotly_chart(chart, use_container_width=True)`. Streamlit tabs can show different metrics, while a select box lets users choose the pie chart’s grouping dimension. section 6.5.1 section 6.5.2

Sales over time and by product

Daily sales become a line chart after sales are aggregated by day. Grouping the same sales by product instead produces values for a pie chart. The change is in the pandas grouping and the Plotly trace, not in how Streamlit displays the figure. section 6.5.1 section 6.5.2

Listing 6.10 A time series chart
import plotly.graph_objs as go
from data_wrangling import get_metric_time_series

def get_time_series_chart(df, metric):
  data = get_metric_time_series(df, metric)
  fig = go.Figure()
  fig.add_trace(
    go.Scatter(x=data['Day'], y=data['Value'], mode='lines+markers')
  )

  fig.update_layout(
    title=f"{metric.title}",
    xaxis_title='Day',
    yaxis_title=metric.title
  )
  return fig

A complete line-chart function shows the prepared data, `go.Figure`, `go.Scatter`, and axis labels together. section 6.5.1

Listing 6.11 Displaying a pie chart
import plotly.graph_objects as go
from data_wrangling import get_metric_grouped_by_dimension

def get_pie_chart(df, metric, dimension):
  data = get_metric_grouped_by_dimension(df, metric, dimension)
  fig = go.Figure()
  fig.add_trace(
    go.Pie(labels=data[dimension], values=data['Value'], hole=0.4)
  )
  return fig

Compare the pie-chart function with the line chart: it keeps the figure-building pattern but substitutes dimension labels and `go.Pie`. section 6.5.2

How Build Python Web Apps with Streamlit covers it

How deep it goes
Works through data preparation, Graph Objects traces, layout, and Streamlit display; does not explore Plotly internals.
Examples
Builds a daily-metric line chart and a dimension-grouped donut chart for a dashboard.
What it assumes
Assumes Python functions and dataframes while introducing Plotly installation and Graph Objects chart construction.
What you can do after
Build it from scratch: create Plotly line and pie figures from grouped metrics and display them in Streamlit.
How it connects
Connects pandas grouping and time series to Streamlit charts, tabs, and selection widgets.

Also explained in

Compare how 2 books explain Plotly →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.