Matplotlib python为每对(x,y)值连接两个散点图和线? [英] Matplotlib python connect two scatter plots with lines for each pair of (x,y) values?

查看:48
本文介绍了Matplotlib python为每对(x,y)值连接两个散点图和线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将散点图中的两组点相互连接起来,以至于一条线(在本例中,使用虚拟数据)将所有前"点与相应的后"点连接起来.'marker = 'o-' 参数不适用于 plt.分散,但可以.阴谋.有关如何连接相应值的建议?谢谢,希望这个问题有意义!

 将matplotlib.pyplot导入为plt将numpy导入为npx1 = ["pre"] * 4x2 = [发布"] * 4y1 = [0.1,0.15,0.13,0.25]y2 = [0.85, 0.76, 0.8, 0.9]plt.scatter(x1,y1,color ='y')plt.scatter(x2, y2, color='g')plt.show()

解决方案

虽然 @ImportanceOfBeingErnest 已经为您提供了看似最简单的可能解决方案,但您可能有兴趣了解替代解决方案以获得您想要的.您可以使用

I am having trouble connecting two sets of points in a scatter plot with each other such that a line (in this example, using dummy data) connects all the 'pre' points with the corresponding 'post' points. The 'marker = 'o-' argument doesn't work for plt. scatter, but does for plt. plot. Suggestions on how to connect the corresponding values? Thanks and hope this question makes sense!

import matplotlib.pyplot as plt
import numpy as np
x1 = ["pre"] * 4
x2 = ["post"] * 4
y1 = [0.1, 0.15, 0.13, 0.25]
y2 = [0.85, 0.76, 0.8, 0.9]
plt.scatter(x1, y1, color='y')
plt.scatter(x2, y2, color='g')
plt.show()

解决方案

While @ImportanceOfBeingErnest already gave you the seemingly simplest possible solution, you might be interested in knowing an alternate solution to get what you want. You can use LineCollection

from matplotlib.collections import LineCollection

fig, ax = plt.subplots()

# Rest of your code

lines = [[x, list(zip([1]*4, y2))[i]] for i, x in enumerate(zip([0]*4, y1))]
print (lines)
# [[(0, 0.1), (1, 0.85)], [(0, 0.15), (1, 0.76)], [(0, 0.13), (1, 0.8)], [(0, 0.25), (1, 0.9)]]

lc = LineCollection(lines)
ax.add_collection(lc)
plt.show()

这篇关于Matplotlib python为每对(x,y)值连接两个散点图和线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆