Get the App
SLTechnology News&Howtos  ›  Development  › 

How matplotlib shares axes

Shulou Source: shulou.com Published: 2022-06-01 12:04:13 09月25日 Update

This article mainly explains "how matplotlib shares axes". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn how to share axes with matplotlib.

Preface

1. Overview

Shared axes are the x or y axes shared between several sub-images. This section focuses on how to share axes when mapping with matplotlib.

Pyplot.subplots (nrows = 1Magna Ncols = 1Magna sharex = False,sharey = False,squeeze = True,subplot_kw = none, gridspec_kw = none, * * fig_kw)

Parameters:

Nrows: number of Lin

Ncols: number of columns

Sharex: whether to share X-axis coordinates

Sharey: whether to share Y-axis coordinates

Return value: array of Figure,Axes objects

1. Sharex and sharey code example:''1. Program purpose: based on sharex and sharey to achieve (1) share x axis (2) share y axis (3) share x axis and y axis (4) adjust the distance between subgraphs 2. Version 2.1 Qingdao, Shandong, May 18, 2021, Version 1. Related modules import import numpy as npimport matplotlib.pyplot as pltplt.rcParams ['font.sans-serif'] = [' SimHei'] # normal display Chinese font plt.rcParams ['axes.unicode_minus'] = False # normal display minus sign # 2. Create cartographic data x = np.linspace (- 5 Magneto 5100) Yizumi 1 = np.sin (x) Yizhi2 = np.cos (x) Yao3 = Yao2Liao3. Drawing # 3.1 shared X-axis figure, (ax1,ax2,ax3) = plt.subplots (3jue 1, figsize= (5je 6), dpi=600, # shared x-axis sharex=True) ax1.plot (x-journal yong-1-meme-cr-blueberry' Linestyle=':') ax2.plot (xQuery yong2pas cymbals orangewaylinestylepionghorangecores') ax3.plot (xrecinct yong3meme cymbals r' Linestyle=':') # adjust the vertical distance between sub-graphics figure.subplots_adjust (hspace=0.1) ax1.set_title ('the following three images share the X axis') # in fact, it is more reasonable to add figure.subtitle () # 3.2 share Y axis # to create a new drawing figure and axes object figure, (ax1,ax2,ax3) = plt.subplots (1 Magi 3, figsize= (6 Magne2) Dpi=600, # shared y-axis sharey=True) figure.suptitle ('the Y-axis is shared in the following three pictures') ax1.plot (Xrecedicy1 retro cymbals bluetooth pedigree') ax2.plot (x-recorder 2-minus cymbals orangeforth') ax3.plot (x-recorder 3) ) # adjust the horizontal distance between sub-shapes figure.subplots_adjust (wspace=0.1) # 3.3 share both x-axis and y-axis # create a new drawing figure and axes object figure, (ax1,ax2,ax3) = plt.subplots (1meme 3, figsize= (6jue 2), dpi=600 # sharing x-axis sharex=True # shared y-axis sharey=True) x4 = np.linspace (- 10Magazine 10100) yange4 = np.cos (x4) * 2figure.suptitle ('the following three pictures share both the X-axis and Y-axis') ax1.plot (x-mag) ax3.plot (x4) ) # adjust the horizontal distance between sub-graphics figure.subplots_adjust (wspace=0.1) plt.show () instance 2import matplotlib.pyplot as pltimport numpy as npplt.rcParams ['font.sans-serif'] = [' SimHei'] plt.rcParams ['axes.unicode_minus'] = Falsex=np.linspace (0Quinnp.pipping500) y=np.sin (x) * np.exp (- x) fig,ax=plt.subplots (nrows=1,ncols=2) Sharey=True) ax1=ax [0] ax1.plot (XMagy) ax1.set_title ("line chart") ax2=ax [1] ax2.scatter (XMagy) ax2.set_title ("scatter graph") plt.suptitle ("one canvas two subgraphs, and share y coordinates") # remove the gap wspace to the horizontal distance between the two graphs Hspace is the vertical distance between two graphs fig.subplots_adjust (wspace=0) plt.show () import matplotlib.pyplot as pltimport numpy as npplt.rcParams ['font.sans-serif'] = [' SimHei'] plt.rcParams ['axes.unicode_minus'] = Falsex=np.linspace (0np.exp 2piMague 500) y=np.sin (x) * np.exp (- x) fig,ax=plt.subplots (nrows=1,ncols=1) ax.plot (xMague y) ax.set_title ("line chart") ax.scatter (x Y [:-1]) plt.suptitle ("sharing axes of a single drawing area") plt.show () Thank you for your reading The above is the content of "how matplotlib shares axes". After the study of this article, I believe you have a deeper understanding of how matplotlib shares axes, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Coordinates axes between drawing adjusting at the same time graphics objects learning content that is horizontal line drawing two Chinese font code preface region parameters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia Shulou Information macOS MariaDB