Data Visualization with Python pt. ii
Published: 2021-01-16 18:37:47

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

The notebook of this lecture is in my GitHub repo:

>>> https://github.com/hugoestradas/Data_Visualisation_with_Python.git

Part 1: What on earth are "Histograms" <?>

Suppose you're in charge that a website always load fast and one day the average page loading time in... lets say June is significantly slower than the previous 5 months.

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

This type of scenarios are where the histograms really shine, because they show a kind of history in their graphs.

Histograms helps you understand the distribution of a numeric value in a way that cannot with mean or median alone.

Part 2: Histograms with Matplotlib

Let's import Pandas and Matplotlib:

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

For this example I'm going to be using a larger dataset called "dataii.csv", let's import it:

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

For this part, I'll create histograms using the 'subplot()' function.

To check which continents are included within the data I'll use the 'set()' function:

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

And this returns the following output, showing all the continents grouped:

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

Now, for example, if you need to select the data of Asia and Europe in 2007, first you need to select the data for 2007:

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

Then select the data for Asia out of the 'data2007' variable, and then the same procedure for Europe:

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

Check both 'asia2007' and 'europe2007' with the 'head()' function:

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

To check how many countries are in these two newly created datasets let's use the 'set()' function:

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

If you don't want to see the complete list of countries, instead only the number of countries for reach data set, use the 'len()' function combined with the 'set()' function:

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

Use this combined with the 'print()' function for both datasets, ant this should be the output:

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

Let's now find the mean and median of GDP per Capita in Asia and Europe in 2007:

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

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

To create a histogram of GDP per capita in Asia, type:

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

Now, to compare this histogram of the GDP Per Capita of Asia with the GDP Per Capita of Europe, both of 2007, lets use the 'suplot()' function:

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

And the result is the following histogram:

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

Part 3: Comparing Complex Histograms

Now, let's compare Europe and America's life expectancy in 1997.

There are many ways to solve this problem, but my approach is the following:

First select only the data of 1997:

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

Then, from newly created dataset ('data97') extract America's and Europe's data:

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

Now, to check the number of countries in each new dataset:

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

Now to get the mean and median life expectancy of each new data set:

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

Now, finally to compare both datasets in histogram:

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

Being the final chart the following:

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