SoFunction
Updated on 2024-11-10

Matplotlib basic plotting commands of the use of errorbar

In matplotlib, the errorbar method is used to plot line plots with error lines, and is basically used as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1)

The output is as follows

The yerr parameter is used to specify the error at the y-axis level, and the method also supports the error at the x-axis level, which corresponds to the parameter xerr. There are several ways to specify the error value, and the above code shows the usage of specifying a uniform scalar, at which time, so the point error value is the same.

In addition to this, you can specify an array of the same number of points and set the error value for each point individually, as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=[1, 2, 3, 4])

The output is as follows

In addition, considering that the error above and below each point will be different, it is also supported to specify the error value above and below each point individually with a multi-dimensional array of rows of 2, as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=[[1,2,3,4],[1, 2, 3, 4]])

The output is as follows

The xerr parameter is used in the same way as the yerr parameter, so I won't repeat it here.

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], xerr=1)

The output is as follows

The errorbar method supports specifying both xerr and yerr parameters, as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], xerr=0.5, yerr=0.5)

The output is as follows

For the style of the error map, it can be individually specified with the following parameters

1. fmt

The value of the fmt parameter is abbreviated in the same way as the color, shape, and line style of the point specified in the plot method, as shown in the following example

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1, fmt='co--')

The above code specifies 3 attributes at the same time and the output is as follows

The default diagram only has the element line, so when we specify the attributes of the point, if we don't specify the style of the line and other attributes, the corresponding attributes will be empty, and the line element will not be displayed, the example is as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1, fmt='co')

The above code does not specify the style of the line and the output is as follows

Let's look at another example, the sample is as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1, fmt='c')

The above code specifies only the color attribute and the output is as follows

2. ecolor

The ecolor parameter specifies the color of the error bar, which can be distinguished from the color of the line, as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1, fmt='co--', ecolor='g')

The output is as follows

3. elinewidth

The elinewidth parameter specifies the width of the line of the error bar.

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1, fmt='ro-',ecolor='k',elinewidth=10)

The output is as follows

4. lims series parameters

The lims series of parameters are used to control the display of the error line, and for the error line at the level of the x-axis, there are the following two parameters

1. xuplims

2. xlolims

For the error line at the level of the y-axis, there are the following two parameters

1. uplims

2. lolims

The default value of these four parameters is False. When the value is True, the error line in the corresponding direction is not displayed, and the error line in the other direction is marked with an arrow.

When the value of the uplims parameter is True, upward error lines are not displayed and downward error lines are added with arrows, as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1, uplims=True)

The output is as follows

When the value of the lolims parameter is True, the downward error line is not displayed and the upward error line is added with an arrow as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1, lolims=True)

The output is as follows

When the values of the uplims and lolims parameters are both True, arrows are added to the error lines in both directions, as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1, uplims=True, lolims=True)

The output is as follows

In addition to being specified as a scalar, the values of the lims series of parameters can also be a list, with separate values for each point, as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1, uplims=[False, True, False, True], lolims=[True, False, True, False])

The output is as follows

Different combinations of True and False can achieve different effects, examples are as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=0.5, uplims=[True,True,False,False],lolims=[True,False,True,False])

The output is as follows

Similar to xerr,yerr, we can also specify 4 lims parameters at the same time, the example is as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=0.5, uplims=[True,True,False,False],lolims=[True,False,True,False],xerr=0.5, xuplims=[True,False,True,False],xlolims=[True,True,False,False])

The output is as follows

5. errorevery

The errorevery parameter is used to specify the sampling frequency of the error line, by default, the error line of each point will be displayed, when there are a lot of points and densely distributed, if the error line is displayed at each point, it will be difficult to see the valid information, for example, in the figure below.

(x=range(100), y=range(100),yerr=50)

Overly dense cases can be sampled using the errorevery parameter, which is basically used as follows

(x=range(100), y=range(100),yerr=50,errorevery=6)

The above code indicates that starting from the first point, an error line is drawn every 6 points, so that after sampling, the error line is not so dense, and the output is as follows

In addition to the above exclusive basic parameters, there are many general parameters to fine-tune the style of the errorbar, examples are as follows

(x=[1, 2, 3, 4], y=[1, 2, 3, 4], yerr=1, marker='s', mfc='red', mec='green', ms=20, mew=4)

The output is as follows

There are many parameters in errorbar, just master the common ones.

to this article on the use of matplotlib basic plotting commands errorbar article is introduced to this, more related matplotlib errorbar content, please search my previous posts or continue to browse the following related articles I hope you will support me in the future!