×

Seaborn with STREAMLIT

Searborn Streamlit Feature

Seaborn is an amazing data visualization library for statistical graphics plotting in Python. Seaborn provides beautiful default styles and color palettes to make statistical plots more attractive.

Seaborn is built on the top of the matplotlib library and is also closely integrated into the data structures from pandas. 

Installation

pip install seaborn
pip install matplotlib
pip install pandas
pip install streamlit

Getting Data-Set

The Seaborn module has built-in datasets. We will use these datasets for practicing Seaborn.

import seaborn as sns

print(sns.get_dataset_names())
Output
['anagrams', 'anscombe', 'attention', 'brain_networks', 'car_crashes',
 'diamonds', 'dots', 'exercise', 'flights', 'fmri', 'gammas', 'geyser',
 'iris', 'mpg', 'penguins', 'planets', 'tips', 'titanic']

We can choose any of the data-set from the above output. And get insides of any data-set using head() function.

import seaborn as sns

data_frame = sns.load_dataset('planets')
print(data_frame.head())
Output
      method           number orbital_period mass   distance year
0  Radial Velocity       1         269.300   7.10     77.40  2006
1  Radial Velocity       1         874.774   2.21     56.95  2008
2  Radial Velocity       1         763.000   2.60     19.84  2011
3  Radial Velocity       1         326.030  19.40    110.62  2007
4  Radial Velocity       1         516.220  10.50    119.47  2009

Line Plot

line plot is a way to display data along a number line. Lets create a Line plot using Seaborn and embed the plot into our streamlit app.

  • st.sidebar.selectbox(), let us customize the option within the selectbox (i.e drop down menu).
import streamlit as st
import matplotlib.pyplot as plt
import seaborn as sns

data_frame = sns.load_dataset('planets')

def main():
    page = st.sidebar.selectbox(
        "Select a Page",
        [
            "Line Plot"
        ]
    )
    linePlot()
  • Seaborn has lineplot() funtion to build a line plot.
  • The figure() function in pyplot module of matplotlib library is used to create a new figure, a blank sheet of size- M * N, where M & N are passed as a parameter.
def linePlot():
    fig = plt.figure(figsize=(10, 4))
    sns.lineplot(x = "distance", y = "mass", data = data_frame)
    st.pyplot(fig)
if __name__ == "__main__":
    main()
Command to run the streamlit app:
streamlit run app.py

app.py is the file name of the Streamlit app that we have created so far.

Output
Line Plot Seaborn 1

Count Plot

The countplot is used to represent the occurrence(counts) of the observation present in the categorical variable. Lets add a new page in our streamlit app for count plot and embed a count plot into the page.

def main():
    page = st.sidebar.selectbox(
        "Select a Page",
        [
            "Count Plot", #New page
        ]
    )

    if page == "Line Plot":
        linePlot()
    
    elif page == "Count Plot":
        countPlot()
  • Seaborn has countplot() funtion to create a count plot.
def countPlot():
    fig = plt.figure(figsize=(10, 4))
    sns.countplot(x = "year", data = data_frame)
    st.pyplot(fig)
Output
Count Plot Seaborn 1

Violin Plot & Strip Plot

Violin Plot

A violin plot depicts distributions of numeric data for one or more groups using density curves. The width of each curve corresponds with the approximate frequency of data points in each region.

Violin Plot Seaborn

Strip Plot

A strip plot is a graphical data analysis technique for summarizing a univariate data set. A strip plot is simply a plot of the sorted response values along one axis.

The strip plot is an alternative to a histogram or a density plot. The strip is typically preferred to used with small data sets.

Strip Plot Seaborn

Lets create a page in our streamlit application for Violin and Strip plot combined, where we can switch between both plots.

def main():
    page = st.sidebar.selectbox(
        "Select a Page",
        [
            "Violin & Strip Plot" #New Page
        ]
    )
   violinStrip_plot()
  • Streamlit.header() is used to print the heading in the streamlit.
  • For creating a drop down menu in streamlit we can use streamlit.selectbox() funtion. Selectbox funtion takes selectbox name(i.e Select a plot) and options in the drop down menu.
  • Seaborn have violinplot() and stripplot() funtions to plot the graphs, both funtions takes input for X and Y axes.
def violinStrip_plot():
    st.header("Violin & Strip Plot")
    sd = st.selectbox(
        "Select a Plot", #Drop Down Menu Name
        [
            "Violin Plot", #First option in menu
            "Strip Plot"   #Seconf option in menu
        ]
    )

    fig = plt.figure(figsize=(12, 6))

    if sd == "Violin Plot":
        sns.violinplot(x = "year", y = "mass", data = data_frame)
    
    elif sd == "Strip Plot":
        sns.stripplot(x = "mass", y = "distance", data = data_frame)

    st.pyplot(fig)
Output
Violin Plot Op Streamlit 1
Violin Plot
Strip Plot Op Seaborn 1
Strip Plot