• Latest
  • Trending
Make Beautiful Interactive Visualizations With Pandas

Make Beautiful Interactive Visualizations With Pandas

December 29, 2021
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
Instagram fined €405m over children’s data privacy

Instagram fined €405m over children’s data privacy

September 6, 2022
8 Most Common Causes of a Data Breach

5.7bn data entries found exposed on Chinese VPN

August 18, 2022
Fibre optic interconnection linking Cameroon and Congo now operational

Fibre optic interconnection linking Cameroon and Congo now operational

July 15, 2022
Ericsson and MTN Rwandacell Discuss their Long-Term Partnership

Ericsson and MTN Rwandacell Discuss their Long-Term Partnership

July 15, 2022
Airtel Africa Purchases $42M Worth of Additional Spectrum

Airtel Africa Purchases $42M Worth of Additional Spectrum

July 15, 2022
Huawei steps up drive for Kenyan talent

Huawei steps up drive for Kenyan talent

July 15, 2022
TSMC predicts Q3 revenue boost thanks to increased iPhone 13 demand

TSMC predicts Q3 revenue boost thanks to increased iPhone 13 demand

July 15, 2022
Facebook to allow up to five profiles tied to one account

Facebook to allow up to five profiles tied to one account

July 15, 2022
Top 10 apps built and managed in Ghana

Top 10 apps built and managed in Ghana

July 15, 2022
MTN Group to Host the 2nd Edition of the MoMo API Hackathon

MTN Group to Host the 2nd Edition of the MoMo API Hackathon

July 15, 2022
KIOXIA Introduce JEDEC XFM Removable Storage with PCIe/NVMe Spec

KIOXIA Introduce JEDEC XFM Removable Storage with PCIe/NVMe Spec

July 15, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Sunday, 29 January, 2023
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

Make Beautiful Interactive Visualizations With Pandas

by ITECHNEWS
December 29, 2021
in Data Science, Leading Stories
0 0
0
Make Beautiful Interactive Visualizations With Pandas

If you’ve been following my data visualization guides, you probably know that I like creating nice-looking and readable visualizations in Python without getting too technical and wasting a lot of time.

Interactive visualizations are no exception, so I’ve been searching for friendly libraries in Python for quite some time. There are many libraries out there that do the job, but when it comes to working along with Pandas, things get messy.

YOU MAY ALSO LIKE

Inaugural AfCFTA Conference on Women and Youth in Trade

Instagram fined €405m over children’s data privacy

Fortunately, there’s an easy way to create interactive visualizations directly from Pandas, and we’ll see how it works in detail in this guide.

 

First things first

 

Install the libraries

To easily create interactive visualizations, we need to install Cufflinks. This is a library that connects Pandas with Plotly, so we can create visualizations directly from Pandas (in the past you had to learn workarounds to make them work together, but now it’s simpler)

First, make sure you install Pandas and Plotly running the following commands on the terminal:

pip install pandas
pip install plotly

 

Note that you can also use conda to install Plotlyconda install -c plotly.

Once you have Plotly installed, run the following commands to install Cufflinks:

pip install cufflinks

 

Import the libraries

To start working, import the following libraries:

import pandas as pd
import cufflinks as cf
from IPython.display import display,HTMLcf.set_config_file(sharing='public',theme='ggplot',offline=True) 

 

In this case, I’m using the ‘ggplot’ theme, but feel free to choose any theme you want. Run the command cf.getThemes() to get all the themes available.

To make interactive visualizations with Pandas in the following sections, we only need to use the syntaxdataframe.iplot().

 

The data

For this guide, we’ll use a population dataframe. First, download the CSV file available on Google Drive or Github, move the file where your Python script is located, and then read it in a Pandas dataframe as shown below.

df_population = pd.read_csv('population_total.csv')

 

The dataframe contains the population throughout the years for most countries around the world and looks like this:

 


The Easiest Way to Make Beautiful Interactive Visualizations With Pandas
Image by author
 

 

This dataframe is almost ready for plotting, we just have to drop null values, reshape it and then select a couple of countries to test our interactive plots. The code shown below does all of this.

# dropping null values
df_population = df_population.dropna()# reshaping the dataframe
df_population = df_population.pivot(index='year', columns='country',
                                    values='population')# selecting 5 countries
df_population = df_population[['United States', 'India', 'China', 
                               'Indonesia', 'Brazil']]

 

Now the dataframe look like the picture below and is ready for plotting.

 


The Easiest Way to Make Beautiful Interactive Visualizations With Pandas
Image by author
 

 

If you’d like to know how a data scientist collects real-world data like this “population_total.csv”, check this guide I made. (you won’t always have a Kaggle dataset available for a data science project)

Now let’s start making interactive visualization!

 

Lineplot

Let’s make a lineplot to compare how much the population has grown from 1955 to 2020 for the 5 countries selected.

As mentioned before, we will use the syntax df_population.iplot(kind='name_of_plot') to make plots as shown below.

df_population.iplot(kind='line',xTitle='Years', yTitle='Population',
                    title='Population (1955-2020)')

 

 

 

The Easiest Way to Make Beautiful Interactive Visualizations With Pandas

Image by author

 

 

At first glance, it’s easy to see that the population in India has grown faster than the other countries.

 

Barplot

We can make a single barplot on barplots grouped by categories. Let’s have a look.

 

Single Barplot

Let’s create a barplot that shows the population of each country by the year 2020. To do so, first, we select the year 2020 from the index and then transpose rows with columns to get the year in the column. We’ll name this new dataframe df_population_2020 (we’ll use this dataframe again when plotting piecharts)

df_population_2020 = df_population[df_population.index.isin([2020])]
df_population_2020 = df_population_2020.T

 

Now we can plot this new dataframe with .iplot(). In this case, I’m going to set the bar color to light-green using the color argument.

df_population_2020.iplot(kind='bar', color='lightgreen',
                         xTitle='Years', yTitle='Population',
                         title='Population in 2020')

 

 

 

The Easiest Way to Make Beautiful Interactive Visualizations With Pandas

Image by author

 

 

Barplot grouped by “n” variables

Now let’s see the evolution of the population at the beginning of each decade.

# filter years out
df_population_sample = df_population[df_population.index.isin([1980, 1990, 2000, 2010, 2020])]# plotting
df_population_sample.iplot(kind='bar', xTitle='Years',
                           yTitle='Population')

 

 

 

The Easiest Way to Make Beautiful Interactive Visualizations With Pandas

Image by author

 

 

Naturally, all of them increased their population throughout the years, but some did it at a faster rate.

 

Boxplot

Boxplots are useful when we want to see the distribution of the data. The boxplot will reveal the minimum value, first quartile (Q1), median, third quartile (Q3), and maximum value. The easiest way to see those values is by creating an interactive visualization.

Let’s see the population distribution of the USA.

df_population['United States'].iplot(kind='box', color='green', 
                                     yTitle='Population')

 

 


The Easiest Way to Make Beautiful Interactive Visualizations With Pandas
Image by author
 

 

Let’s say now we want to get the same distribution but for all the selected countries.

df_population.iplot(kind='box', xTitle='Countries',
                    yTitle='Population')

 

 

 

The Easiest Way to Make Beautiful Interactive Visualizations With Pandas

Image by author

 

 

As we can see, we can also filter out any country by clicking on the legends on the right.

 

Histogram

A histogram represents the distribution of numerical data. Let’s see the population distribution of the USA and Indonesia.

df_population[['United States', 'Indonesia']].iplot(kind='hist',
                                                xTitle='Population')

 

 

 

The Easiest Way to Make Beautiful Interactive Visualizations With Pandas

Image by author

 

 

Piechart

Let’s compare the population by the year 2020 again but now with a piechart. To do so, we’ll use the df_population_2020 dataframe created in the “Single Barplot” section.

However, to make a piechart we need the “country” as a column and not as an index, so we use .reset_index() to get the column back. Then we transform the 2020 into a string.

# transforming data
df_population_2020 = df_population_2020.reset_index()
df_population_2020 =df_population_2020.rename(columns={2020:'2020'})# plotting
df_population_2020.iplot(kind='pie', labels='country',
                         values='2020',
                         title='Population in 2020 (%)')

 

 

 

The Easiest Way to Make Beautiful Interactive Visualizations With Pandas

Image by author

 

 

Scatterplot

Although population data is not suitable for a scatterplot (the data follows a common pattern), I would make this plot for the purposes of this guide.

Making a scatterplot is similar to a line plot, but we have to add the mode argument.

df_population.iplot(kind='scatter', mode='markers')

 

 


The Easiest Way to Make Beautiful Interactive Visualizations With Pandas
 

That’s it! Now you’re ready to make your own beautiful interactive visualization with Pandas.

Source: Frank Andrade, Data Scientist and Python Instructor
Tags: PandasPython
ShareTweetShare
Plugin Install : Subscribe Push Notification need OneSignal plugin to be installed.

Search

No Result
View All Result

Recent News

Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
Instagram fined €405m over children’s data privacy

Instagram fined €405m over children’s data privacy

September 6, 2022
8 Most Common Causes of a Data Breach

5.7bn data entries found exposed on Chinese VPN

August 18, 2022

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
Instagram fined €405m over children’s data privacy

Instagram fined €405m over children’s data privacy

September 6, 2022

Recent News

  • Inaugural AfCFTA Conference on Women and Youth in Trade September 6, 2022
  • Instagram fined €405m over children’s data privacy September 6, 2022
  • 5.7bn data entries found exposed on Chinese VPN August 18, 2022
  • Fibre optic interconnection linking Cameroon and Congo now operational July 15, 2022
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© 2021-2022 iTechNewsOnline.Com - Powered by BackUPDataSystems

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© 2021-2022 iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Go to mobile version