0% found this document useful (0 votes)
7 views17 pages

Data Visualization with Matplotlib and Pandas

The document provides a comprehensive guide on data visualization using matplotlib and pandas, focusing on reading data from CSV files, plotting sales data, and analyzing customer purchase patterns. It includes examples of bar plots, histograms, boxplots, and correlation matrices, along with code snippets for each visualization type. Additionally, it offers resources for further reading on data analysis and visualization techniques.

Uploaded by

alastairmayer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views17 pages

Data Visualization with Matplotlib and Pandas

The document provides a comprehensive guide on data visualization using matplotlib and pandas, focusing on reading data from CSV files, plotting sales data, and analyzing customer purchase patterns. It includes examples of bar plots, histograms, boxplots, and correlation matrices, along with code snippets for each visualization type. Additionally, it offers resources for further reading on data analysis and visualization techniques.

Uploaded by

alastairmayer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Data Visualization

Part II

Plotting with matplotlib


and pandas

1
First, Read Data from CSV file
import pandas as pd
import numpy as np
import [Link] as plt
%matplotlib inline

sales =
pd.read_csv("[Link]
rippen/DataSets/master/[Link]",
parse_dates=['date'])
[Link]()
[Link]

[Link]()

sales['unit price'].describe() 2
Customers
customers = sales[['name','ext price','date']]
[Link]()

customer_group = [Link]('name')
customer_group.size()

sales_totals = customer_group.sum()
sales_totals.sort_values('ext price').head()

my_plot = sales_totals.plot(kind='bar')
my_plot = sales_totals.plot(kind='barh')

# identical
my_plot = sales_totals.[Link]()
3
Customers – Title and Labels
my_plot = sales_totals.sort_values('ext price',
ascending=False).plot(kind='bar', legend=None,
title="Total Sales by Customer")

my_plot.set_xlabel("Customers")

my_plot.set_ylabel("Sales ($)")

4
Customers with Product Category
customers = sales[['name', 'category', 'ext price',
'date']]
[Link]()
category_group =
[Link](['name','category']).sum()
category_group.head(10)
category_group = category_group.unstack()
category_group.head(10)
my_plot = category_group.plot(kind='bar', stacked=True,
title="Total Sales by Customer")
my_plot.set_xlabel("Customers")
my_plot.set_ylabel("Sales ($)")
my_plot.legend(["Belts","Shirts","Shoes"], loc='best',
ncol=3)
5
Customers with Product Category –
Sorted!
category_group = category_group.sort_values(('ext
price', 'Belt'), ascending=False)
category_group.head()
my_plot = category_group.plot(kind='bar', stacked=True,
title="Total Sales by Customer")

# sort by total without showing total!


category_group['total'] = category_group.sum(axis=1)
category_group = category_group.sort_values('total',
ascending=False)
category_group.head()
category_group.drop('total', axis=1, inplace=True)
my_plot = category_group.plot(kind='bar', stacked=True,
title="Total Sales by Customer")
6
Purchase Patterns
purchase_patterns = sales[['category','ext
price','date']]
purchase_patterns.head()

purchase_plot = purchase_patterns['ext
price'].hist(bins=20)

# done many times now,


# but should always be done to make figure self-
explanatory
purchase_plot.set_title("Purchase Patterns")
purchase_plot.set_xlabel("Order Amount ($)")
purchase_plot.set_ylabel("Number of Orders")

7
Purchase Patterns – Timeline
purchase_patterns = purchase_patterns.set_index('date')
purchase_patterns.head()

# sorted by time
purchase_patterns.sort_index()

# resampled by months
purchase_plot =
purchase_patterns.resample('M').sum().plot(title="Total
Sales by Month", legend=None)

# save the figure


fig = purchase_plot.get_figure()
[Link]("[Link]")
8
Purchase Patterns – Timeline
by Categories
fig, ax = [Link]()

# key gives the group name (i.e., category), data gives


the actual values
for key, data in purchase_patterns.groupby('category'):
[Link]('M').sum().plot(y='ext price', ax=ax,
label=key)

# change y range to start from 0 with matplotlib


ax.set_ylim(bottom=0)

9
Boxplots …
# Box and Whisker Plots
[Link](figsize=(14,10)) # Not very useful!

# Four boxplots in one figure with individual scales


[Link](figsize=(14,10), kind='box', subplots=True,
layout=(2,2), sharex=False, sharey=False)
# This was not working for some time in some pandas
versions because of the date column, so:
[Link]('date',axis=1).plot(figsize=(14,10),
kind='box', subplots=True, layout=(2,2), sharex=False,
sharey=False)

# Individual boxplots for all names


[Link](column="ext price", by="name")
[Link](rotation='vertical')
10
… and Histograms
# Histograms
[Link](figsize=(14,10))

# for individual variables in one plot


[Link]('date',axis=1).plot(figsize=(14,10),
kind='hist', subplots=True, layout=(2,2), sharex=False,
sharey=False) # "ignored", unfortunately

# individual plots for individual customers


[Link](column="ext price", by="name", bins=30)

# same axes for comparison


[Link](column="ext price", by="name", bins=30,
sharex=True, sharey=True)
11
First, Read Data from CSV file
import numpy as np
import [Link] as plt

# Load CSV using pandas


import pandas as pd
from pandas import read_csv
# AirBnB website visitors
filename = '[Link]'
visitors = read_csv(filename, index_col='id_visitor')
print([Link]())

print([Link])
print([Link]())
print([Link])
12
Histograms, Density Plots, Box and
Whisker Plots
# Univariate Histograms
[Link]()

# Univariate Density Plots


[Link](kind='density', subplots=True,
layout=(2,2), sharex=False)

# Box and Whisker Plots


[Link](kind='box', subplots=True, layout=(2,2),
sharex=False, sharey=False)

13
Correlation Matrix Plot
# correlation matrix
correlations = [Link]()
# plot correlation matrix (generic)
fig = [Link]()
ax = fig.add_subplot(111)
cax = [Link](correlations, vmin=-1, vmax=1)
[Link](cax)

# change the tick labels


ticks = [Link](0,4,1)
ax.set_xticks(ticks)
ax.set_yticks(ticks)
ax.set_xticklabels([Link])
ax.set_yticklabels([Link])
14
Scatter Plot Matrix
# Scatterplot Matrix
from [Link] import scatter_matrix
scatter_matrix(visitors)

15
Additional Readings
 Python for Data Analysis: Data Wrangling with Pandas, NumPy, and
IPython by Wes McKinney (pub. yr. 2017). Chapter 9 and 10.
 Machine Learning Mastery with Python by Jason Brownlee (pub. yr.
2017). Chapter 6.
 [Link]
[Link]
 [Link]
 [Link]
ython-data-manipulation/
 [Link]
-over-time
 [Link]
n/use-time-series-data-in-python/date-time-types-in-pandas-python/res
ample-time-series-data-pandas-python/
 [Link]
from-0-with-matplotlib
16
Additional Readings (cont'd)
 [Link]
 [Link]
 [Link]
[Link]
 [Link]
html
 [Link]
 [Link]
[Link]

 DataCamp:
– Course: Intermediate Python for Data Science
» Chapter: Matplotlib
– Introduction to Data Visualization with Python
» Chapter: Customizing Plots

17

You might also like