Change language

How to Create a Beautiful Python Visualization Dashboard With Panel/Hvplot

How to Create a Beautiful Python Visualization Dashboard With Panel/Hvplot

hey friends welcome back to my channel if youre new here my name is tufu im a data science consultant working here in the netherlands in todays video ill teach you how to create an interactive visualization dashboard in python an interactive dashboard is a great way to share insights from the data and the results of your data science project of course you can also create dashboards in bi2 such as tableau or power bi but using python we have the flexibility to create a lot of different types of visualization and we can also customize our visualization however we want for this visualization project well be using a data set from our world in data website about the worlds carbon dioxide emissions over the history if you want to customize this project to make it your own portfolio project please feel free to pick another data set that you are interested in so before we start lets take a look at some popular visualization platforms and libraries in r and python this graph shows a different dashboarding tools and their popularity over time over the past few years dash and streamlet have gained a lot more popularity as dashboarding solutions in bison however x2 has their own pros and cons and so its important for us to know when to use which if youre working with duped the notebooks for your project foiler and panel are the two libraries that support working with tube the notebooks which mean s you can turn your notebook into a dashboard mean while dash streamlet and flask do not support this in addition each of them is suitable or optimal for a certain use case but i wont go deeper into the comparison in this video for now i have two options namely boiler and panel as im planning to work with two with the notebooks i found out that voila is actually not very flexible in terms of how you can customize and design the looks of your dashboard so for this reason im gonna go for panel so what the heck is pano pano is an open source python library that lets you create custom interactive web apps and dashboards by connecting user-defined widgets to plots images tables or text panel is developed by anaconda i mean the anaconda company not the anaconda its one of the seven packages in the whole of this ecosystem heres a simplified overview of how these seven packages work together to help turn a pandas data frame into a visualization dashboard okay thats a lot of information if youre still hanging there lets now really get our hands dirty and get started with our project first of all lets create a project photo called co2 dashboard and then go into this folder now we want to create a virtual environment for our project this will create an isolated environment for our python project so that this project can have its own dependencies and packages regardless of whatever dependencies every other project has this step is crucial to make our visualization work properly because ive encountered some really strange issues with the visualization in jupiter lab if we dont do this if youre on windows you need to use another comment for this but ill put it somewhere on the screen for you well be working with two libraries namely h3plot and panel lets install svplot and also to be the lab for our project for panel we need to install it in the base environment instead of the virtual environment because otherwise i found out that the plot would not be interactive in jupiter lab its kind of strange and i still dont understand exactly why but anyway lets go out of our virtual environment and install panel now lets go back again into our virtual environment and launch the jupiter lab now we want to create a new notebook and import the necessary libraries namely pandas numpy panel and an extension called tabulator for creating interactive tables and lastly we import the hp plot.pandas which allows us to create interactive dataframes more on that in a second now lets read in the dataset that we want to use as you can see for x country in the world and for each year in the history we have the data for each measure of co2 emissions if we print out all the columns we can see that we have data on co2 amounts co2 per capita co2 from different sources such as coal oil and gas we also have other information such as gdp and population of the countries please note that the country column here also includes continents and the world as a whole for example we can filter this data for the world or europe or north america looking at this data set we can brainstorm on what kind of visualization we want to build in our dashboard and how exactly we want our dashboard to look like i want our dashboard to have a header a sidebar with some text and the overall settings and control widgets then in the main body of the dashboard we can have a few different visualizations first of all we can visualize the co2 emission over time by continent next to this visualization we can add a table to show the exact amount of co2 per continent just for further information for the next graph it could be interesting to plot the co2 emissions against the gdp per capita so each country will be a dot on this scatter plot we want to see if co2 emissions are associated with higher or lower gdp or if theres no correlation at all in the last visual what shall we do i dont have idea but okay maybe we can plot the amount of co2 emissions from different sources such as coal oil or gas per continent so we can have a comparison of which continent produces the most of a certain source of co2 for over the graphs we can have an overarching interactive control for this data set as we have many different years i think it makes sense to have a slider here for the year going back to our notebook im a bit short in time for filming today so ill walk you through the goat and not do the full life coding anyway first of all well do some pre-processing to replace the na values with zeros and we also create a new column for gdp per capita by dividing the gdp by the population we have to be a bit careful here with dividing by zero values now we need to make our data frame interactive by calling this interactive method on our data frame youll see in a bit what we can do with it please bear with me next we want to create a few widgets for our visualizations the widgets are the interactive elements on our dashboard like sliders radio buttons or drop-down menus that users can use to configure our visualization if you go to the panels documentation page you can see a lot of different types of widgets you can create for example slider radio button and also how exactly to create them so here we go i have checked for the mean and the max value of the year in our data set so i have put them here next we want to create a radial button widget for the different measures of co2 which is the y-axis of our first chart in this case im interested in co2 and co2 per capita so i include them here now we need to connect the data pipeline with the widget so that every time the widgets change the underlying data for our visualization is also updated so here i just select only the years that are before the year value in the slider and then we aggregate the data by country and u column so heres what the data pipeline looks like now lets move on to create the chart using this data pipeline its very simple just one line of gold and you can see that our chart is interactive and if we move the slider the year is now also moving now lets move on to the table which use the same underlying data only that we want to present the data in the table format and for this we can use the type later extension that we have imported and you can see that the table also has exactly the same widgets and the table is also interactive for the co2 and gdp scatter plot we want to create a different pipeline because now we want to filter the data set based on the exact year on the year slider and also we want to select only the countries and not the continents and again the scatter plot is also very simple to create with hp plot so here we have the x-axis being gdp per capita and the y-axis being the co2 and if we move the slider the scatter plot is also updated for the last visualization which is the co2 sources by continent i create a new radio button widget for different sources of co2 core co2 oil co2 and gas co2 now in a very similar manner as for the previous charts we create data pipeline and connect it to the widgets and then we can use hp plot to plot the data and now we can select different co2 sources and see which continent produces the most of them so now that we have created all the visualizations that we need the last step would be to create a dashboard which is simply to put all the visualizations together on one page luckily pano has a lot of dashboarding templates that allow us to put very easily all the visualizations together ill choose the fast list template and we can define here the title the sidebar which is the left panel on the dashboard we can add here the text or the images in markdown formats and we also want to include our year slider on this sidebar next well define the main body of the dashboard so the whole concept is very similar to how we design websites so everything can be put in rows and columns as our dashboard has two rows i have two rows here so the first row has two columns one for the line chart and the other one is for the table now for the line chart i also want to include the the widget which belongs to this graph so thats why i include this widget here in this column and the right hand side column is just a table so its just this for the second row we also have very similar structure we have two columns and the first column is the scatter plot and the second column is the bar chart with its own widgets now after we define the template we just need to make it serveable now i can actually serve this dashboard by going to the terminal and using the command panel serve so yes this is our dashboard it doesnt look bad right and you can move the slider and click on things to play with it ill post all the code to my github and also share it with you in the description below so you can take a look at it in more detail and also use it for your own project if you get any value from this video please smash the like button to support my channel for more portfolio projects like this check out my portfolio project playlist on my channel thank you for watching bye [Music]

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically