In my previous blog entry, I covered the basics of using Streamlit to inspect basic attributes of the dataframe, including numeric and categorical variables.
In this blog entry, we will cover how we can use data visualisation tools in Streamlit for data dashboarding. The advantage of using Streamlit is that we can use Python graph packages, such as Plotly and Altair to plot interactive charts that allow users to hover over the data points to query specific data point attributes. Moreover, users can also utilise widgets or radio buttons in Streamlit plot interactive scatterplots for data dashboarding. Specifically, we will focus on how we can plot pair plots and scatterplots in Streamlit. The codes are publicly available in GitHub (saved under iris.py) and the website instance is hosted at: https://share.streamlit.io/kuanrongchan/iris_dataset/main/iris.py.
To plot pairplots, we will import seaborn. The rationale has been described previously, and we can use the codes we optimised from Jupyter Notebooks into Streamlit. We first need to load the additional packages:
import seaborn as sns
Then we plot the pairplot using the commands (with the header and subheader descriptions) below:
st.header('Visualising relationship between numeric variables') st.subheader('Pairplot analysis') g = sns.pairplot(df, vars = ["sepal_length", "sepal_width", "petal_length", "petal_width"], dropna = True, hue = 'species', diag_kind="kde") g.map_lower(sns.regplot) st.pyplot(g)
Output file is as follows:
After plotting pair plots, we may want to have a close-up analysis using scatterplots. The advantage of using Streamlit is that we can use widgets to allow users plot a scatterplot of any 2 selected variables.
st.subheader('Scatterplot analysis') selected_x_var = st.selectbox('What do you want the x variable to be?', df.columns) selected_y_var = st.selectbox('What about the y?', df.columns) fig = px.scatter(df, x = df[selected_x_var], y = df[selected_y_var], color="species") st.plotly_chart(fig)
Output is as shown below. I have annotated the widgets, which allows users to select and click on any variables they desire for scatterplot analysis. You may hover over the data points to zoom into the data point attributes, as this graph was plotted using Plotly.
We can also show the correlation coefficients and the associated p-values for the scatterplots to indicate if their relationship is linearly correlated. We first add and load the required packages:
from scipy.stats import pearsonr from sklearn import linear_model, metrics from sklearn.metrics import r2_score from scipy import stats
We can then calculate the Pearson and Spearman coefficients, together with the associated p-values using the following commands:
#Correlation calculations (Pearson) st.subheader("Pearson Correlation") def calc_corr(selected_x_var, selected_y_var): corr, p_val = stats.pearsonr(selected_x_var, selected_y_var) return corr, p_val x = df[selected_x_var].to_numpy() y = df[selected_y_var].to_numpy() correlation, corr_p_val = calc_corr(x, y) st.write('Pearson correlation coefficient: %.3f' % correlation) st.write('p value: %.3f' % corr_p_val) #Correlation calculations (Spearman) st.subheader("Spearman Correlation") def calc_corr(selected_x_var, selected_y_var): corr, p_val = stats.spearmanr(selected_x_var, selected_y_var) return corr, p_val x = df[selected_x_var].to_numpy() y = df[selected_y_var].to_numpy() correlation, corr_p_val = calc_corr(x, y) st.write('Spearman correlation coefficient: %.3f' % correlation) st.write('p value: %.3f' % corr_p_val)
Output is as follows:
Pearson correlation coefficient: -0.109
p value: 0.183
Spearman correlation coefficient: -0.159
p value: 0.051
Hence, sepal length and sepal width are not linearly correlated.
As you may appreciate, the Streamlit commands allow you to organise the data by adding headers and subheaders. In this addition, they provide the tools that allow users to interactively explore their dataset, preventing the need to plot every pairwise permutations for scatterplots. The outcome is a neat and professional dashboard that can be readily deployed and shared with others.