[Python] How to draw multiple graphs with Matplotlib

Display two graphs on the left and right.


import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(-np.pi, np.pi, 1000)

x1 = np.sin(2*t)
x2 = np.cos(2*t)

fig, (axL, axR) = plt.subplots(ncols=2, figsize=(10,4))

axL.plot(t, x1, linewidth=2)
axL.set_title('sin')
axL.set_xlabel('t')
axL.set_ylabel('x')
axL.set_xlim(-np.pi, np.pi)
axL.grid(True)

axR.plot(t, x2, linewidth=2)
axR.set_title('cos')
axR.set_xlabel('t')
axR.set_ylabel('x')
axR.set_xlim(-np.pi, np.pi)
axR.grid(True)

fig.show()

multiplot_01.png

Axis Share

When displaying multiple graphs, Share can be used when it is troublesome to set the X-axis settings for all graphs. Just set sharex = True as an argument to subplots. Ai

share available

import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(-np.pi*2, np.pi*2, 1000)

x1 = np.sin(2*t)
x2 = np.cos(2*t)

fig, (axL, axR) = plt.subplots(ncols=2, figsize=(10,4), sharex=True)

axL.plot(t, x1, linewidth=2)
axL.set_title('sin')
axL.set_xlabel('t')
axL.set_ylabel('x')
axL.set_xlim(-np.pi, np.pi)
axL.grid(True)

axR.plot(t, x2, linewidth=2)
axR.set_title('cos')
axR.set_xlabel('t')
axR.set_ylabel('x')
axR.grid(True)

fig.show()

20160220_g1.png

no share

20160220_g2.png

Make it a little complicated.

If you want to display two graphs normally in the first column and use all the graphs in the second column to display a long graph, you can use subplot2grid as shown below.

import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(-np.pi, np.pi, 1000)

x1 = np.sin(2*t)
x2 = np.cos(2*t)
x3 = x1 + x2

fig = plt.figure(figsize=(10,8))
ax1 = plt.subplot2grid((2,2), (0,0))
ax2 = plt.subplot2grid((2,2), (0,1))
ax3 = plt.subplot2grid((2,2), (1,0), colspan=2)

ax1.plot(t, x1, linewidth=2)
ax1.set_title('sin')
ax1.set_xlabel('t')
ax1.set_ylabel('x')
ax1.set_xlim(-np.pi, np.pi)
ax1.grid(True)

ax2.plot(t, x2, linewidth=2)
ax2.set_title('cos')
ax2.set_xlabel('t')
ax2.set_ylabel('x')
ax2.set_xlim(-np.pi, np.pi)
ax2.grid(True)

ax3.plot(t, x3, linewidth=2)
ax3.set_title('sin+cos')
ax3.set_xlabel('t')
ax3.set_ylabel('x')
ax3.set_xlim(-np.pi, np.pi)
ax3.grid(True)

fig.show()

multiplot_02.png

Another option is to use matplotlib.gridspec. In this case, specify the used part of the 2D array obtained by gridspec.Gridspec (xxx, yyy) and use it as an argument of subplot. For example, if you want to use the top left

gs = gridspec.GridSpec(2,2)
ax1 = plt.subplot(gs[0,0])

Will be.

Also, if you want to use the entire first row,

ax2 = plt.subplot(gs[1,:]) 

Will be.

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

t = np.linspace(-np.pi, np.pi, 1000)

x1 = np.sin(2*t)
x2 = np.cos(2*t)
x3 = x1 + x2

fig = plt.figure(figsize=(10,8))
gs = gridspec.GridSpec(2,2)

ax1 = plt.subplot(gs[0,0])
ax2 = plt.subplot(gs[0,1])
ax3 = plt.subplot(gs[1,:])

ax1.plot(t, x1, linewidth=2)
ax1.set_title('sin')
ax1.set_xlabel('t')
ax1.set_ylabel('x')
ax1.set_xlim(-np.pi, np.pi)
ax1.grid(True)

ax2.plot(t, x2, linewidth=2)
ax2.set_title('cos')
ax2.set_xlabel('t')
ax2.set_ylabel('x')
ax2.set_xlim(-np.pi, np.pi)
ax2.grid(True)

ax3.plot(t, x3, linewidth=2)
ax3.set_title('sin+cos')
ax3.set_xlabel('t')
ax3.set_ylabel('x')
ax3.set_xlim(-np.pi, np.pi)
ax3.grid(True)

fig.show()

Make a blank with subplots

If there are few graphs for the frame created by subplots, axis ('off') is applied to the area that you do not want to draw.

import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(-np.pi, np.pi, 1000)


x1 = np.sin(2*t)
x2 = np.cos(2*t)
x3 = x1 + x2

fig,axes = plt.subplots(nrows=2,ncols=2,figsize=(10,8))


axes[0,0].plot(t, x1, linewidth=2)
axes[0,0].set_title('sin')
axes[0,0].set_xlabel('t')
axes[0,0].set_ylabel('x')
axes[0,0].set_xlim(-np.pi, np.pi)
axes[0,0].grid(True)

axes[0,1].plot(t, x2, linewidth=2)
axes[0,1].set_title('cos')
axes[0,1].set_xlabel('t')
axes[0,1].set_ylabel('x')
axes[0,1].set_xlim(-np.pi, np.pi)
axes[0,1].grid(True)

axes[1,0].plot(t, x3, linewidth=2)
axes[1,0].set_title('sin+cos')
axes[1,0].set_xlabel('t')
axes[1,0].set_ylabel('x')
axes[1,0].set_xlim(-np.pi, np.pi)
axes[1,0].grid(True)

axes[1,1].axis('off')

download (67).png

I referred to the following http://matplotlib.org/examples/pylab_examples/subplots_demo.html http://matplotlib.org/users/recipes.html

Recommended Posts

[Python] How to draw multiple graphs with Matplotlib
Easy to draw graphs with matplotlib
[Python] How to draw a scatter plot with Matplotlib
How to title multiple figures with matplotlib
[Python] How to draw a histogram in Matplotlib
Animate multiple graphs with matplotlib
How to draw a bar graph that summarizes multiple series with matplotlib
[Python] How to create a 2D histogram with Matplotlib
How to get started with Python
How to use FTP with Python
How to calculate date with python
Draw Lyapunov Fractal with Python, matplotlib
How to draw a graph using Matplotlib
How to work with BigQuery in Python
How to do portmanteau test with python
How to display python Japanese with lolipop
How to enter Japanese with Python curses
[Python] How to deal with module errors
How to install python3 with docker centos
Two ways to display multiple graphs in one image with matplotlib
How to assign multiple values to the Matplotlib colorbar
How to upload with Heroku, Flask, Python, Git (4)
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
[Small story] How to save matplotlib graphs in a batch with Jupyter
How to do multi-core parallel processing with python
How to install Python
How to draw a 2-axis graph with pyplot
Draw multiple graphs using matplotlib figures and axes
Try to draw a life curve with python
I want to display multiple images with matplotlib.
[Python] How to read excel file with pandas
How to crop an image with Python + OpenCV
How to install python
Draw graphs in Julia ... Leave the graphs to Python
Heatmap with Python + matplotlib
How to specify attributes with Mock of python
How to draw a vertical line on a heatmap drawn with Python seaborn
How to use Matplotlib
How to measure execution time with Python Part 1
How to use tkinter with python in pyenv
How to display images continuously with matplotlib Note
[Python] How to handle Japanese characters with openCV
[Python] Mention to multiple people with Slack API
[Python] How to compare datetime with timezone added
How to measure execution time with Python Part 2
How to return multiple indexes with index method
[Python] Customize Colormap when drawing graphs with matplotlib
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
How to convert / restore a string with [] in python
I tried to summarize how to use matplotlib of python
How to write string concatenation in multiple lines in Python
How to scrape image data from flickr with python
How to do hash calculation with salt in Python
[Introduction to Python] How to iterate with the range function?
I tried to draw a route map with Python
How to upload with Heroku, Flask, Python, Git (Part 3)
How to run tests in bulk with Python unittest
[Python] How to specify the download location with youtube-dl
How to measure mp3 file playback time with python
How to use python interactive mode with git bash