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 use Matplotlib to draw vibration diagram, box diagram and violin diagram by Python

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

Share

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

This article introduces the relevant knowledge of "how Python uses Matplotlib to draw vibration diagrams, box diagrams and violin diagrams". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Matplotlib introduction

Matplotlib is a Python software package for data visualization, which supports cross-platform operation. It can draw 2D images according to the NumPy ndarray array. It is easy to use, clear and easy to understand, and is loved by the majority of technology enthusiasts.

NumPy is a software package for Python scientific computing, and ndarray is an array structure provided by NumPy.

Matplotlib was written by John D. Hunter in 2002, Matplotlib released its first version in 2003, and joined the BSD open source software organization. Matplotlib 1.4 is the last version to support Python 2, and its latest version 3.1.1 was released on July 1, 2019.

Matplotlib provides a set of API interface for drawing object programming, which can easily realize the drawing of all kinds of images, and it can embed graphics in the application program with Python GUI tools (such as PyQt, Tkinter, etc.). At the same time, Matplotlib can also be embedded in IPython shell, Jupyter notebook and web application server in the form of script.

Vibration diagram

The vibration graph is also called magnetic field diagram, or quantity field diagram, and its image is expressed in the form of a set of vector arrows, which mathematically means that it has a partial vector (uMagnev) at the point (xMagney).

Matplotlib provides a function for drawing a quantum field, as follows:

Quiver (xpencil ypencil upenol v)

The above function indicates that the vector is drawn in the form of an arrow on the specified (xrecom y) coordinate. The parameters are described as follows:

The parameter describes the x one-dimensional or two-dimensional array or sequence, representing the x coordinates of the arrowhead position. An one-dimensional or two-dimensional array or sequence that represents the y coordinates of the arrowhead position. U one-dimensional or two-dimensional array or sequence that represents the x component of the arrow vector. V one-dimensional or two-dimensional array or sequence that represents the y component of the arrow vector. C one-dimensional or two-dimensional array or sequence that represents the arrow color.

For the following example, a simple vibration diagram is drawn:

Import matplotlib.pyplot as pltimport numpy as npx,y = np.meshgrid (np.arange (- 2,2,0.2), np.arange (- 2,2,0.25)) z = x*np.exp (- x-ray gradient 2) # calculate the gradient v of the elements in the array, u = np.gradient (z, 0.2,0.2) fig, ax = plt.subplots () Q = ax.quiver.

After the above code is executed, the output is as follows:

Figure 1: sample vibration diagram

Box diagram

Box chart (also known as box-beard diagram) was invented by John Tuckey (John Tukey), a famous American statistician in 1977. It displays the maximum, minimum, median, and upper and lower quartiles of a set of data.

In the box diagram, we draw a box from the upper quartile to the lower quartile, and then use a vertical tentacle (vividly called the "box whisker") to pass through the middle of the box. The upper perpendicular extends to the upper edge (maximum) and the lower perpendicular extends to the lower edge (minimum). The structure of the box diagram is as follows:

Figure 1: box type such as structure diagram

First prepare the data you need to create a box chart: you can use the numpy.random.normal () function to create a set of random data based on the normal distribution, which has three parameters, the average of the normal distribution, the standard deviation, and the number of expected values. As follows:

# using random number seeds to make each generated random number the same np.random.seed (10) collectn_1 = np.random.normal (100,10,200) collectn_2 = np.random.normal (80,30,200) collectn_3 = np.random.normal (90,20,200) collectn_4 = np.random.normal (70,25,200) data_to_plot=

Then use the data_to_plot variable to specify the data sequence needed to create the box diagram, and finally draw the box diagram with the boxplot () function, as shown below:

Fig = plt.figure () # create drawing area ax = fig.add_axes ([0mem0Power1]) # create box diagram bp = ax.boxplot (data_to_plot) plt.show ()

After the above code is executed, the output is as follows:

Figure 2: output result of box diagram

Violin drawing

A violin diagram (Violin Plot) is a chart used to show the distribution and probability density of data. This kind of chart combines the characteristics of box chart and density chart. The violin diagram is similar to the box diagram, except that the violin diagram also shows the probability density of the data at different values.

The violin chart uses kernel density estimation (KDE) to calculate the distribution of samples. The elements in the violin chart include median, quartile spacing and confidence interval. Violin pictures are especially suitable when the amount of data is very large and it is not convenient to display them one by one.

Probability density estimation, confidence interval and quartile spacing are all concepts in statistics and can be checked on their own, which are not explained here.

Violin pictures can provide more information than box pictures. Although the box chart shows the statistical information such as the mean, the median and the upper and lower quartile, the violin chart shows the complete distribution of the data, which is more conducive to data analysis and comparison. The following is an example of the use of a violin diagram:

Import matplotlib.pyplot as pltnp.random.seed (10) collectn_1 = np.random.normal (100,10,200) collectn_2 = np.random.normal (80,30,200) collectn_3 = np.random.normal (90,20,200) collectn_4 = np.random.normal (70,25,200) # create a data sequence for violin drawing data_to_plot = [collectn_1, collectn_2, collectn_3 Collectn_4] # create a canvas fig = plt.figure () # create a drawing area ax = fig.add_axes ([0mem0Power1]) # create a violin picture bp = ax.violinplot (data_to_plot) plt.show ()

The output is as follows:

Figure 1: violin drawing

This is the end of the introduction of "how Python uses Matplotlib to draw vibration, box and violin diagrams". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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