Matplotlib Basics
Matplotlib Basics
Matplotlib Basics
Here we cover the minimum basics of matplotlib functionality, just enough to understand how Pandas plotting
and Seaborn are built on top of Matplotlib. We will mainly use Pandas or Seaborn plotting throughout the
course, here we show just the basic interactions possible with matplotlib. Do not consider this a
comprehensive guide! For more information on matplotlib, visit: https://matplotlib.org/tutorials/index.html
(https://matplotlib.org/tutorials/index.html)
In [4]:
import numpy as np
import pandas as pd
Visualizing Plots
Review video to understand this section!
In [5]:
In [6]:
x = [0,1,2]
y = [100,200,300]
In [8]:
plt.plot(x,y)
Out[8]:
[<matplotlib.lines.Line2D at 0x169e481f4c8>]
localhost:8888/notebooks/Documents/Pelatihan/ABCD/My Works/Day 3 Pandas and Data Visualization/Sesi 3-3A Matplotlib Basics.ipynb 1/7
11/22/22, 1:51 PM Sesi 3-3A Matplotlib Basics - Jupyter Notebook
In [9]:
In [10]:
# When running a .py file , you need to add plt.show() at the end of your commands
In [11]:
Basic Tools
We will only use pure matplotlib for really quick,basic plots.
In [12]:
housing = pd.DataFrame({'rooms':[1,1,2,2,2,3,3,3],
'price':[100,120,190,200,230,310,330,305]})
localhost:8888/notebooks/Documents/Pelatihan/ABCD/My Works/Day 3 Pandas and Data Visualization/Sesi 3-3A Matplotlib Basics.ipynb 2/7
11/22/22, 1:51 PM Sesi 3-3A Matplotlib Basics - Jupyter Notebook
In [13]:
housing
Out[13]:
rooms price
0 1 100
1 1 120
2 2 190
3 2 200
4 2 230
5 3 310
6 3 330
7 3 305
In [16]:
In [17]:
plt.scatter(housing['rooms'],housing['price'])
Out[17]:
<matplotlib.collections.PathCollection at 0x169e4a5eec8>
Style Calls
One of the main reasons to learn the absolute basics is to see how the style interactions effect the API.
localhost:8888/notebooks/Documents/Pelatihan/ABCD/My Works/Day 3 Pandas and Data Visualization/Sesi 3-3A Matplotlib Basics.ipynb 3/7
11/22/22, 1:51 PM Sesi 3-3A Matplotlib Basics - Jupyter Notebook
In [18]:
plt.plot(x,y)
Out[18]:
[<matplotlib.lines.Line2D at 0x169e4ac9888>]
In [21]:
plt.plot(x,y)
plt.title('Title')
plt.xlabel('X Label')
plt.ylabel('Y Label');
localhost:8888/notebooks/Documents/Pelatihan/ABCD/My Works/Day 3 Pandas and Data Visualization/Sesi 3-3A Matplotlib Basics.ipynb 4/7
11/22/22, 1:51 PM Sesi 3-3A Matplotlib Basics - Jupyter Notebook
In [22]:
plt.plot(x,y)
# Labeling
plt.title('Title')
plt.xlabel('X Label')
plt.ylabel('Y Label');
localhost:8888/notebooks/Documents/Pelatihan/ABCD/My Works/Day 3 Pandas and Data Visualization/Sesi 3-3A Matplotlib Basics.ipynb 5/7
11/22/22, 1:51 PM Sesi 3-3A Matplotlib Basics - Jupyter Notebook
In [24]:
plt.plot(x,y,color='red')
# Labeling
plt.title('Title')
plt.xlabel('X Label')
plt.ylabel('Y Label');
localhost:8888/notebooks/Documents/Pelatihan/ABCD/My Works/Day 3 Pandas and Data Visualization/Sesi 3-3A Matplotlib Basics.ipynb 6/7
11/22/22, 1:51 PM Sesi 3-3A Matplotlib Basics - Jupyter Notebook
In [33]:
plt.plot(x,y,color='red',marker='o',markersize=20,linestyle='--')
# Labeling
plt.title('Title')
plt.xlabel('X Label')
plt.ylabel('Y Label');
That is all we will cover for matplotlib, the rest will be more intuitive with our use of pandas and seaborn!
localhost:8888/notebooks/Documents/Pelatihan/ABCD/My Works/Day 3 Pandas and Data Visualization/Sesi 3-3A Matplotlib Basics.ipynb 7/7