Covid-19 data analysis dashbord in Python by Plotly Dash

Covid-19 data analysis dashbord in Python by Plotly Dash
Covid-19 data analysis dashbord in Python by Plotly Dash
Covid-19 data analysis dashbord in Python by Plotly Dash
Covid-19 data analysis dashbord in Python by Plotly Dash

Build covid-19 data analysis dashbord in python using interactive plotly data visualization library and dash web app library.

There are seven graphs and one drop down list in this interactive Covid-19 data analysis dashbord in Python by Plotly Dash. There are only two graphs are connected with drop down list and data dynamically changed on these two graphs by changing the country name from the drop down list. Scattermapbox graph is static and displays data for each country on its geographic location.Donut graph displays total global cases in number and percentage format.Two horizontal bar graphs display top ten countries confirmed cases and deaths respectively. Combination of bar chart and line chart show daily confirmed cases and rolling average of the last seven days.

Import Library

Lines 1-6

Start creating this dashboard by importing necessary library. Make sure you have installed this library in your machine or code editor. I recommend you PyCharm editor to run dash apps in this code editor. I have used dash library, dash html components, dash core components, plotly interactive graph objects data visualization library and pandas for data analysis and manipulating data for creating this dashboard.

Data

Lines 10-11

I have used Kaggle datasets for creating this dashboard in python by plotly dash. You can download this datasets using the below link. Find this link above the code template. In the code template, you can see I have used two CSV data files in this dashboard. First note the name of files and then download these two CSV data files using the below link from the Kaggle website.

Title

Lines 13-28

Create the layout of dash app. Inside the layout of dash app, first give the name of this dashboard as you want and style it using the html style properties and place it right side of your dashboard. Change the width and margin of this title as according to your system screen resolution. Also give the author name of this dashboard as you want and style it using the html style library.

Drop Down List

Lines 32-52

In this dashboard, I have inserted first chart scattermapbox map chart before the drop down list. First of all, create the layout of scattermapbox map chart. After that create drop down list using dash core components. There are many dash core components, I have used here in this dashboard only drop down list. Drop down list is most useful component in dash apps. I have used many properties of this component, you can see below in the code template.

Charts

Lines 55-105

There are total seven charts in this dashboard. One chart is above the drop down list. Other six charts are below the drop down list. Create the layout of these charts and give unique id to each chart. Adjust the position of these charts in the dash app layout according to your system screen resolution because I have not used here dash bootstrap components. Using dash bootstrap components in the dash app, the position of charts do not disturb in any screen resolution.

Data and Layout for each chart

Lines 108-667

Finally, I display data on the charts. Frist of all, I create new data frame for scattermapbox chart. You can see this data frame below the define chart function line 111 in the code template. Scattermapbox chart is an interactive tool of Mapbox that also works with plotly. The most important functionality of this tool is that we can go inside it deeply e.g. we can view streets using this tool. You need to create free account on Mapbox to use token because token is required in scattermapbox to work it properly. You can create free mapox account using below link. This map chart works with latitude and longitude. Make sure your data have latitude and longitude columns. If not, you can add latitude and longitude columns for specific locations. We can control size of bubbles by changing the numerical value. You can view line 119 in the code template below.

Second chart is bar chart. It displays daily new cases for each country. By selecting any country name from the list, this chart displays daily new cases values. Create specific data frame for this bar chart and use this data frame to display values on the bar chart and also create hover effect using this data frame. More details about the property of this chart can be viewed in the code template or you can mention any issue in the comment box.

Donut chart is third chart. It displays total global cases values from the specific date range. This chart is static and only shows hover effect by hovering any slice of this chart. The working of both pie and donut charts are same. Only differentiate one parameter with each other that is hole. I have used here in this chart hole size .7. All other properties are same for pie and donut charts.

Fourth chart is the combination of line and stack bar chart. This chart is dynamically changed by selecting any input value from the drop down list. Line represents daily new cases over the period and stack bars represent daily deaths and daily recovered cases over the period for specific input value. Make sure you have selected bar mode stack in the layout of this chart. You can see in the code template below line 342.

Horizontal bar chart is fifth chart. This chart represents top 10 countries by confirmed cases over the period. Create specific data frame for this kind of chart and also create hover effect on this chart using this data frame. You can see other properties for this chart in the code template below. You can see the code for this chart in the code template from 406 to 484 lines. Don’t forget to put orientation parameter is equal to h. Because it represents horizontal orientation of bars.

The next chart is the combination of line and bars. It represents rolling average of the last seven days of the daily new cases over the period. You need to create new data frame for this type of chart. You can view that data frame in the lines 572 and 573. I have created new column in this data frame using rolling function of pandas and taken mean in the line 573. You can view other properties of this type of chart in the code template below.

Another horizontal bar chart displays data for top 10 countries by deaths for date range in the specific period. This chart is also static or it works as output. Create new data frame for this type of chart and mention orientation property horizontal using h letter in the data section. You can view code for this type of chart in the code template from lines 487 to 565.

Create free Mapbox account using below link to use token in the scattermapbox map chart.

https://account.mapbox.com/auth/signup/

Download the csv data file using the below link.

https://www.kaggle.com/imdevskp/corona-virus-report

To see full code,hover on the code area and use horizontal scrollbar if needed.


If you have any problem in learning above code, please comment in the comment box. I will help you as soon as possible.


Recommended For You

About the Author: Mubeen Ali

I am passionate about learning Dash. Dash is a python framework that creates beautiful web based data visualization dashboards.