Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Top and bottom line on errorbar with python and seaborn

I am trying to plot errorbars with python and seaborn but I am not entirely satisfied with how they look.

The default seaborn error bars look like this :

errobar default style

But I am looking to add the bottom and top lines on the error bars like this (in order to differentiated between the two error bars, it's the default matplotlib style) :

defaultmatplolib

How can I do this in seaborn ?

Here is the code:

import matplotlib.pyplot as plt
import seaborn as sns

fig1 = plt.figure(figsize=(20, 12))


x_values = [1,2,3,4]
y_values = [1,2,3,4]

y_error = [1,0.5,0.75,0.25]

plt.errorbar(x_values, y_values,  yerr=y_error ,fmt='o', markersize=8)

plt.show()
like image 304
Anthony Lethuillier Avatar asked Mar 10 '16 11:03

Anthony Lethuillier


People also ask

How do you show error bars in Python?

errorbar() method is used to create a line plot with error bars. The two positional arguments supplied to ax. errorbar() are the lists or arrays of x, y data points. The two keyword arguments xerr= and yerr= define the error bar lengths in the x and y directions.


2 Answers

The capsize parameter should be enough, but for some reason You have to specify the cap.set_markeredgewidth for them to show up too.. Based on: Matplotlib Errorbar Caps Missing.

(_, caps, _) = plt.errorbar(
    x_values, y_values, yerr=y_error, fmt='o', markersize=8, capsize=20)

for cap in caps:
    cap.set_markeredgewidth(1)

returns:

enter image description here

like image 197
Tony Babarino Avatar answered Nov 09 '22 16:11

Tony Babarino


The top and bottom lines on an errorbar are called caps.

To visualize them, just set capsize to a value bigger 0, which is the default value:

plt.errorbar(x, y, yerr=stds, capsize=3)
like image 3
Ray Walker Avatar answered Nov 09 '22 17:11

Ray Walker