Scatter chart with xerr and yerr with matplotlib

I want to visualize the positions of two arrays with each other. My table is as follows

Number Description value_1 value_2 err_1 err_2 1 descript_1 124.46 124.46 22.55 54.2 2 Descript_2 8.20 50.2 0.37 0.1 3 Descript_2 52.55 78.3 3.77 2.41 4 Descript_2 4.33 778.8 0.14 1.78 

what I basically want is something like this: scatterplot with two errorbars

So, in this figure, each point has basically three properties: 1. xerror bar 2. yerror bar 3. a description of what this point represents.

I have the feeling that this can be done elegantly with matplotlib, and although I tried some things with errors that didn’t quite give me what I would expect. And I have not yet learned how to place signatures in the plot.

+6
source share
2 answers

Sounds like you want something like that?

 import matplotlib.pyplot as plt x = [124.46, 8.20, 52.55, 4.33] y = [124.46, 50.2, 78.3, 778.8] xerr = [54.2, 0.1, 2.41, 1.78] yerr = [22.55, 0.37, 3.77, 0.14] descrip = ['Atom 1', 'Atom 2', 'Atom 3', 'Atom 4'] plt.errorbar(x, y, xerr, yerr, capsize=0, ls='none', color='black', elinewidth=2) for xpos, ypos, name in zip(x, y, descrip): plt.annotate(name, (xpos, ypos), xytext=(8, 8), va='bottom', textcoords='offset points') plt.show() 

enter image description here

errorbar works just like plot . If you need a β€œscatter” graph, you need to specify linestyle='none' (or, equivalently, ls='none' ). Based on your drawing, you do not want to make caps on errors, so I specified capsize=0 . Likewise, you seem to want fairly thick lines for errors, so elinewidth=2 .

If you need a marker in addition to errors, just point marker='o' (or any marker style) to errorbar .

annotate is the easiest way to annotate points on a plot. Here I indicated that the annotation should be placed 8 points above and to the right of each dimension.

+8
source

Will not

 import matplotlib.pyplot as plt x = [100, 200, 300, 400] y = [100, 200, 300, 400] xerr = [100, 100, 100, 100] yerr = [20, 20, 20, 20] plt.errorbar(x, y, xerr, yerr, ls='none') plt.show() 

mean error bars are on wrong axes?

pyplot.errorbar :

 matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt=u'', ecolor=None, elinewidth=None, capsize=3, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, errorevery=1, capthick=None, hold=None, **kwargs) 

Note that yerr precedes xerr, so

 plt.errorbar(x, y, xerr, yerr, ls='none') 

makes your error bars in the reverse order - yerr = xerr, xerr = yerr. Good time to use named args:

 plt.errorbar(x, y, xerr=xerr, yerr=yerr, ls='none') 
0
source

Source: https://habr.com/ru/post/916379/


All Articles