Method ① | Method ② |
---|---|
Draw the graph using the matplotlib.pyplot
module as you would when displaying a single graph.
demo.py
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0., 10., 0.1)
s = np.sin(x)
c = np.cos(x)
plt.subplot(211)
plt.plot(x, s)
plt.ylim(-3, 3)
plt.subplot(212)
plt.plot(x, c)
plt.ylim(-3, 3)
plt.show()
Create ʻAxes objects (ʻax1
, ʻax2) and draw a graph using the ʻAxes
objects.
demo2.py
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0., 10., 0.1)
s = np.sin(x)
c = np.cos(x)
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(x, s)
ax1.set_ylim(-3, 3)
ax2 = fig.add_subplot(212)
ax2.plot(x, c)
ax2.set_ylim(-3, 3)
plt.show()
The former is a simple method with a little less description, but it is not clear which graph you are currently working on. On the other hand, in the latter case, a ʻAxes` object is assigned to each graph, so it is clear which graph is being operated.
Recent matplotlib-related books often use the latter object-oriented method ("Introduction to Data Analysis with Python: Data Processing with NumPy and pandas". jp / books / 9784873116556 /) etc.).
Pyplot tutorial — Matplotlib 1.5.1 documentation http://matplotlib.org/users/pyplot_tutorial.html
Introduction to matplotlib-Apples are out http://bicycle1885.hatenablog.com/entry/2014/02/14/023734
Recommended Posts