In [75]:
import param
import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import panel as pn
import hvplot as hv

# make plots static
%matplotlib inline

# add a grid to the plots 
plt.rcParams['axes.grid'] = True
plt.rcParams['axes.axisbelow'] = True

pn.extension()

df = pd.DataFrame({'Animal':['Pig', 'Goat' ,'Sheep', 'Frog', 'Goat', 'Goat', 'Pig', 'Sheep', 
                             'Octopus', 'Goat', 'Pig', 'Sheep', 'Octopus', 'Frog', 'Goat', 'Goat',
                            'Pig', 'Pig', 'Sheep', 'Frog', 'Frog', 'Octopus', 'Octopus'], 
                   'Rating':[3, 10, 3, 2, 9, 10, 4, 1, 
                             1, 8, 5, 6, 2, 4, 10, 9,
                            5, 5, 3, 2, 3, 1, 1]})

class RatingsDashboard(param.Parameterized):
    
    # widget containing the list of animals
    Animal = param.ObjectSelector(default='Goat', objects=list(df.Animal.unique()))
    
    title = 'Ratings for '
    xlabel = 'Rating'
    
    # seaborn strip plot for all ratings for all animals
    def strip_view_all(self):
        ax = sns.stripplot(data = df, x='Animal', y='Rating', jitter=True, size=10)
        plt.xticks(size=14)
        plt.yticks(size=14)
        plt.xlabel('')
        plt.ylabel('Rating', size=14)
        plt.close()
        return ax.figure
    
    # create data set containing only the data applicable to the chosen animal
    def get_data(self):
        class_df = df[(df.Animal==self.Animal)].copy()
        return class_df
    
    # histogram of ratings for the chosen animal
    def hist_view(self):
        data = self.get_data()
        title = "Histogram: " + self.title
        
        plot = plt.figure()
        plot.add_subplot(111).hist(data['Rating'], width=1, color='#2d53a6')
        plt.title(title + self.Animal, size=14)
        plt.xlabel(self.xlabel, size=14)
        plt.xticks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], size=14)
        plt.yticks([0, 1, 2, 3,4], size=14)
        plt.ylim(0, 4)
        plt.xlim(0, 11)
        
        plt.close(plot)
        return plot
    
    # seaborn box plot per animal
    def box_view(self):
        data = self.get_data()
        title = "Box Plot: " + self.title
        
        ax = sns.boxplot(data['Rating'], color='#e0c916')
        plt.title(title + self.Animal, size=14)
        plt.xticks(size=14)
        plt.yticks(size=14)
        plt.xlabel('Rating', size=14)
        plt.ylabel('')
        plt.xlim(0, 11)
        plt.close()
        return ax.figure
    
    # table of data for the chosen animal
    def table_view(self):
        data = self.get_data().style.hide_index()
        return data

# create an instance of the class
rd = RatingsDashboard(name='')

dashboard_title = '# Animal Ratings Dashboard'
dashboard_desc = '#### An example of a simple interactive HoloViz Panel dashboard using a dummy data set of animal ratings.'
dashboard_note = "#### Click 'Show Code' to see the code used to generate this."
tab1_title = '#### Strip Plot of All Animal Ratings'
tab2_title = '#### Choose an animal:'

tab1 = pn.Row(pn.Column(tab1_title, rd.strip_view_all))
tab2 = pn.Column(tab2_title, rd.param, pn.Row(rd.hist_view, rd.box_view), rd.table_view)

dashboard = pn.Column(dashboard_title, dashboard_desc, dashboard_note, 
                      "#### Click on the 'Per Animal' tab to see a histogram, box plot, and data table for each animal.", '', 
                      pn.Row(pn.Tabs(('All', tab1), ('Per Animal', tab2))))
In [76]:
dashboard.embed(max_opts = 5)