30

I'm attempting to create a scatter plot with errorbars in matplotlib. The following is an example of what my code looks like:

import matplotlib.pyplot as plt
import numpy as np
import random

x = np.linspace(1,2,10)
y = np.linspace(2,3,10)
err = [random.uniform(0,1) for i in range(10)]

plt.errorbar(x, y,
       yerr=err,
       marker='o',
       color='k',
       ecolor='k',
       markerfacecolor='g',
       label="series 2",
       capsize=5,
       linestyle='None')
plt.show()

The problem is the plot which is output contains no caps at all! enter image description here

For what it's worth, I'm on Ubuntu 13.04, Python 2.7.5 |Anaconda 1.6.1 (64-bit)|, and Matplotlib 1.2.1.

Could this be a hidden rcparam that needs to be overwritten?

astromax
  • 6,001
  • 10
  • 36
  • 47
  • 1
    did you change anything to make your background gray? I am wondering if you are having zorder issues (related to http://stackoverflow.com/questions/14003572/how-to-force-errorbars-to-render-last-with-matplotlib/14007175#14007175). The patch for that issue is in 1.3, but not 1.2.1 – tacaswell Aug 25 '13 at 23:04
  • 3
    Your code runs correctly on my system, I suspect that in making the graphs look awful (yes, I know it is subjective) you broke something. We need to see your rcparams to debug this. – tacaswell Aug 25 '13 at 23:08
  • Yes, Actually I am using a matplotlibrc file which is significantly altered from what it is by default. What section of the file are you needing to look at? – astromax Aug 26 '13 at 02:17
  • 1
    does this work as expected if you use the default `matplotlibrc` file (just rename yours before you start up python)? – tacaswell Aug 26 '13 at 03:08

3 Answers3

25

What worked for me was adding this (as per: How to set the line width of error bar caps, in matplotlib):

(_, caps, _) = plt.errorbar(x,y, yerr=err, capsize=20, elinewidth=3)

for cap in caps:
    cap.set_color('red')
    cap.set_markeredgewidth(10)
Community
  • 1
  • 1
astromax
  • 6,001
  • 10
  • 36
  • 47
  • Not particularly. I've tried to understand it, but it the syntax seems to be confusing me. I know (_,caps,_) is producing a tuple of tuples, however, set_color() and set_markeredgewidth() I am unfamiliar with. – astromax Aug 28 '13 at 02:42
  • They do exactly what they sound like, set the color and the the edge width of the markers. http://matplotlib.org/api/artist_api.html#matplotlib.lines.Line2D.set_color http://matplotlib.org/api/artist_api.html#matplotlib.lines.Line2D.set_markeredgewidth – tacaswell Aug 28 '13 at 02:55
  • I was more asking did you figure out what you changed in you `matplotlibrc` file that broke it. – tacaswell Aug 28 '13 at 02:55
  • For me the main fault was for some inexplicable reason `capsize`. Instead of `set_markeredgewidth` and `set_color`, you should be able to use the `capthick` and `ecolor` arguments in the [`errorbar` call](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.errorbar.html). – mxmlnkn Mar 19 '18 at 15:50
16

It has to do with the rcParams in matplotlib. To solve it, add the following lines at the beginning of your script:

import matplotlib
matplotlib.rcParams.update({'errorbar.capsize': 2})

It also works with plt.bar().

Neuron
  • 5,141
  • 5
  • 38
  • 59
Lucho
  • 311
  • 2
  • 4
16

Slight simplification of astromax's answer:

plt.errorbar(x,y, yerr=err, capsize=20, elinewidth=3, markeredgewidth=10)

It seems that somehow markeredgewidth is defaulting to 0 sometimes.

aquirdturtle
  • 2,038
  • 26
  • 20