Tho*_*ten 2 python plot scatter matplotlib
有没有办法影响散点图中哪些点连接?
我想要一个散点图,其中连接在一起的点是连接的.当我使用plot(x,y)命令绘图时,点之间的线取决于列表的顺序,这不是我想要的.
我从你的问题的措辞中暗示你有两组点x,y是无序的.当你绘制它们时(不是在@tcaswell指出的散点图中;这不会连接点!),因此连接点的线将遵循点的顺序.
如果这是您要解决的问题,可以这样做:
fig, (ax1, ax2) = plt.subplots(ncols=2)
x = np.random.uniform(0, 1, 10)
y = np.random.uniform(0, 1, 10)
# Plot non-ordered points
ax1.plot(x, y, marker="o", markerfacecolor="r")
# Order points by their x-value
indexs_to_order_by = x.argsort()
x_ordered = x[indexs_to_order_by]
y_ordered = y[indexs_to_order_by]
ax2.plot(x_ordered, y_ordered, marker="o", markerfacecolor="r")
Run Code Online (Sandbox Code Playgroud)
重要的一点是,如果您正在使用的数据是numpy数组(如果它们只是调用列表np.array(list)),则argsort返回已排序数组的索引.使用这些索引意味着我们可以对两个列表进行成对排序,并按正确的顺序绘制,如右图所示:

如果我误解了你的问题,那么我道歉.在这种情况下,请发表评论,我会尝试删除我的答案.