[PYTHON] Make common settings with subplot of matplotlib

Thing you want to do

I want to make common settings such as grids and labels for all axes drawn with matplotlib subplot. Example1.png

Method 1

  1. Get a list of axes.
  2. Loop for each axis
  3. Change the current axis
# 1.Get a list of axes
axs = plt.gcf().get_axes()

# 2.Loop for each axis
for ax in axs:
    # 3.Change current axis
    plt.axes(ax)

Code example

import matplotlib.pyplot as plt
import numpy as np

#data
x  = np.linspace(-np.pi, np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# figure
plt.figure(figsize=(8,4))

#Plot 1
plt.subplot(1,2,1)
plt.plot(x,y1,label='sin')

#Plot 2
plt.subplot(1,2,2)
plt.plot(x,y2,label='cos')

#Get a list of axes
axs = plt.gcf().get_axes()

#Loop for each axis
for ax in axs:
    #Change current axis
    plt.axes(ax)

    #Show legend
    plt.legend(loc=2)

    #grid
    plt.grid(linestyle='--')

    #Axis label
    plt.xlabel('x')
    plt.ylabel('y')

    #Axis range
    plt.xlim([-np.pi, np.pi])
    plt.ylim([  -1.2,   1.2])

#Adjustment of figure
plt.tight_layout()

Method 2

  1. List data and labels.
  2. Loop using enumerate
  3. Don't forget to add +1 to plt.subplot
# 1.List data and labels.
Y    = [y1, y2]
lbls = ['sin', 'cos']

# 2.Loop with enumerate
for i, y in enumerate(Y):

    # 3. plt.subplot is+Don't forget to
    plt.subplot(1,2,i+1)

Code example 2

# figure
plt.figure(figsize=(8,4))
# list
Y    = [y1, y2]
lbls = ['sin', 'cos']

for i, y in enumerate(Y):
    #Subplot
    plt.subplot(1,2,i+1)
    
    #plot
    plt.plot(x,y,label=lbls[i])

    #Show legend
    plt.legend(loc=2)

    #grid
    plt.grid(linestyle='--')

    #Axis label
    plt.xlabel('x')
    plt.ylabel('y')

    #Axis range
    plt.xlim([-np.pi, np.pi])
    plt.ylim([  -1.2,   1.2])

#Adjustment of figure
plt.tight_layout()

The following supplementary information

gca, gcf You can get the current figure with plt.gcf () and the current axes with plt.gca (). You can also specify the current axis with plt.axes ().

Hierarchical structure

Like MATLAB, matplotlib's figure has a hierarchical structure as shown in the figure below. 図1.png

There are axes in the figure and lines in the axes. You can get a list of all the axes in the current figure with plt.gcf (). get_axes (). Besides, plt.gca (). get_lines () can get all the lines in the current axes.

Recommended Posts

Make common settings with subplot of matplotlib
Align the size of the colorbar with matplotlib
Make a partially zoomed figure with matplotlib
[Python] Let's make matplotlib compatible with Japanese
Construction of Ceph (Octopus) (common to settings)
Make API of switchbot thermo-hygrometer with Node-RED
[Python] limit axis of 3D graph with Matplotlib
Increase the font size of the graph with matplotlib
Animation with matplotlib
Japanese with matplotlib
Animation with matplotlib
Histogram with matplotlib
Animate with matplotlib
Add information to the bottom of the figure with Matplotlib
Change IP settings to ACL of conoha with python
Adjust the ratio of multiple figures with the matplotlib gridspec
Make Lambda Layers with Lambda
2-axis plot with Matplotlib
Make Yubaba with Discord.py
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
Heatmap with Python + matplotlib
Band graph with matplotlib
Learn with Cheminformatics Matplotlib
Real-time drawing with matplotlib
Various colorbars with Matplotlib
3D plot with matplotlib
Installation of matplotlib (Python 3.3.2)
Make slides with iPython
Adjust axes with matplotlib
Make a Linux version of OpenSiv3D with find_package a little easier
Perform isocurrent analysis of open channels with Python and matplotlib
Make a gif animation from a serial number file with matplotlib
Reformat the timeline of the pandas time series plot with matplotlib
Let's visualize the number of people infected with coronavirus with matplotlib
I wrote the basic operation of matplotlib with Jupyter Lab