Matplotlib Errorbar Caps Missing

Question:

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?

Asked By: astromax

||

Answers:

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)
Answered By: astromax

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().

Answered By: Lucho

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.

Answered By: aquirdturtle
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.