Applied Machine Learning For Engineers: Data Visualization With Matplotlib
Applied Machine Learning For Engineers: Data Visualization With Matplotlib
FS 2020 - B. Vennemann
What is Matplotlib
A Matlab-style plotting library for Python
Can produce publication-ready figures
Can be run in Python scripts, Python shells, Ipython shells, Jupyter notebooks, ...
In [1]:
Use Jupyter Notebook line magic for plotting inside the notebook
In [2]:
%matplotlib inline
In [3]:
import numpy as np
In [4]:
x = np.linspace(0, 5, 50)
In [5]:
In [6]:
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.plot(x, x, label='linear curve')
plt.legend()
plt.title('first subplot')
plt.subplot(1, 2, 2)
plt.plot(x, x**2, label='quadratic curve')
plt.legend()
plt.title('second subplot')
plt.show()
Scatter plots
In [8]:
x = np.random.random(20)
y = np.random.random(20)
plt.scatter(x, y)
plt.show()
In [9]:
In [10]:
plt.bar(category_names, values)
plt.show()
In [11]:
x = np.random.randn(10000)
plt.figure(figsize=(8, 5))
plt.hist(x, bins=50)
plt.text(-3, 500, 'some text')
plt.annotate('normal distribution', xy=(1, 450), xytext=(1.8, 550),
arrowprops=dict(facecolor='black', width=2))
plt.show()
Further reading
More info at https://matplotlib.org/contents.html (https://matplotlib.org/contents.html)