Chapter 15 Key Takeaways: Advanced Charts and Dashboards
The Core Ideas
1. seaborn extends matplotlib; it does not replace it.
Every seaborn plot is a matplotlib figure. You can still call plt.title(), plt.savefig(), and ax.set_ylabel() after any seaborn call. seaborn's job is to reduce the lines of code needed for common statistical chart types and apply attractive defaults automatically.
2. plotly produces interactive HTML, not static images. This is the defining difference. A plotly figure saved as HTML can be hovered, zoomed, filtered, and shared via email to anyone with a browser — no Python required. For stakeholder communication, this is often more valuable than a beautifully formatted static chart.
3. Set the seaborn theme before drawing any charts.
sns.set_theme(style="whitegrid", palette="muted") at the top of your script applies consistent styling to all subsequent charts. Changing the theme mid-script is possible but creates inconsistency.
4. Color map choice communicates meaning, not just aesthetics.
- Sequential (e.g., "Blues") — for data that goes from low to high with no midpoint
- Diverging (e.g., "RdYlGn") — for deviation from a benchmark (negative = one color, positive = another)
- Categorical (e.g., "muted") — for grouping where no ordering is implied
Getting this wrong misleads your audience. A diverging palette on positive-only data implies there is something meaningful at the midpoint when there isn't.
5. annot=True in sns.heatmap writes values in cells; custom annotation strings give you control over formatting.
Pass a pre-formatted DataFrame of strings to annot= (instead of True) combined with fmt="" to show values like "$132K" or "+12%" instead of raw floats.
6. hovertemplate in plotly is worth writing manually.
The default hover template shows raw column names and unformatted numbers. A custom template with "$%{y:,.0f}" and "<extra></extra>" produces the polished tooltip that makes executives trust the dashboard. The <extra></extra> tag at the end removes plotly's default trace-name box.
7. hovermode="x unified" is the right setting for time-series comparisons.
When you have multiple lines on a chart (actual vs target, this year vs last year), unified hover mode shows all values at the hovered x position in a single tooltip. Without it, the user must hover each line separately.
8. make_subplots assembles multiple charts into one HTML file.
Use specs to declare chart types per cell — required when mixing pie charts with standard x/y plots. Pass row= and col= to each fig.add_trace() call to direct it to the correct panel.
9. write_html(include_plotlyjs=True) creates a fully offline file.
Use True when you cannot guarantee the recipient has internet access. Use "cdn" when file size matters (reduces from ~3 MB to a few KB) and the viewer will definitely be online.
10. Static image export from plotly requires the kaleido package.
fig.write_image("chart.png") will fail silently or raise an error without kaleido. Install it separately: pip install kaleido. Use scale=2 for high-resolution output suitable for print.
The Decision Framework
| Output needed | Library | Format |
|---|---|---|
| PDF or print report | matplotlib or seaborn | .savefig("chart.pdf") |
| Slide deck PNG (one chart) | seaborn or plotly | .savefig() or write_image() |
| Statistical exploration (internal) | seaborn | .savefig() |
| Stakeholder dashboard (interactive) | plotly | write_html() |
| Hierarchical data | plotly (px.treemap, px.sunburst) |
write_html() |
| Distribution comparison | seaborn (sns.boxplot) |
.savefig() |
| Correlation exploration | seaborn (sns.pairplot) |
.savefig() |
Common Mistakes to Avoid
Mistake: Using a pie chart with more than 4-5 categories. The human eye cannot compare arc lengths accurately. Switch to a bar chart (easier) or treemap (if hierarchy matters).
Mistake: Leaving default hover templates in a client-facing plotly chart.
Default templates show raw column names and unformatted numbers. Always write custom hovertemplate strings for any chart that will be seen by a non-technical audience.
Mistake: Using sns.pairplot in an executive presentation.
The pairplot is for you, the analyst. It is dense, requires interpretation, and looks like a statistics textbook. Extract the interesting relationships from the pairplot and build individual focused charts for stakeholders.
Mistake: Not setting errorbar=None on sns.barplot with aggregated data.
If your DataFrame already has one number per group (totals, not raw observations), the confidence interval bars are meaningless. Suppress them with errorbar=None.
Mistake: Using the same chart type for all data. Line charts are for trends over time. Bar charts are for comparing discrete categories. Scatter plots are for relationships between two continuous variables. Using a line chart for quarterly regional totals (no time ordering on the x-axis) creates a false impression of trend.
Vocabulary Introduced This Chapter
seaborn — A Python visualization library built on matplotlib that provides higher-level chart types, automatic statistical summaries, and attractive default themes. Imported as sns.
plotly — A visualization library that produces interactive, browser-based charts. Outputs HTML files that can be opened in any browser without Python.
plotly express (px) — plotly's high-level interface that produces charts with one function call. Analogous to seaborn relative to matplotlib.
plotly graph objects (go) — plotly's low-level interface offering fine-grained control over every chart element. Required for subplots and complex multi-trace figures.
hovertemplate — A string that controls the content and format of a plotly hover tooltip. Uses %{x}, %{y}, %{customdata[0]}, and Python format specifiers.
heatmap — A chart that encodes a matrix of values as colors. Rows and columns are categorical; cell color represents the numeric value. Ideal for two-dimensional comparison across categorical axes.
pairplot — A grid of scatter plots showing the pairwise relationship between every combination of numeric variables in a DataFrame. Used for exploratory analysis.
treemap — A plotly chart type that displays hierarchical data as nested rectangles where area represents magnitude.
sunburst — A plotly chart type that displays hierarchical data as nested rings, with the innermost ring being the highest level of the hierarchy.
make_subplots — A plotly function that creates a figure with multiple chart panels arranged in a grid. Returns a Figure object to which traces are added with row= and col= arguments.
kaleido — A separate Python package required for plotly's static image export (write_image). Not included with plotly itself.
diverging palette — A color palette with two distinct hues meeting at a neutral midpoint, used to represent values above and below a benchmark.
sequential palette — A color palette that varies in intensity along a single hue, used to represent magnitude from low to high.