Visualize data in Snowflake Notebooks

In Snowflake Notebooks, you can use your favorite Python visualization libraries, such as matplotlib and plotly, to develop your visualizations.

本主题介绍如何使用以下库在笔记本中可视化数据:

数据集

The examples in this topic use the following toy dataset that is based on the Palmer’s Penguin dataset (https://allisonhorst.github.io/palmerpenguins/articles/intro.html).

speciesmeasurementvalue
adelibill_length37.3
adeliflipper_length187.1
adelibill_depth17.7
chinstrapbill_length46.6
chinstrapflipper_length191.7
chinstrapbill_depth17.6
gentoobill_length45.5
gentooflipper_length212.7
gentoobill_depth14.2

您可以使用以下代码在笔记本中创建此数据集:

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})
df

使用 Altair 可视化结果

Altair is imported by default on Snowflake Notebooks as part of Streamlit. Snowflake Notebooks currently support Altair version 4.0. For details on available visualization types when using Altair, see Vega-Altair: Declarative Visualization in Python (https://altair-viz.github.io/index.html).

The following code plots a stacked bar chart of all the measurements in a dataframe named df that contains the toy dataset:

import altair as alt
alt.Chart(df).mark_bar().encode(
    x= alt.X("measurement", axis = alt.Axis(labelAngle=0)),
    y="value",
    color="species"
)

运行单元格后,将显示以下可视化效果:

A stacked bar chart showing the stacked values of each of the measurements for each penguin type.

使用 matplotlib 可视化结果

要使用 matplotlib,请为您的笔记本安装 matplotlib 库:

  1. From the notebook, select Packages.
  2. Locate the matplotlib library and select the library to install it.

The following code plots the toy dataset, df, using matplotlib:

import matplotlib.pyplot as plt

pivot_df = pd.pivot_table(data=df, index=['measurement'], columns=['species'], values='value')

import matplotlib.pyplot as plt
ax = pivot_df.plot.bar(stacked=True)
ax.set_xticklabels(list(pivot_df.index), rotation=0)

运行单元格后,将显示以下可视化效果:

A stacked bar chart showing the stacked values of each of the measurements for each penguin type.

For more details on using the st.pyplot chart element, see st.pyplot (https://docs.streamlit.io/library/api-reference/charts/st.pyplot).

使用 plotly 可视化结果

要使用 plotly,请为您的笔记本安装 plotly 库:

  1. From the notebook, select Packages.
  2. Locate the plotly library and select the library to install it.

The following code plots a bar chart of the penguin measurements from the toy dataset, df:

import plotly.express as px
px.bar(df, x='measurement', y='value', color='species')

运行单元格后,将显示以下可视化效果:

A stacked bar chart showing the stacked values of each of the measurements for each penguin type.

使用 seaborn 可视化结果

要使用 seaborn,您必须为您的笔记本安装 seaborn 库:

  1. From the notebook, select Packages.
  2. Locate the seaborn library and select the library to install it.

The following code plots a bar chart of the penguin measurements from the toy dataset, df:

import seaborn as sns

sns.barplot(
    data=df,
    x="measurement", hue="species", y="value",
)

运行单元格后,会显示以下可视化效果:

Bar chart showing each of the measurement values for each penguin type.

For more examples of seaborn visualizations, see the seaborn Example gallery (https://seaborn.pydata.org/examples/index.html).

使用 Streamlit 可视化结果

Streamlit is imported by default in Snowflake Notebooks. You can use chart elements supported by Streamlit version 1.39.0 to create a line chart, bar chart, area chart, or a map with points on it. See Chart elements (https://docs.streamlit.io/library/api-reference/charts) .

Note

Some Streamlit chart elements are not supported in Snowflake or might be subject to additional terms. See Streamlit support in notebooks.

To visualize the toy dataset, df, in a bar chart, you can use the following Python code:

import streamlit as st

st.bar_chart(df, x='measurement', y='value', color='species')

运行两个单元格后,会显示以下可视化效果:

Bar chart that stacks the penguin measurements for each penguin species.

To learn more about how you can build interactive data apps with Streamlit, see Streamlit in notebooks.