Data visualization is one of primary skills of any data scientist. It’s also a large field in itself. There are many courses available just focused on Data Visualization. This post is just an introduction to this much broader topic.
In this post first we will look at data visualization conceptually, then we will explore more using Python libraries.
By visualizing information, we turn it into a landscape that you can explore with your eyes, a sort of information map. And when you’re lost in information, an information map is kind of useful. ―David McCandless
Data visualizations is the process of turning large and small datasets into visuals that are easier for the human brain to understand and process.
When we have a dataset, it will take some time to make the meaning of that data. But, when we represent this data in graphs or other visualizations, it is much more easier for us to understand. That’s the power of data visualization.
You can clearly see that US defense budget almost equal as the combined budget of other countries.
I can show you many more here. There are endless supply of Data Visualizations available on internet.
These principles are directly taken from Data Visualisation: A Handbook for Data Driven Design by Andy Kirk. I highly recommend reading this book.
This means that the data presented is honestly portrayed, or the visualization is not misleading. Trust is hard to earn and easy to lose. This is very important.
Accessible is about focusing on your target audience and ability to use your visualization.
It’s important to have stylish and beautiful visualization when you present them. If you are exploring data, it might not be critical. But, if you presenting your visualization to a particular audience or submitting on some platform, you will need beautiful visualizations.
Matplotlib is a widely used visualization package in Python. It’s very easy to create and present data visualizations using Matplotlib. There are other visualization libraries available in Python.
We are going to learn how to create Bar plots, Line plots and Histograms using Matplotlib in this post. The entire code created is using Jupyter Notebooks.
Line plots are very simple plots. It represents frequency of data along a number lines. You can learn more about Line charts and Spline charts from Data Viz Project.
We’ll use Bitcoin Historical Price Dataset from Kaggle to draw line plots here.
First we’ll import all numpy, pandas and matplotlib. Then we read the data using
read_csv function from pandas.
import numpy as np import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('bitcoin_dataset.csv') data.head()
You will get an output table with 24 columns and 5 rows(Too long to print here).
We’ll need to convert the Date string to pandas datetime.
data['Date'] = pd.to_datetime(data['Date'].values)
Now we extract date and price from our data set.
date = data['Date'].values price = data['btc_market_price'].values
Now we can plot using these values.
plt.plot(date, price) plt.show()
This plot is not labelled. And the axes are not perfect. We’ll fix that now.
plt.plot(date, price, c='magenta') # Add title plt.title("BTC Price over time") # Axis labels plt.xlabel("Year") plt.ylabel("Price in USD") # Axes Range plt.axis(['2009', '2018', 0, 5000]) plt.show()
Bar Plot is chart that represents categorical data with rectangular bars. More about bar plots at Data Viz Project
We’ll use European Developers Salary data to plot bar graph. Get this data from here
At first we read the data from csv file.
salary = pd.read_csv('salary.csv') salary.columns = ['Experience', 'Salary', 'Country'] salary.head()
We will be plotting mean salary by each country. So we’ll get mean value by each country.
salary = salary.groupby(['Country']).mean() salary
Now we extract these values to plot. We are only taking first 5 countries.
country = salary.index[:5] country_array = np.arange(5) mean_salary = salary['Salary'].values[:5]
# Basic Plot plt.bar(country_array, mean_salary, color='#f44c44') # X-Axis Tick Labels plt.xticks(country_array, country) # Title plt.title("European Developers Salary") # Y-Axis Label plt.ylabel("Salary in €") plt.show()
We can clearly see that Belgium has the highest average salary and Cyprus has least average salary among these five countries.
Histogram a diagram consisting of rectangles whose area is proportional to the frequency of a variable and whose width is equal to the class interval. More about Histograms
We are going to generate some random numbers using numpy. Then we will plot histogram of these random numbers.
data = np.random.randint(low=-100, high=250, size=400) #Plot. bins=no. of bins plt.hist(data, bins=15, color='#988659') plt.xlabel('Value') plt.ylabel('Frequency') plt.title('Histogram') #Using Grids plt.grid() plt.show()
This is not showing any kind of special data. We can generate Gaussian(Normal) random numbers using numpy to create better histograms. These are just random numbers, this doesn’t represent any data.
# Mean = 5, Standard Deviation = 2, Number of points = 1000 data = np.random.normal(5, 2, 1000) #Plot. bins=no. of bins plt.hist(data, bins=10, color='#8cdcb4') plt.xlabel('Value') plt.ylabel('Frequency') plt.title('Histogram') #Using Grids plt.grid() plt.show()
So far we have learned how to create Line plots, Bar plots and Histograms using Matplotlib library. In the future posts we will learn more about how to create more plots. Also, we will use data science methods for a particular case study.