向具有两组数据的散点图添加图例

Adding a legend to a scatter plot with two sets of data

我按以下方式绘制了我的数据:

G=[(1.42*1e-5, 8.5*1e-2), (1.19*1e-5, 7.8*1e-2), (1.03*1e-5, 6*1e-2), (8.95*1e-6, 4.7*1e-2), (7.63*1e-6, 3.8*1e-2), (7.12*1e-6, 3.2*1e-2), (5.72*1e-6, 2.6*1e-2)]
PN=[5*1e3, 10*1e3, 20*1e3, 40*1e3, 80*1e3, 120*1e3, 200*1e3]


figure(5,figsize=(12,10))
for PNe, Ge, in zip(PN, G):
    scatter([PNe]*len(Ge), Ge, color=['red', 'green'])
grid()
xlim(xmin=0, xmax=200000)
#ylim(ymin=0, ymax=1)
xlabel('Number of particles')
ylabel(r'Energy release rate')
legend(['$G_{simulation}$','$G_{analytical}$'])

我得到的图例如下:legend

如您所见,颜色属性不正确。 我需要将 red 分配给 G_{analytical} 并将 green 分配给 G_{simulation}。 我在这里做错了什么?

谢谢

所以这是交易, 只需将绘图格式更改为

Gs=[8.5*1e-2, 7.8*1e-2, 6*1e-2, 4.7*1e-2, 3.8*1e-2, 3.2*1e-2, 2.6*1e-2]
Ga=[1.42*1e-5, 1.19*1e-5, 1.03*1e-5, 8.95*1e-6, 7.63*1e-6, 7.12*1e-6, 5.72*1e-6]
PN=[5*1e3, 10*1e3, 20*1e3, 40*1e3, 80*1e3, 120*1e3, 200*1e3]


figure(5,figsize=(12,10))
scatter(PN, Gs, color='red', label='$G_{Simulation}$')
scatter(PN, Ga, color='green', label='$G_{Analytical}$')

grid()
xlim(xmin=0, xmax=200000)
#ylim(ymin=0, ymax=1)
xlabel('Number of particles')
ylabel(r'Energy release rate')
legend()

完成任务。 这是结果:Label with correct color assignment

我们开始了。