Home > Enterprise >  Label a whole numpy array with one label on matplotlib
Label a whole numpy array with one label on matplotlib

Time:11-22

I would like to label a whole numpy array with only one label. The following code for example creates 6 (=2 4) labels instead of only 2 labels:

import numpy as np
import matplotlib.pyplot as plt

a = np.random.rand(10,2)
b = np.random.rand(10,4)


plt.figure()
plt.plot(a, 'blue', label = 'a')
plt.plot(b, 'red', label = 'b')
plt.legend()  

How should the code above be modified to create only 2 legend labels, 'a' and 'b'?

enter image description here

CodePudding user response:

a_lines = plt.plot(a, c='blue')
b_lines = plt.plot(b, c='red')
plt.legend(handles=[a_lines[0], b_lines[0]], labels=['a', 'b'])
  • Related