Основная проблема - это смешивание типов аргументов.Вы можете использовать любой из
plt.legend(handles, labels)
plt.legend(handles=handles, labels=labels)
, но не
plt.legend(handles, labels=labels)
Во-вторых, вам нужно распаковать список строк, чтобы добавить к легенде.
Всего:
import matplotlib.pyplot as plt
x1 = [1,2,3]
x2 = [4,5,6]
x3 = [7,8,9]
x4 = [10,11,12]
y1 = [1,2,3]
y2 = [4,5,6]
y3 = [7,8,9]
y4=[10,11,12]
set1, = plt.plot(x1, y1, '.', color="red")
set2, = plt.plot(x2, y2, '*', color="blue")
set3, = plt.plot(x3, y3, '^', color="green")
set4, = plt.plot(x4, y4, 'x', color="black")
label_set1_and_set2 = ["set1", "set2"]
label_set3_and_set4 = ["set3", "set4"]
leg1 = plt.legend(handles=[set1, set2], labels=label_set1_and_set2, loc=0,
title="Set1 and Set2", frameon=True, fontsize=10)
ax = plt.gca()
ax.add_artist(leg1)
leg2 = plt.legend(handles=[set3, set4], labels=label_set3_and_set4, loc=4,
title="Set3 and Set4")
plt.show()
![enter image description here](https://i.stack.imgur.com/EwiD8.png)