I want to generate two different legends for these five points according to their size, the corresponding labels are written in my code, but I only generate one wrong legend so far, how can I correct my code? By the way, if I want to generate wiring with the same logic as in my code, is there a better way? I have checked a lot of information and can only generate the picture like this, I hope to get your help to optimize the code. Thanks in advance!
Edit: I made changes with reference to matplotlib: 2 different legends on same graph, but the legend I got is still incorrect. I want to add legends only with dots instead of lines. I also tried some methods but all failed, can you give me some suggestions?
import numpy as np
import matplotlib.pyplot as plt
X_new = np.random.randint(1,20,(3,2))
x1 = X_new[0,:]
x2 = X_new[1,:]
x3 = X_new[2,:]
p = np.random.randint(1,20,(1,2))
p1 = np.random.randint(1,20,(1,2))
# plt.style.use('ggplot')
color_map = {0: 'blue', 1:'green', 2: 'darkred', 3: 'black', 4:'red'}
legend1_label = {0: 'trn1', 1: 'trn2', 2: 'trn3'}
legend2_label = {0: 'p', 1: 'p1'}
plt.plot()
for idx, cl in enumerate(legend1_label):
scatter = plt.scatter(x=X_new[idx, 0], y=X_new[idx, 1], c=color_map[cl], marker='.',
s=100)
plt.plot([x1[0],p[0,0]], [x1[1],p[0,1]], color='k',linestyle='--',linewidth=1)
plt.plot([x2[0],p[0,0]], [x2[1],p[0,1]], color='k',linestyle='--',linewidth=1)
plt.plot([x3[0],p[0,0]], [x3[1],p[0,1]], color='k',linestyle='--',linewidth=1)
plt.plot([x1[0],p1[0,0]], [x1[1],p1[0,1]], color='r',linestyle='--',linewidth=1)
plt.plot([x2[0],p1[0,0]], [x2[1],p1[0,1]], color='r',linestyle='--',linewidth=1)
plt.plot([x3[0],p1[0,0]], [x3[1],p1[0,1]], color='r',linestyle='--',linewidth=1)
plt.scatter(x=p[0,0], y=p[0,1],c='red',marker='.',s=200)
plt.scatter(x=p1[0,0], y=p1[0,1],c='black',marker='.',s=200)
legend1 = plt.legend(labels=["trn1","trn2","trn3"],loc=4, title="legend1")
plt.legend(labels=["p","p1"], title="legend2")
plt.gca().add_artist(legend1)
plt.show()