Two ways to display multiple graphs in one image with matplotlib

demo

Method ① Method ②
demo.png demo2.png

Method ① Interactive 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()

Method ② Object-oriented method

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()

Differences between the two

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.).

reference

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

Two ways to display multiple graphs in one image with matplotlib
I want to display multiple images with matplotlib.
[Python] How to draw multiple graphs with Matplotlib
Animate multiple graphs with matplotlib
How to embed multiple embeds in one message with Discord.py
I tried two ways to combine multiple commits in Git
Easy to draw graphs with matplotlib
How to display legend marks in one with Python 2D plot
How to title multiple figures with matplotlib
How to suppress display error in matplotlib
Display Japanese graphs with VS Code + matplotlib
[Small story] How to save matplotlib graphs in a batch with Jupyter
Easily log in to AWS with multiple accounts
How to display images continuously with matplotlib Note
Multiple graphs are displayed in one window (python)
A confusing story with two ways to implement XGBoost in Python + overall notes
How to display in the entire window when setting the background image with tkinter
Convert the image in .zip to PDF with Python
How to display multiple images of galaxies in tiles
Python OpenCV tried to display the image in text.
API implementation to toggle values in two ways (go)
[Python] Send gmail with python: Send one by one with multiple image files attached
How to adapt multiple machine learning libraries in one shot
How to check ORM behavior in one file with django
[Ev3dev] How to display bmp image on LCD with python
Image analysis with Object Detection API to try in 1 hour
I wanted to delete multiple objects in s3 with boto3
Embed matplotlib graphs in Tkinter
Tweet with image in Python
2 ways to deal with SessionNotCreatedException
I tried to process the image in "sketch style" with OpenCV
I tried to process the image in "pencil style" with OpenCV
How to calculate "xx time" in one shot with Python timedelta
Inference & result display with Tensorflow + matplotlib
Connect to multiple databases with SQLAlchemy
How to convert 0.5 to 1056964608 in one shot
Two Ways to Make Ansible Portable
6 ways to string objects in Python
Heatmap with Dendrogram in Python + matplotlib
Convert PDF to image with ImageMagick
Compare two images with image hash
(Matplotlib) I want to draw a graph with a size specified in pixels
(Mac) How to display Japanese with Matplotlib and Seaborn At MacOS Sierra
How to draw a bar graph that summarizes multiple series with matplotlib
If you want to get multiple statistics with groupby in pandas v1
[Google Colab] I want to display multiple images side by side in tiles