如何在散点图中按顺序连接点?

时间:2020-04-05 20:56:01

标签: python scatter-plot line-plot

我想连接一些点以了解数据的形状。我的代码如下

import numpy as np 
import matplotlib.pyplot as plt 
from random import randint, uniform,random
import random
import math

FO_1_NSGA=[1737903,1775238,1745292,1753900,1739432,1765932]
FO_2_NSGA=[1250000,1212665,1235970,1224003,1242687,1216719]
plt.scatter(FO_1_NSGA,FO_2_NSGA,marker='s',c="red",linewidths=True,label="NSGA",edgecolors='blue')
plt.plot(FO_1_NSGA,FO_2_NSGA)
plt.grid()
plt.xlabel("Costo Total de Transporte (CTT)")
plt.ylabel("Costo Total por Demanda Insatisfecha (CDIT)")
plt.legend(loc=0)
plt.show()

输出如下(正确)

enter image description here

我想连接这些点以查看变量的指数递减行为,但是当我尝试这样做时,添加以下代码

plt.plot(FO_1_NSGA,FO_2_NSGA)

我明白了 enter image description here

这显然是不对的...有什么想法吗?。

1 个答案:

答案 0 :(得分:1)

您的数据集不是按升序或降序排列的,您需要确保在使用plt.plot时,x变量具有升序,而在相应位置具有相应的y变量。这是因为plt.plot将按照给定的顺序连接点,因此必须按升序或降序提供数据点。

将数据集重新格式化为:

F0_1_NSGA = [1737903,1739432,1745292,1753900,1765932,1775238]

F0_2_NSGA = [1212665、1216719、1224003、1235970、1242687、1250000]

这将确保您的plt.plot和plt.scatter具有相同的图形形状。

希望能奏效:)