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

How to draw subgraphs by Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to draw sub-pictures of Python, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

1. The comparison of the graphs will be shown in the form of subgraphs. 2. The drawing code is as follows: accuracy_alexnet_clef = [78.05,78.43,78.65,78.61,78.69] accuracy_resnet_clef = [84.56,84.84,85.07,85.01,85.13] accuracy_alexnet_office10 = [87.30,87.57,87.78,87.72,87.50] accuracy_resnet_office10 = [96.31,96.35,96.62,96.43] [96.15] orders = ['2percent,' 3percent, '5percent,' 10percent, '20'] names = [' alexnet', 'resnet'] # create two subgraphs f, ax = plt.subplots ) # the offset coordinate of the first column x = [i for i in range (len (orders))] # the offset coordinate of the second column x1 = [I + 0.35for i in range (len (orders))] # the distance between the two subimages plt.subplots_adjust (wspace = 0) Hspace = 0.4) # Select the first image figure_1 = ax [0] # set the x-axis offset and label figure_1.set_xticks ([iDiver0.15 for i in x]) figure_1.set_xticklabels (orders) # set the range of the y-axis figure_1.set_ylim (bottom=77,top=86) # draw a bar chart X represents x-axis content, accuracy_alexnet_clef represents y-axis content, and alpha represents transparency Width represents column width # label represents column figure_1.bar (x, accuracy_alexnet_clef, alpha=0.7, width = 0.35, facecolor ='# 4c72b0mm, label='Alexnet') figure_1.bar (x1, accuracy_resnet_clef, alpha=0.7, width = 0.35, facecolor ='# dd8452' Label='Resnet') figure_1.set_ylabel ('Accuracy%') # set the label of the y-axis figure_1.set_xlabel (' Order') # set the name of the x-axis figure_1.set_title ('Alexnet') # set the title name of figure 1 figure_1.legend () # display the legend of figure 1 # Select the second picture figure_2 = ax [1] figure_1.set_xticks ([iShate0.15 for I] In x]) figure_1.set_xticklabels (orders) figure_2.set_ylim (bottom=77 Top=100) figure_2.bar (x, accuracy_alexnet_office10,alpha=0.7,width = 0.35 orders, label='Alexnet') figure_2.bar (x1, accuracy_resnet_office10,alpha=0.7,width = 0.35) figure_2.bar (orders, accuracy_resnet_clef,alpha=0.7,width = 0.35) Facecolor ='# dd8452') figure_2.set_ylabel ('Accuracy%') figure_2.set_xlabel (' Order') figure_2.set_title ('Resnet') figure_2.legend () f.suptitle (' ImageCLEF_DA') # set the general title plt.show ()

Add: python uses matplotlib to draw multiple sub-graphs in one graph and multiple dynamic broken lines in one sub-graph

Before explaining how to draw multiple sub-graphs, briefly understand how to use matplotlib to draw a graph, import the necessary library matplotlib for drawing and create an equally spaced list x, divide [0Jing 2Secretpi] into 50 equal parts, and draw the function sin (x). When no x-axis value is given, the default subscript is the value of x, and if the x value is determined, it is written as plt.plot (x _ line y) when drawing.

If you want to write a label when you want to draw a picture, write it as plt.plot (xQuery yjournal label = "figure1").

From numpy import * import matplotlib.pyplot as plt x = linspace (0,2 * pi, 50) plt.plot (sin (x)) plt.xlabel ('xmurlabel') plt.ylabel ('Murray labelings, fontsize='large') plt.title (' title')

The following is to insert the whole code, and then distribute the explanation:

Import numpy as npimport matplotlib.pyplot as plt from matplotlib.ticker import MultipleLocator, FormatStrFormatter def minmax_value (list1): minvalue=min (list1) maxvalue=max (list1) return minvalue,maxvalueplt.figure (figsize= (16) 14) Dpi=98) xmajorLocator = MultipleLocator (1) # set the x main scale label to a multiple of 1 plt.rcParams ['font.sans-serif'] = [' SimHei'] plt.rcParams ['axes.unicode_minus'] = Falsep1 = plt.subplot (121) p2 = plt.subplot (122) # the number of points shown in the figure pointcount=5x= [i for i in range (20)] print (x) y1 = [iTunes 2 for i in range (20)] y2 = [item4 for i in range ( 20)] y3 = [iTunes 2 for i in range (20)] y4 = [iatro4 for i in range (20)] for i in range (len (x)-1): if i=pointcount: minx Maxx=minmax_value (x [I-pointcount:i]) minx,maxx=minmax_value (x [I-pointcount:i]) minyA,maxyA=minmax_value (y1 [I-pointcount:i]) minyB,maxyB=minmax_value (y2 [I-pointcount:i]) maxy1=max (maxyA,maxyB) miny1=min (minyA,minyB) p1.axis ([minx,maxx,miny1,maxy1]) p1.grid (True) A = p1.plot (x [I-pointcount:i], y1 [i-pointcount:i], "g -") B _ pointcount:i p1.plot (x [I-plot], y2 [i-pointcount:i], "b -") # sets the location of the main scale label, the format of the label text p1.xaxis.set_major_locator (xmajorLocator) legend=p1.legend (handles= [AMague B], labels= ["figure 1") "figure 2") minx,maxx=minmax_value (x [I-pointcount:i]) minx,maxx=minmax_value (x [I-pointcount:i]) minyA,maxyA=minmax_value (y3 [I-pointcount:i]) minyB,maxyB=minmax_value (y4 [I-pointcount:i]) maxy1=max (maxyA,maxyB) miny1=min (minyA,minyB) p2.axis ([minx,maxx,miny1]) Maxy1]) p2.grid (True) Agramp2.plot (x [I-pointcount:i], y3 [i-pointcount:i], "r -") Brecedp2.plot (x [I-pointcount:i], y4 [i-pointcount:i], "y -") # sets the location of the main scale label, the format of the label text p2.xaxis.set_major_locator (xmajorLocator) legend=p2.legend (handles= [A) B], labels= ["figure 3", "figure 4"]) p1.set_xlabel ("horizontal axis attribute name 1", fontsize=14) p1.set_ylabel ("vertical axis attribute name 1", fontsize=14) p1.set_title ("topic 1", fontsize=18) p2.set_xlabel ("horizontal axis attribute name 2", fontsize=14) p2.set_ylabel ("vertical axis attribute name 2", fontsize=14) p2.set_title ("topic 2") Fontsize=18) plt.pause (0.3) plt.tight_layout (pad=4, w_pad=4.0, h_pad=3.0)

The running result is:

1. Import the library

Import numpy as npimport matplotlib.pyplot as plt from matplotlib.ticker import MultipleLocator, FormatStrFormatter

2. Because the maximum and minimum value is used many times in the drawing process, the maximum and minimum value will be written into the function, and then the function can be called directly.

Def minmax_value (list1): minvalue=min (list1) maxvalue=max (list1) return minvalue,maxvalue

3 、

(1) create a custom image, and set the length and width of figured and the dpi parameter to specify the resolution of the drawing object.

(2) set the interval of the x-axis scale.

(3) set the font in this drawing.

(4) under matplotlib, a figure object can contain multiple subgraphs (Axes), which can be quickly drawn using subplot ().

Plt.figure (figsize= (165.14), dpi=98) xmajorLocator = MultipleLocator (1) plt.rcParams ['font.sans-serif'] = [' SimHei'] plt.rcParams ['axes.unicode_minus'] = False p1 = plt.subplot (121p2 = plt.subplot)

4. When there is too much data, the one-time display of the data can not achieve the interpretation of the internal information of the data. In this example, a part of the data is displayed at once, and the picture is updated dynamically, at the same time, the value range of the horizontal and vertical axis is dynamically updated. The following code first sets the number of points for each presentation and gets all the data values in topic 1. The value range of horizontal and vertical coordinates in the current drawing process is obtained according to the value range of x and value range y, and finally the drawing is carried out according to the value of x ~ ~ y.

Below, we will first show two static discounts on a subgraph. When using a dynamic line chart, you only need to dynamically update the range of data and horizontal and vertical coordinates. It has been written in the overall code, so I won't repeat it below.

# the number of points shown in the figure pointcount=5x= [I for i in range (20)] y1 = [item4 for i in range (20)] minx,maxx=minmax_value (x [: pointcount]) minyA,maxyA=minmax_value (y1 [: pointcount]) minyB,maxyB=minmax_value (y2 [: pointcount]) maxy1=max (maxyA,maxyB) miny1=min (minyA,minyB) p1.axis ([minx,maxx,miny1]) Maxy1]) p1.grid (True) # Grid settings that appear during the plotting process. Plot (x [: pointcount], y1 [: pointcount], "g -") BGrainp1.plot (x [: pointcount], y2 [: pointcount], "b -") # sets the location of the main scale label, the format of the label text p1.xaxis.set_major_locator (xmajorLocator) legend=p1.legend (handles= [AMague B], labels= ["figure 1", "figure 2"])

5, set the boundary, do not set the boundary often because the font of the horizontal and vertical axis is too large and other reasons such as horizontal and vertical axis or title can only display part of it.

Plt.tight_layout (pad=4, w_pad=4.0, h_pad=3.0) above is all the content of this article "how to draw a subgraph in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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