What you will be able to do
- Move from a SQL cell to a Python cell in a Snowflake Notebook and chart the data
- Choose between Altair, Streamlit, matplotlib, seaborn and plotly, and know which ones must be installed
- Read a stacked bar chart correctly and spot the scale problems it hides
- Present a notebook so the audience sees current results without the code
1.SQL, Python and Markdown cells in one notebook
A Snowflake Notebook keeps the query, the chart and the explanation in one document, and that is why it suits a business case. It is a cell-based environment in Snowsight with three cell types: SQL, Python and Markdown. A typical flow pulls the data in a SQL cell, reshapes and plots it in a Python cell, and states the conclusion in a Markdown cell placed right under the chart.
Cells can use each other's output. A Python cell can reference the result of a SQL cell, and the reverse also works, so the chart is drawn from exactly the rows the query returned. You can change an existing cell's language from its dropdown or with a keyboard shortcut. To try a different chart without losing the one that works, Duplicate the cell. The copy appears directly below the original, so you can compare the two outputs side by side.
Notebooks in Workspaces is replacing the original experience, which is now called Legacy Notebooks. The new version is Jupyter-compatible and adds file management. The cell-by-cell workflow described here is the same in both.
Checkpoint 1 of 4· Check yourself
In a Snowflake Notebook, a SQL cell returns the churn rows for your business case. You want a Python chart drawn from exactly those rows, with the written conclusion directly under the chart. What is the right approach?
Notebook cells can use each other's output, so a Python cell can plot the exact rows the SQL cell returned. A Markdown cell then states the conclusion next to the chart. Exporting the data or repeating the query is unnecessary and could leave the chart out of step with the query.
“Reference outputs from SQL cells in Python cells and vice versa.”Source: docs.snowflake.com
2.Plotting with Altair, Streamlit, matplotlib and seaborn
Notebooks let you chart with familiar open-source Python libraries. They differ in one practical way: whether they are already loaded. Altair (version 4.0) comes preloaded as part of Streamlit. Streamlit itself is imported by default, with chart elements from Streamlit 1.39.0. You must add matplotlib, seaborn and plotly yourself: select Packages in the notebook, find the library and install it. Only then will the import succeed.
| Library | Available how | Bar chart call in the examples |
|---|---|---|
| Altair | Imported by default as part of Streamlit (version 4.0) | alt.Chart(df).mark_bar() |
| Streamlit | Imported by default (chart elements of 1.39.0) | st.bar_chart |
| matplotlib | Install from Packages | pivot_df.plot.bar(stacked=True) |
| seaborn | Install from Packages | sns.barplot |
| plotly | Install from Packages | px.bar |
All the examples use the same small long-format dataframe built from the Palmer penguins data. Each row is one species, one measurement and one value.
species = ["adelie"] * 3 + ["chinstrap"] * 3 + ["gentoo"] * 3
measurements = ["bill_length", "flipper_length", "bill_depth"] * 3
values = [37.3, 187.1, 17.7, 46.6, 191.7, 17.6, 45.5, 212.7, 14.2]
df = pd.DataFrame({"species": species,"measurement": measurements,"value": values})
dfimport altair as alt
alt.Chart(df).mark_bar().encode(
x= alt.X("measurement", axis = alt.Axis(labelAngle=0)),
y="value",
color="species"
)Streamlit draws the same chart in one line and needs no install. It is the quickest option when the notebook may later become an interactive app. Some Streamlit chart elements are not supported in Snowflake, so check an element before you build a presentation around it.
import streamlit as st
st.bar_chart(df, x='measurement', y='value', color='species')import seaborn as sns
sns.barplot(
data=df,
x="measurement", hue="species", y="value",
)Checkpoint 2 of 4· Fill the gap
Your notebook has no extra packages installed, and you want a stacked bar chart comparing the penguin measurements across species. Altair needs no install. Which method completes the cell?
import altair as alt
alt.Chart(df). ? ().encode(
x= alt.X("measurement", axis = alt.Axis(labelAngle=0)),
y="value",
color="species"
)Altair comes preloaded through Streamlit, and its bar chart starts from alt.Chart(df).mark_bar(). barplot is seaborn's call, and seaborn has to be installed from Packages first. bar_chart is Streamlit's st.bar_chart, not an Altair method. mark_line draws a line, which fits a trend over time, not a comparison between categories.
Source: docs.snowflake.comSources4
3.Interpreting what the chart actually shows
In a stacked bar, the bar's height is a sum, and the sum may not be a quantity anyone measured. The Altair example and the matplotlib example (stacked=True) both stack the three species. The flipper_length bar therefore reaches 187.1 + 191.7 + 212.7 = 591.5, which is no penguin's flipper length. Use a stacked bar when the total means something, such as revenue by region adding up to company revenue. When the question is "which species is largest?", compare the individual segments, not the top of the bar.
A shared axis can also hide the differences you care about. Flipper lengths sit between 187 and 213, while bill depths sit between 14.2 and 17.7. On one y-axis, the bill-depth bars are slivers, and gentoo's noticeably shallower bill (14.2 against about 17.6 for the other two species) disappears. If that difference is the point of your case, chart that measurement on its own scale.
Compare the segment sizes, not the bar tops. Gentoo has the largest flipper length (212.7), but chinstrap has the longest bill (46.6 against 45.5), and gentoo has the shallowest bill depth (14.2). The stacked heights and the shared axis make that hard to see.
Checkpoint 3 of 4· Check yourself
Someone presenting the Altair penguin chart points at the top of the flipper_length bar, at about 591.5, and calls it "the gentoo flipper length". What is the correct reading of that bar?
The Altair example stacks the species, so the height of each bar adds up the segments: 187.1 + 191.7 + 212.7 = 591.5. That total measures nothing real here. To compare species, compare the segment sizes. A stacked bar's top is only worth quoting when the total means something, such as revenue by region adding up to company revenue.
“The following code plots a stacked bar chart of all the measurements in a dataframe named df that contains the toy dataset”Source: docs.snowflake.com
Sources4
4.Presenting a finished notebook
Use Run all right before you present or share, so every chart reflects current data. It runs all SQL and Python cells in order from the top. Because it stops at the first error, scroll to the end afterwards. A chart near the bottom that still looks fine may be output left over from an earlier run.
Then hide the code. Under Show/hide all, the Show results only option hides the code and shows only the output. You can also collapse a single Python cell to show only its visualization, or a SQL cell to show only its results table. Add Markdown cells between the charts to say what each one shows and which caveat applies. For example: "median, not average, because order values are right-skewed", or "the 95th percentile is an estimate".
Checkpoint 4 of 4· Check yourself
Tomorrow's audience should see the charts and the Markdown conclusions, but none of the SQL or Python behind them. You have already selected Run all and confirmed the last cell ran. Which option under Show/hide all do you choose?
Show results only hides the code and keeps every output visible, so the charts and the Markdown conclusions stay on screen. Show code only does the opposite and hides the output. Hide all collapses both the code and the results, and Show all leaves the code visible.
“Show results only: Hides the code and displays only the output.”Source: docs.snowflake.com
Practise it for real
Build a short notebook that charts the penguin dataset two ways and is ready to present.
1.Create a Python cell and run the toy-dataset code that builds df.
Why: Every plotting example in this lesson uses this long-format dataframe.
You should see: A nine-row table with the columns species, measurement and value.
2.Add a Python cell with the Altair stacked bar code and run it.
Why: Altair is preloaded, so this checks charting with no install.
You should see: A stacked bar chart with three bars, one per measurement, coloured by species.
3.Select Packages, install seaborn, then run the sns.barplot cell.
Why: seaborn is not preloaded and the import fails without this step.
You should see: A seaborn bar chart of the same measurements by species.
4.Add a Markdown cell under the charts noting that stacked heights are sums and that bill depth is hard to read on the shared axis.
Why: The audience needs the interpretation and the caveat next to the chart.
You should see: A text cell that renders as formatted notes between the charts.
5.Select Run all, then choose Show/hide all and Show results only.
Why: Run all refreshes every output, and hiding the code leaves only the evidence.
You should see: Only the charts and Markdown notes are visible, with no errors at the bottom of the notebook.
Stuck? Get a nudge
If the seaborn cell fails on import, check that the Packages install finished before you re-run the cell.
Sources
(4)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Snowflake Notebooks support three types of cells: SQL, Python, and Markdown.”
↩︎ SQL, Python and Markdown cells in one notebook“Select Duplicate. A copy of the cell appears immediately below the original.”
↩︎ SQL, Python and Markdown cells in one notebook“Choose this option before presenting or sharing a notebook to ensure that the recipients see the most current information.”
↩︎ Presenting a finished notebook“Show results only: Hides the code and displays only the output.”
↩︎ Presenting a finished notebook“If an error occurs in any cell, execution will halt and subsequent cells will not run.”
↩︎ Prediction - 2.
“Reference outputs from SQL cells in Python cells and vice versa.”
↩︎ SQL, Python and Markdown cells in one notebook“Contextualize results and make notes about different results with Markdown cells and charts.”
↩︎ Presenting a finished notebook - 3.https://docs.snowflake.com/en/user-guide/ui-snowsight/notebooks-in-workspaces/notebooks-in-workspaces-overviewOfficial docs
“Notebooks in Workspaces replaces the Legacy Notebooks experience.”
↩︎ SQL, Python and Markdown cells in one notebook - 4.
“Altair is imported by default on Snowflake Notebooks as part of Streamlit.”
↩︎ Plotting with Altair, Streamlit, matplotlib and seaborn“Snowflake Notebooks currently support Altair version 4.0.”
↩︎ Plotting with Altair, Streamlit, matplotlib and seaborn“Some Streamlit chart elements are not supported in Snowflake or might be subject to additional terms.”
↩︎ Plotting with Altair, Streamlit, matplotlib and seaborn“The following code plots a stacked bar chart of all the measurements in a dataframe named df that contains the toy dataset:”
↩︎ Interpreting what the chart actually shows“To use seaborn, you must install the seaborn library for your notebook:”
↩︎ Exam trap 1“The following code plots a stacked bar chart of all the measurements in a dataframe named df that contains the toy dataset”
↩︎ Checkpoint