Wednesday, June 21, 2017

Plot some graph at once by matplotlib

How to do subplot


When we make machine learning model and check how accurate the predictions are, we frequently plot those.
Plotting some graphs at the same time is very useful to compare outcomes. By matplotlib, those can be done.


import matplotlib.pyplot as plt
import numpy as np
import math

x = np.arange(0, 100, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

plt.plot(x, y_sin)
plt.plot(x, y_cos)
The point is the fuction subplot().
subplot(121) means that the plot area has 1 row, 2 columns and the plot just after written by plt.plot() is the first plot.
The plot is like this.

plt.plot(x, y_sin)
plt.plot(x, y_cos)
In this case, plt.subplot(212) means taht the plot area has 2 rows, 1 column and the plot just after written is the second plot.
The plot is like this.

No comments:

Post a Comment