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 operations of python data visualization

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what the operation of python data visualization, 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!

0. Preface

In the process of data processing, visualization can feel the data more intuitively, so I intend to combine some of my practice managers to write this blog based on the results. The content should continue to expand.

1. What is the relationship among figure, subplot and plot in matplotlib

Keep in mind that these relationships can be combined with reality. Suppose you go outside to sketch with what tools, including artboards, paper and brushes, then you can correspond one by one.

Function tools figure artboard subplot, add_subplot paper plot, hist, scatter brush

Then think deeply, the drawing paper is affixed to the drawing board, the drawing paper can be cut into multiple layouts on the drawing board, and the brush can only be painted on the paper, which may be a bit general, and the following code can be clearly understood with comments. (feel the need to remember the following code)

Code

Import matplotlib.pyplot as pltimport numpy as np# picks up the artboard fig = plt.figure () # and pastes the paper on the artboard ax1 = fig.add_subplot ax2 = fig.add_subplot ax3 = fig.add_subplot (223) # one step (directly pick up the artboard and paper)-# ax1 = plt.subplot # ax2 = plt.subplot # ax3 = plt.subplot (223) #-# drawing on paper ax1.hist (np.random.randn) Bins=20, color='k', alpha=0.3) ax2.scatter (np.arange (30), np.arange (30) + 3 * np.random.randn (30)) ax3.plot (np.random.randn (50). Cumsum (), 'KMurray') plt.show ()

Running result

Function analysis

The function of the line of code refers to the link ax1.hist (np.random.randn, bins=20, color='k', alpha=0.3) to draw the histogram python to interpret 2. Modification of the details of drawing

Complete the following drawing effects in turn:

1. A sine function and a random numerical curve, sine function straight line, random numerical curve dashed line, and other style modifications

two。 Modification of legends, labels, etc.

3. With the annotation, the range of the dimension is represented by a red rectangle.

2.1 modification of plot drawing form

Code

Import matplotlib.pyplot as pltimport numpy as np# picks up the drawing board fig = plt.figure () # pastes the drawing paper ax1 = fig.add_subplot (111D) # data preparation x_sin = np.arange (0,6, 0.001) # [0,6] y_sin = np.sin (x_sin) data_random = np.zeros (7) # generates 7 random numbers for i in range (0) of [- 1Jing 1] 6): data_ drawing [I] = np.random.uniform (- 1,1) # drawing ax1.plot (x_sin, y_sin, linestyle='-', color='g', linewidth=3) ax1.plot (data_random, linestyle='dashed', color='b', marker='o') plt.show ()

Running result

2.2 add legends, labels, etc.

Code

Import matplotlib.pyplot as pltimport numpy as np# picks up the drawing board fig = plt.figure () # pastes the drawing paper ax1 = fig.add_subplot (111D) # data preparation x_sin = np.arange (0,6, 0.001) # [0,6] y_sin = np.sin (x_sin) data_random = np.zeros (7) # generates 7 random numbers for i in range (0) of [- 1Jing 1] 6): data_ drawing [I] = np.random.uniform (- 1,1) # drawing ax1.plot (x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin') ax1.plot (data_random, linestyle='dashed', color='b', marker='o' Label='random') #-add part-# add title ax1.set_title ('Title') # add x-axis name ax1.set_xlabel (' x') # set x-axis coordinate range ax1.set_xlim (xmin=0, xmax=6) # add legend Add labelax1.legend (loc='best') #-plt.show () to the plot

Running result

2.3 draw notes and rectangles on the diagram

Code

Import matplotlib.pyplot as pltimport numpy as np# picks up the drawing board fig = plt.figure () # pastes the drawing paper ax1 = fig.add_subplot (111D) # data preparation x_sin = np.arange (0,6, 0.001) # [0,6] y_sin = np.sin (x_sin) data_random = np.zeros (7) # generates 7 random numbers for i in range (0) of [- 1Jing 1] 6): data_ drawing [I] = np.random.uniform (- 1,1) # drawing ax1.plot (x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin') ax1.plot (data_random, linestyle='dashed', color='b', marker='o' Label='random') # add title ax1.set_title ('Title') # add x-axis name ax1.set_xlabel (' x') # set x-axis coordinate range ax1.set_xlim (xmin=0, xmax=6) # add legend ax1.legend (loc='best') #-add part-# Annotation ax1.annotate ('max' Xy= ((np.pi) / 2, np.sin (np.pi/2)), xytext= ((np.pi) / 2, np.sin (np.pi/2)-0.2), arrowprops=dict (facecolor='black', headwidth=4, width=2,headlength=4), horizontalalignment='left', verticalalignment='top') ax1.annotate ('min', xy= ((np.pi) * 3 / 2, np.sin (np.pi * 3 / 2) Xytext= ((np.pi) * 3 / 2, np.sin (np.pi * 3 / 2) + 0.2), arrowprops=dict (facecolor='black', headwidth=4, width=2,headlength=4), horizontalalignment='left', verticalalignment='top') # rectangular print (ax1.axis ()) rect = plt.Rectangle ((np.pi / 2, ax1.axis () [2]), np.pi, ax1.axis () [3]-ax1.axis () [2] Color='r', alpha=0.3) # starting coordinate point Width, heightax1.add_patch (rect) #-plt.show ()

Running result

3. Graphics save plt.savefig ('figpath.png', dpi=400)

Be careful to put it in front of the show.

Complete code:

Import matplotlib.pyplot as pltimport numpy as np# picked up the artboard fig = plt.figure () # and pasted the paper ax1 = fig.add_subplot ax2 = fig.add_subplot ax3 = fig.add_subplot (223) # data preparation x_sin = np.arange (0,6, 0.001) # [0,6] y_sin = np.sin (x_sin) data_random = np.zeros (7) # generate 7 random numbers for i in range (0) 6): data_ drawing [I] = np.random.uniform (- 1,1) # drawing ax1.plot (x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin') ax1.plot (data_random, linestyle='dashed', color='b', marker='o', label='random') ax2.plot (x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin') ax2.plot (data_random, linestyle='dashed', color='b' Marker='o', label='random') ax3.plot (x_sin, y_sin, linestyle='-', color='g', linewidth=3, label='sin') ax3.plot (data_random, linestyle='dashed', color='b', marker='o', label='random') # # add title ax2.set_title ('Title') # add x-axis name ax2.set_xlabel (' x') # set x-axis coordinate range ax2.set_xlim (xmin=0 Xmax=6) # add legend ax2.legend (loc='best') ax3.set_title ('Title') # add x-axis name ax3.set_xlabel (' x') # set x-axis coordinate range ax3.set_xlim (xmin=0, xmax=6) # add legend ax3.legend (loc='best') # Note ax3.annotate ('max', xy= ((np.pi) / 2, np.sin (np.pi/2)), xytext= (np.pi) / 2 Np.sin (np.pi/2)-0.2), arrowprops=dict (facecolor='black', headwidth=4, width=2,headlength=4), horizontalalignment='left', verticalalignment='top') ax3.annotate ('min', xy= ((np.pi) * 3 / 2, np.sin (np.pi * 3 / 2)), xytext= ((np.pi) * 3 / 2, np.sin (np.pi * 3 / 2) + 0.2) Arrowprops=dict (facecolor='black', headwidth=4, width=2,headlength=4), horizontalalignment='left', verticalalignment='top') # rectangle # print (ax1.axis ()) rect = plt.Rectangle ((np.pi / 2, ax3.axis () [2]), np.pi, ax3.axis () [3]-ax3.axis () [2], color='r', alpha=0.3) # starting coordinate point Width, heightax3.add_patch (rect) #-add part-plt.savefig ('figpath.png' Dpi=400) #-plt.show () is all the content of the article entitled "what are the operations of python data visualization?" 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