Wednesday, June 21, 2017

Plot some graph at once by matplotlib

How to do subplot

Overview

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.



Code

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.subplot(121)
plt.plot(x, y_sin)
plt.subplot(122)
plt.plot(x, y_cos)
plt.show()
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.subplot(211)
plt.plot(x, y_sin)
plt.subplot(212)
plt.plot(x, y_cos)
plt.show()
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