0% found this document useful (0 votes)
20 views37 pages

Intreoduction To Python Basic Plots With Matplolib

The document discusses creating basic plots using Matplotlib in Python including line plots, scatter plots, histograms, and customizing plots with labels, titles, ticks and adding additional data points.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views37 pages

Intreoduction To Python Basic Plots With Matplolib

The document discusses creating basic plots using Matplotlib in Python including line plots, scatter plots, histograms, and customizing plots with labels, titles, ticks and adding additional data points.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Basic plots with

Matplotlib
I N TE R M E D I ATE P Y TH O N
Basic plots with Matplotlib
Visualization Data Structure

Control Structures Case Study


Data visualization
Very important in Data Analysis
Explore data

Report insights
1 Source: GapMinder, Wealth and Health of Nations
1 Source: GapMinder, Wealth and Health of Nations
1 Source: GapMinder, Wealth and Health of Nations
Matplotlib
import matplotlib.pyplot as plt
year = [1950, 1970, 1990, 2010]

pop = [2.519, 3.692, 5.263, 6.972]


plt.plot(year, pop)
plt.show()
Matplotlib
Matplotlib
Scatter plot
import matplotlib.pyplot as plt
year = [1950, 1970, 1990, 2010]
pop = [2.519, 3.692, 5.263, 6.972]
plt.plot(year, pop)
plt.show()
Scatter plot
import matplotlib.pyplot as plt
year = [1950, 1970, 1990, 2010]
pop = [2.519, 3.692, 5.263, 6.972]
plt.scatter(year, pop)
plt.show()
Histogram
I N TE R M E D I ATE P Y TH O N
Histogram
Explore dataset

Get idea about distribution


Histogram
Explore dataset

Get idea about distribution


Histogram
Explore dataset

Get idea about distribution


Histogram
Explore dataset

Get idea about distribution


Histogram
Explore dataset

Get idea about distribution


Histogram
Explore dataset

Get idea about distribution


Histogram
Explore dataset

Get idea about distribution


Matplotlib
import matplotlib.pyplot as plt

help(plt.hist)

Help on function hist in module matplotlib.pyplot:


hist(x, bins=None, range=None, density=False, weights=None,
cumulative=False, bottom=None, histtype='bar', align='mid',
orientation='vertical', rwidth=None, log=False, color=None,
label=None, stacked=False, *, data=None, **kwargs)
Plot a histogram.

Compute and draw the histogram of *x*. The return value is a


tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...],
*bins*, [*patches0*, *patches1*, ...]) if the input contains
multiple data.
Matplotlib example
values = [0,0.6,1.4,1.6,2.2,2.5,2.6,3.2,3.5,3.9,4.2,6]
plt.hist(values, bins=3)
plt.show()
Population py ramid
Population py ramid
Customization
I N TE R M E D I ATE P Y TH O N
Data visualization
Many options
Di erent plot types
Many customizations

Choice depends on
Data

Story you want to tell


Basic plot
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

plt.plot(year, pop)

plt.show()
Axis labels
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

plt.plot(year, pop)

plt.xlabel('Year')
plt.ylabel('Population')

plt.show()
Axis labels
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

plt.plot(year, pop)

plt.xlabel('Year')
plt.ylabel('Population')

plt.show()
Title
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

plt.plot(year, pop)

plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population Projections')

plt.show()
Title
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

plt.plot(year, pop)

plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population Projections')

plt.show()
Ticks
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

plt.plot(year, pop)

plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population Projections')
plt.yticks([0, 2, 4, 6, 8, 10])

plt.show()
Ticks
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

plt.plot(year, pop)

plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population Projections')
plt.yticks([0, 2, 4, 6, 8, 10])

plt.show()
Ticks (2)
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

plt.plot(year, pop)

plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population Projections')
plt.yticks([0, 2, 4, 6, 8, 10],

['0', '2B', '4B', '6B', '8B', '10B'])

plt.show()
Ticks (2)
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

plt.plot(year, pop)

plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population Projections')
plt.yticks([0, 2, 4, 6, 8, 10],

['0', '2B', '4B', '6B', '8B', '10B'])

plt.show()
Add historical data
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

# Add more data


year = [1800, 1850, 1900] + year
pop = [1.0, 1.262, 1.650] + pop

plt.plot(year, pop)

plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population Projections')
plt.yticks([0, 2, 4, 6, 8, 10],

['0', '2B', '4B', '6B', '8B', '10B'])

plt.show()
Add historical data
population.py

import matplotlib.pyplot as plt


year = [1950, 1951, 1952, ..., 2100]
pop = [2.538, 2.57, 2.62, ..., 10.85]

# Add more data


year = [1800, 1850, 1900] + year
pop = [1.0, 1.262, 1.650] + pop

plt.plot(year, pop)

plt.xlabel('Year')
plt.ylabel('Population')
plt.title('World Population Projections')
plt.yticks([0, 2, 4, 6, 8, 10],

['0', '2B', '4B', '6B', '8B', '10B'])

plt.show()
Before vs. after

You might also like