Data Visualization with Python pt. i
Tags: python, datascience
Published: 2021-01-15 16:49:41

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/beibp8ow4r68cp57h5wr.jpg)

First things first, all the code I cover on this lecture it's right here:
>>> https://github.com/hugoestradas/Data_Visualisation_with_Python.git

Part 1: Using Matplitlib for the Very First Time

Matplotlib is a popular data visualization library for Python.

The reason I'm going to use it, it's because it's fairly easy to use and out of many Python data visualization libraries it's the most commonly used one.

With Matplotlib you'll be able to create many different types of charts.

Let's see how to create a line chart with Matplotlib:

The first thing to do is to import the Matplotlib module:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/45dpmtnwlsjr3pd0ezqx.png)

Now to start plotting data I'll use the following line:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fkbzqc647ht14f0dgqhm.png)

This line says "put 1, 2 and 3 in the 'x' axis; and 1, 4 and 9 in the 'y' axis".
To show this plot, it's necessary the following line:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rjeiu89vdhrfscousmi6.png)

It is possible to add labels for the 'x' and 'y' axis and a title for the whole plot:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/phlte2pt19ziqlsqa85r.png)

The whole cell should look like this, and the end plot should be the following:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8scfhlgn7miinhbnuhyv.PNG)

It's also possible to plot multiple lines on the same plot:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7gx1savv4dinxuq8lkpy.png)

And the plot looks like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4fxq1ehdwu0re1y95n0s.PNG)

To clarify the values of each line, it is possible to define them by name using the "plt.legend" method:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/io6tosoityx83a3qcagn.png)

And the plot looks like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lyowl8h36khj7ar8ai2j.PNG)

It is possible to export the plot as an image as well:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ddcevg0vrcy6rqnc2ukt.PNG)

Part 2: Using Pandas

Pandas is a Python library that helps you import, organize and process data, it's familiar to "dataframes" in the R language.

Let's create a dataframe in Pandas, select data with Boolean indexing and finally plots using the same Pandas dataframe:


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8zzoty9ccxinr0ard7oy.png)

This is the data I'll be using:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uzg7ejls5rrsbsox4ul7.png)

To create a dataframe to store this data, I'm going to create a dummy data as dictionary with three attributes: 'year', 'attendees' and 'average age'.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k92xjukefcgpe7j8eklc.png)

And after executing the cell, the displayed dataframe should look like this one:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g0gq2rywo1vig5drc6z2.PNG)

I can assign this newly created dataframe to a variable called 'df' (the standard variable name for a dataframe in Pandas):

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x35qxmjx54i7rt1lzixf.png)

And the result should be the same:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h3eqxfi4de4kqmhfxl7o.PNG)

There are three columns in this dummy dataframe, you can select a single column out of this dataframe, for example:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/blqnsfp56k8jfc8kmglz.png)

The type of this new data is something called a "Pandas Series".

It's similar to a regular Python list and also to the NumPy array, if you're familiar with the NumPy library.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v9hpccabywt7bic05qey.png)

Knowing this, you can apply an inequality operation on the series with df['year'] < 2010:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y9818bv91ssyj2k23qyr.png)

This returns a series of Boolean values:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s116hrc8nzrqcup31e8l.PNG)

Let's store the output into a variable:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/abdglxvmakbkbja8qfd7.png)

Using the Boolean Series you can select only the part of the data where the year is earlier than 2010, this is called "Boolean Indexing":

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3j13p48jqslipehezejg.PNG)

Imagine that you want to examine how the number of attendees has changed for the last three events.

To best figure this out, you might want to plot the number of attendees against the year:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dyayaau8fr750soyzfuu.png)

This line of course puts the year on the x axis and the attendees on the y axis, and the result it's the following:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nja0rdxz20sv4laj8lo8.PNG)

If you want to plot the number of attendees and average age on the same plot we can just call 'plt.plot()' multiple times:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zu0v9pwlsivhx2mc6hsf.PNG)

Part 3: Importing Data with Pandas

For this example, the sample data that I'm going to use, is the following .csv file:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2y37h8oakijw6f4pcy9w.PNG)

It is a list of countries and their basic demographics for each year, years ranging from 1952 to 2007 for every five years.

To import this .csv file make sure that you ether know the path of the file or the both the notebook and the .csv file are located in the same location within the Jupyter intance.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jjz8ppw2hbnf9kjqbux0.png)

This dataset is pretty small, but in real world scenarios if you want to have a glimpse of the data you're dealing with, all you need to to is to use the 'head()' method:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dqfuk3whxs6f0qxu8yww.png)

This gives you the first five rows of the dataframe:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0dpejpoqmstbkp0pfxn8.PNG)

Now, if you would like to plot how GDP Per Capita has changed over time in Afghanistan.

To do that, it's necessary to isolate the data bout Afghanistan from the data variable.
To Select the country column you can write:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l17bop0u2h9vamoeqbhh.png)

or

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hp7gg861laaqa2jnlzq0.png)

Either syntax does exactly the same thing:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dangkj90seajab54p9v2.PNG)

Using this and Boolean Indexing you can select only the data about Afghanistan with:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4n6j3fk82qi56ka1i914.png)

And to plot it:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dx2w1bkzyp7992k30b2z.png)

And the final plot is the following:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/78bnv1ice4cm28cojfxs.PNG)