Подтвердить что ты не робот

Matplotlib: не показывать ошибки в легендах

Я рисую серию точек данных с ошибками x и y, но НЕ хочу, чтобы ошибки были включены в легенду (только маркер). Есть ли способ сделать это?

How to avoid errorbars in legend?

Пример:

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='')
ax1.legend(loc='upper left', numpoints=1)
fig.savefig('test.pdf', bbox_inches=0)
4b9b3361

Ответ 1

Вы можете изменить обработчик легенды. См. руководство по легенде matplotlib. Адаптируя ваш пример, это можно прочитать:

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

Это создает

output image

Ответ 2

Вот уродливый патч:

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)

Вот цифра, например:

enter image description here

Ответ 3

Я работаю для меня, если я устанавливаю аргумент метки как тип None.

plt.errorbar(x, y, yerr, label=None)