-1

I am using matplotlib to plot some error bars with some labels and plot legends. However, I notice that double the legend symbols are being added. To replicate this, I have the following code:

import numpy as np
import pylab as pl

# define datasets
parameters = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
mean_1 = [10.1, 12.1, 13.6, 14.5, 18.8, 11.8, 28.5]
std_1 = [2.6, 5.7, 4.3, 8.5, 11.8, 5.3, 2.5]

mean_2 = [10.1, 12.1, 13.6, 14.5, 18.8, 11.8, 28.5]
std_2 = [2.6, 5.7, 4.3, 8.5, 11.8, 5.3, 2.5]

mean_3 = [10.1, 12.1, 13.6, 14.5, 18.8, 11.8, 28.5]
std_3 = [2.6, 5.7, 4.3, 8.5, 11.8, 5.3, 2.5]

pl.errorbar(np.array(parameters)-0.01, mean_1, yerr=std_1, fmt='bo', label='M1')
pl.errorbar(parameters, mean_2, yerr=std_2, fmt='go', label='M2')
pl.errorbar(np.array(parameters)+0.01, mean_3, yerr=std_3, fmt='ro', label='M3')
pl.legend(loc='upper left')
pl.show()

To illustrate the problem, the code generates the following image:

enter image description here

As you can see, the legend is getting replicated.

Luca
  • 10,458
  • 24
  • 107
  • 234

1 Answers1

2

I think Matplotlib plots two points in the legend by default. You should be able to override this by calling pl.legend(loc='upper left', numpoints=1).

There's a lot more details in the answers for: matplotlib Legend Markers Only Once

Community
  • 1
  • 1
Will Angley
  • 1,392
  • 7
  • 11