SoFunction
Updated on 2024-11-15

matplotlib shared axis implementation (X or Y axis)

preamble

1. General

Shared axes are shared x-axis or y-axis between several sub-diagrams, this section focuses on understanding how to share axes when plotting with matplotlib.

(nrows = 1,ncols = 1,sharex = False,sharey = False,
squeeze = True,subplot_kw =not have,gridspec_kw =not have,** fig_kw )

Parameters:
nrows: number of rows
ncols: number of columns
sharex: whether to share the X-axis coordinates or not
sharey: if or not share the Y coordinate.
Return value: Figure, array of Axes objects

I. Sharex and shareyCode Example:

'''
1. Purpose of the program:
   Based on sharex and sharey to implement
     (1) Share x-axis
     (2) Share y-axis
     (3) Share both x-axis and y-axis at the same time
     (4) Adjusting the distance between subplots

2. Versions
   2.1 Qingdao, Shandong May 18, 2021 Version 1

'''

# 1. Relevant module import
import numpy as np
import  as plt

['-serif'] = ['SimHei'] # Normal display of Chinese fonts
['axes.unicode_minus'] = False  # Normal display of the minus sign

# 2. Creation of cartographic data
x = (-5,5,100)
y_1 = (x)
y_2 = (x)
y_3 = y_2*2

# 3. mapping
  # 3.1 Shared X-axis
figure,(ax1,ax2,ax3) = (3,1,
                                    figsize=(5,6),
                                    dpi=600,
                                    # Shared x-axis
                                    sharex=True)

(x,y_1,c='blue',linestyle=':')
(x,y_2,c='orange',linestyle=':')
(x,y_3,c='r',linestyle=':')

    # Adjust the vertical distance between subgraphs
figure.subplots_adjust(hspace=0.1) 

ax1.set_title('The following three graphs share the x-axis')  # It actually makes more sense to add the name of the diagram when ()

  # 3.2 Shared Y-axis
    # Create new drawing figure and axes objects
figure,(ax1,ax2,ax3) = (1,3,
                                    figsize=(6,2),
                                    dpi=600,
                                    # Shared y-axis
                                    sharey=True)
('The following three graphs share the y-axis')
(x,y_1,c='blue',linestyle=':')
(x,y_2,c='orange',linestyle=':')
(x,y_3,c='r',linestyle=':')
    # Adjust the horizontal distance between subgraphs
figure.subplots_adjust(wspace=0.1) 

  # 3.3 Sharing both x- and y-axis at the same time
    # Create new drawing figure and axes objects
figure,(ax1,ax2,ax3) = (1,3,
                                    figsize=(6,2),
                                    dpi=600,
                                    # Shared x-axis
                                    sharex=True,
                                    # Shared y-axis
                                    sharey=True)

x4 = (-10,10,100)
y_4 = (x4)*2

('The following three graphs share both the X and Y axes')
(x,y_1,c='blue',linestyle=':')
(x,y_2,c='orange',linestyle=':')
(x4,y_4,c='r',linestyle=':')

    # Adjust the horizontal distance between subgraphs
figure.subplots_adjust(wspace=0.1) 

()

Mapping results:

共享x轴

共享y轴

同时共享x轴和y轴

Example 2

import  as plt
import numpy as np
['-serif']=['SimHei']
['axes.unicode_minus']=False
x=(0,2*,500)
y=(x)*(-x)
fig,ax=(nrows=1,ncols=2,sharey=True)
ax1=ax[0]
(x,y)
ax1.set_title("Line graphs.")
 
ax2=ax[1]
(x,y)
ax2.set_title("Scatterplot.")
("Two subplots of a canvas with shared y-coordinates.")
# Delete the gap wspace is the horizontal distance between the two graphs, hspace is the vertical distance between the two graphs
fig.subplots_adjust(wspace=0)
()

import  as plt
import numpy as np
['-serif']=['SimHei']
['axes.unicode_minus']=False
x=(0,2*,500)
y=(x)*(-x)
fig,ax=(nrows=1,ncols=1)
(x,y)
ax.set_title("Line graphs.")
(x,y[::-1])
("Share axes of a single plotting area")
()

to this article on the realization of matplotlib shared axes (X or Y axis) of the article is introduced to this, more related matplotlib shared axes content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!