Data Visualisation with Python (Matplotlib)

Data Visualisation with Python (Matplotlib)

by Dirisu Jesse

Importing required Packages

import random
import matplotlib.pyplot as plt

Customising graph styles

from matplotlib import style
style.use('bmh')

A simple graph

plt.plot([1,2,3,4,5], [6,7,8,9,10], 'c', label='First Plot')
plt.show()
output_6_0

Labeling Graphs

x = [6,7,8,9,10]
y = [2,4,1,5,3]
y2 = [4,2,5,3,1]
plt.plot(x, y, 'c', label='First Plot')
plt.plot(x, y2, 'm--', label='Second Plot')
plt.xlabel('Plot Label')
plt.ylabel('Important Var')
plt.title('Nice Graph')
plt.legend(loc='best')
plt.show()
output_8_0

Barcharts and Histograms

x = [2,4,6,8,10]
y = [6,4,9,3,7]

plt.bar(x, y, color='#ccd231', label='First Plot')
plt.bar(x, y2, color='#265fec', label='Second Plot')
plt.ylim(0, 9.2)
plt.xlim(1.8, 11)
plt.xlabel('Plot Label')
plt.ylabel('Important Var')
plt.title('Nice Graph')
plt.legend(loc='best')
plt.show()

output_10_0

population_ages = []

for i in range(130):
    rand = random.randint(1, 130)
    population_ages.append(rand)
print(population_ages)
[118, 126, 84, 84, 9, 61, 35, 129, 50, 126, 45, 61, 108, 33, 32, 48, 55, 37, 79,
91, 14, 58, 72, 92, 60, 72, 15, 73, 127, 107, 127, 95, 53, 9, 21, 94, 72, 24, 90,
30, 17, 107, 12, 7, 87, 98, 130, 114, 21, 75, 115, 67, 72, 101, 54, 111, 54, 120,
6, 55, 89, 36, 106, 69, 93, 127, 75, 111, 73, 106, 1, 113, 46, 41, 125, 59, 16, 
66, 14, 128, 38, 7, 32, 18, 89, 20, 92, 63, 111, 47, 54, 125, 124, 99, 14, 105, 
11, 46, 95, 20, 16, 95, 9, 10, 98, 81, 44, 111, 93, 44, 4, 17, 58, 18, 25, 121, 
52, 85, 70, 82, 114, 21, 20, 61, 24, 24, 128, 11, 76, 22]
bins = [x for x in range(0, 131, 10)]

plt.hist(population_ages, bins, histtype='bar', color='c', label='First Plot', rwidth=0.9)
plt.xlabel('Plot Label')
plt.ylabel('Important Var')
plt.title('Nice Graph')
plt.legend(loc='best')
plt.show()

output_12_0

Scatter Plots

l = [(i ** 2) for i in bins]
j = [(i // 2) for i in l]

plt.scatter(bins, l, color='c', label='First Plot', s=100)
plt.scatter(bins, j, color='m', label='Second Plot', s=50, marker='x')
plt.xlabel('Plot Label')
plt.ylabel('Important Var')
plt.title('Nice Graph')
plt.legend(loc='best')
plt.show()

output_15_0

Stack Plots

days = [i for i in range(1, 6)]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]

plt.stackplot(days, sleeping, eating, working, playing, colors=['c', 'm', 'r', 'y'], labels=['Sleep', 'Eat', 'Work', 'Play'])
plt.xlabel('Plot Label')
plt.ylabel('Important Var')
plt.title('Nice Graph')
plt.legend(loc='best')
plt.show()

output_17_0

Pie Charts

days = [i for i in range(1, 6)]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]

slices = [7,2,2,13]
activities = ['Sleeping', 'Eating', 'Working', 'Playing']

plt.pie(slices, labels=activities, colors=['c', 'm', 'r', 'b'], startangle=90, 
        shadow=True, explode=(0, 0.1, 0.05,0.15), autopct="%1.1f%%")
plt.title('Nice Graph')
plt.show()

output_19_0

Loading Data from CSV Files

import csv

plot = [[],[]]

f = open('us_marriages_divorces.csv', 'r')
for i in csv.reader(f):
    if i[0].isdigit() and i[2].isdigit():
        plot[0].append(int(i[0]))
        plot[1].append(int(i[2]))

plt.plot(plot[0], plot[1], 'g', label="Divorce Rates in America Over the years")
plt.xlabel('Years')
plt.ylabel('Divorces')
plt.title('Divorce Rate Graph')
plt.legend(loc='best')
plt.show()

output_21_0

Extras a peek into Pandas

import pandas as pd

pl = pd.read_csv('example.csv')
pl.T.plot()
plt.legend().remove()
plt.show()

output_22_0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s