Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of drawing subgraphs in Matplotlib

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what are the methods of drawing subgraphs in Matplotlib". The editor shows you the operation process through actual cases, the method of operation is simple and fast, and it is practical. I hope that this article "what are the methods of drawing subgraphs in Matplotlib" can help you solve the problem.

Preface

Matplotlib can put many pictures into one display interface, which is very useful for comparative analysis.

The corresponding methods are subplot of plt and add_subplo of figure. The parameters can be either a three-digit number (for example, 111or an array (for example, [1ji1Jue 1]), with three numbers representing respectively.

Total number of rows of subgraph

Total number of columns of subgraph

Subgraph location

Here are two kinds of code for drawing subgraphs

Three commonly used ways

Method 1: through the subplot of plt

Import numpy as npimport pandas as pdimport matplotlib.pyplot as plt# author: chenqionghe# draw the first chart: line chart x=np.arange (1100) plt.subplot (221) plt.plot (x Magnex) # draw the second chart: scatter chart plt.subplot (1100) plt.scatter (np.arange (0L10), np.random.rand (10)) # draw the third chart: pie chart plt.subplot (223) plt.pie (x = [15,030,221) plt.plot ('ABCD'), autopct='%.0f',explode= (0]) # draw the 4th graph: bar graph plt.subplot (224) plt.bar ([20, 10, 10, 15, 25, 15), [25, 15, 15, 35, 30, 20], color='b') plt.show ()

Method 2: through the add_subplot of figure

Import numpy as npimport pandas as pdimport matplotlib.pyplot as plt# author: chenqionghefig=plt.figure () # draw the first chart: line chart x=np.arange (1100) ax1=fig.add_subplot (221x) ax1.plot (xMaginx) # draw the second chart: scatter chart ax2=fig.add_subplot (222m) ax2.scatter (np.arange (0L10), np.random.rand (10)) # draw the third chart: pie chart ax3=fig.add_subplot (223) ax3.pie (x = [15,30,45,10], labels=list ('ABCD') Autopct='%.0f',explode= # draw the 4th graph: bar graph ax4=fig.add_subplot (224) ax4.bar ([20pc10pr 30pr 25pc15], [25pr 15pr 35je 30g 20g], color='b') plt.show ()

Method 3: through the subplots of plt

The value returned by subplots is of type tuple and contains two elements: the first is a canvas and the second is a subgraph

Import numpy as npimport pandas as pdimport matplotlib.pyplot as plt# author: chenqionghefig,subs=plt.subplots (2jue 2) # draw the first graph: line chart x=np.arange (1100) subs [0] [0] .plot (xQuinx) # draw the second graph: scatter subs [0] [1] .scatter (np.arange (0prime10), np.random.rand (10)) # draw the third chart: pie chart subs [1] [0] .pie (x = [15pc30, 45p10], labels=list ('ABCD'), autopct='%.0f' Explode= # draw the 4th graph: bar graph subs [1] [1] .bar ([20pc10] 30pr 25pr 15], [25pr 15pr 35pr 30pr 20], color='b') how to divide plt.show () irregularly

The first two pictures occupy positions 221 and 222. If you want to put only one picture below, you have to treat the first two as one column, that is, the second position of two rows and one column.

Import numpy as npimport pandas as pdimport matplotlib.pyplot as plt# author: chenqionghe# draw the first chart: line chart x=np.arange (1100) plt.subplot (221) plt.plot (x Magnex) # draw the second chart: scatter chart plt.subplot (1100) plt.scatter (np.arange (0L10), np.random.rand (10)) # draw the third chart: pie chart plt.subplot (223) plt.pie (x = [15,030,221) plt.plot ('ABCD'), autopct='%.0f',explode= 0]) # draw the third picture: bar chart # the two pictures in front occupy 221 and 222 positions If you want to put only one picture below, you have to put the first two as a column, that is, 2 rows and 1 column, and the second position plt.subplot (212) plt.bar ([20 plt.bar 10, 10, 30], [25, 15, 15, 35, 30, 20], color='b') plt.show () about "what are the methods of drawing subgraphs by Matplotlib" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report