Matplotlib: Don't show errorbars in legend

You can modify the legend handler. See the legend guide of matplotlib. Adapting your example, this could read:

import matplotlib.pyplot as plt
import numpy as np

subs=['one','two','three']
x=[1,2,3]
y=[1,2,3]
yerr=[2,3,1]
xerr=[0.5,1,1]
fig,(ax1)=plt.subplots(1,1)

for i in np.arange(len(x)):
    ax1.errorbar(x[i],y[i],yerr=yerr[i],xerr=xerr[i],label=subs[i],ecolor='black',marker='o',ls='')

# get handles
handles, labels = ax1.get_legend_handles_labels()
# remove the errorbars
handles = [h[0] for h in handles]
# use them in the legend
ax1.legend(handles, labels, loc='upper left',numpoints=1)


plt.show()

This produces

output image


Here is an ugly patch:

pp = []
colors = ['r', 'b', 'g']
for i, (y, yerr) in enumerate(zip(ys, yerrs)):
    p = plt.plot(x, y, '-', color='%s' % colors[i])
    pp.append(p[0])
    plt.errorbar(x, y, yerr, color='%s' % colors[i])  
plt.legend(pp, labels, numpoints=1)

Here is a figure for example:

enter image description here

Tags:

Matplotlib