scatterplot with xerr and yerr with matplotlib


Question

i am looking to visualize the positions of two arrays with each other. My Table looks like this

    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 plot every point has basically three properties: 1. xerror bar 2. yerror bar 3. description what this point represents.

I have a feeling that this can be done elegantly with matplotlib, and while i tried some stuff with errorbars that did not quite give me what i would expect. And i have not yet found out how to place the captions in a plot.

1
7
5/22/2012 5:39:44 PM

Accepted Answer

It sounds like you want something like this?

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 want a "scatter" plot then you need to specify linestyle='none' (or equivalently, ls='none'). Based on your drawing, you don't want caps on the errorbars, so I've specified capsize=0. Similarly, you seem to want fairly thick lines for the errorbars, thus elinewidth=2.

If you want a marker in addition to the errorbars, just specify marker='o' (or any marker style you'd like) to errorbar.

annotate is the easiest way to annotate points on a plot. Here, I've specified that the annotation should be placed 8 points above and to the right of each measurement.

8
11/7/2013 6:01:42 PM

Won't

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 the error bars are on the 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 be backwards - yerr=xerr, xerr=yerr. A good time to use named args:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon