Data Scientist
Visualization with Matplotlib
Templates for plotting figures quickly
A picture is worth a thousand words.
Matplotlib is the most widely used Python library for various visualizations: static, dynamic, or animated. Today, we will learn how to plot data using this library in Python.
How to install matplotlib in Python?
Let us see how to install matplotlib
using Python installer in various operating systems.
- Windows
pip install -U matplotlib
- Linux
sudo apt-get install python3-matplotlib
- Mac
sudo pip3 install matplotlib
To plot your data visualization: let only copy first, edit after.
Histogram
Histograms are one of the heavily used plots across functions as it gives a very good view of the distribution of data.
from numpy import random
from matplotlib import pyplot as plt
random.seed(0)
meu = 50
sig = 0.30
data = meu + sig * random.randn(1000)
plt.hist(data, bins=20, facecolor="#08D9D6", alpha=0.30, rwidth=0.7)
plt.subplots_adjust(left=0.30)
plt.show()
Line
Single Line
from matplotlib import pyplot as plt
price = [1, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2]
quantity = [800, 700, 600, 550, 500, 460, 420]
plt.plot(price, quantity, marker='*', color='#FF2E63')
plt.xlabel('price')
plt.ylabel('quantity')
plt.show()
Several Lines
How do we add multiple lines in the same chart? Let us take it a bit further to plot multiple lines in the same chart.
from matplotlib import pyplot as plt
price = [1, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2]
demand = [800, 700, 600, 550, 500, 460, 420]
supply = [500, 550, 600, 640, 680, 700, 720]
plt . plot(price, demand, marker='*', color='#FF2E63', label='demand')
plt . plot(price, supply, marker='*', color='#08D9D6', label='supply')
plt.xlabel('price')
plt.ylabel('quantity')
plt.legend()
plt.show()
We created a multiple line plot by calling 2plot functions.
Bar Plot
Bar plots are one of the most popular charts used in any type of visualization as it shows the relative data points in an emphasized manner like a line chart.
Bar Plot
from matplotlib import pyplot as plt
classes = [ 'class1' , 'class2' , 'class3' ]
train = [ 1000 , 700 , 300 ]
plt . bar ( classes , train , width = 0.3 , align = 'center' )
plt . title ( 'Training set' )
plt.show()
Stacked Bar Plot
from numpy import random
from matplotlib import pyplot as plt
random . seed ( 0 )
classes = [ 'class1' , 'class2' , 'class3' ]
train = [ 1000 , 700 , 300 ]
test = [ 100 , 70 , 30 ]
plt . bar ( classes , train , width = 0.3 , align = 'center', color='#08D9D6' )
plt . bar ( classes , test , width = 0.3 , align = 'center' , bottom = train, color='#FF2E63')
plt . title ( 'Training and test set' )
plt.show()
Scatter Plot
from numpy import random
import matplotlib.pyplot as plt
random.seed(1968)
number = 20
x = random.rand(number)
y = random.rand(number)
area = (40 * random.rand(number))**2.5
plt.scatter(x, y, c='#FF2E63', alpha=0.8)
plt.show()
Conclusion
Easy, right? Let’s choose the right chart. This will help your presentation stick in the audience’s minds.