Python 基于坐标的图像重叠图

Python 基于坐标的图像重叠图,python,python-3.x,networkx,Python,Python 3.x,Networkx,我有两个坐标列表:第一个是x坐标,第二个是y坐标。我试图将它们用作图的节点 import networkx as nx list_of_coordinates_x = list(4.5 , 4, 67, 578, 68) #random numbers list_of_coordinates_y = list(6.7, 3, 12, 45.555, 692) G=nx.MultiGraph() for i in range(len(list_of_coordinates_x)): G.

我有两个坐标列表:第一个是x坐标,第二个是y坐标。我试图将它们用作图的节点

import networkx as nx
list_of_coordinates_x = list(4.5 , 4, 67, 578, 68) #random numbers
list_of_coordinates_y = list(6.7, 3, 12, 45.555, 692)

G=nx.MultiGraph()
for i in range(len(list_of_coordinates_x)):
    G.add_node(i, x = list_of_coordinates_x[i], y = list_of_coordinates_y[i])
    if i > 0:
       G.add_edge(i-1,i)

nx.draw(G,node_size=10,with_labels=False)
但是我用这种方法得到的所有图看起来像是随机放置在平面上的节点集。如何通过图像上的坐标固定它们?我如何使用自己的1680x1050.jpg文件来实现这一点? 在使用了问题第一部分的答案后,我得到了以下图表:

但我想把它放在这样一张照片上:


为此,您需要将节点位置设置为属性,只需使用占位符作为节点名称。因此,一种方法可以是按节点出现的顺序枚举节点,并遵循与您相同的逻辑,但将压缩后的坐标元素添加为
pos
属性:

x = [4.5 , 420, 67, 620, 68]
y = [6.7, 68, 56, 231, 380]

G = nx.DiGraph()
coords = list(zip(x,y))
for node, coo in enumerate(coords, start=1):
    G.add_node(node, pos=coo)
    if node<len(coords):
        G.add_edge(node, node+1)

为了在现有图像上重叠图形,必须确保它们共享相同的
范围。这是很好的解释:

分层多个图像时,图像需要具有相同的 范围。这并不意味着它们需要有相同的形状,但是 它们都需要渲染到由 xmin,xmax,ymin,ymax

为此,您可以在一定程度上对图形坐标和图像强制执行。此值将取决于图像大小,因此必须根据图像的实际大小调整图形和图像的范围。我将使用
klearn.datasets.load\u sample\u image
中的示例图像作为示例,但是对于您自己的图像,您可以使用
matplotlib.image.imread('my\u image.jpg')
加载它

从sklearn.dataset导入加载样本图像
img=加载样本图像('flower.jpg')
x=[4.54206762068]
y=[6.7,68,56,231,380]
y_lim,x_lim=img.shape[:-1]
范围=0,x_lim,0,y_lim
G=nx.DiGraph()
坐标=列表(zip(x,y))
对于节点,枚举中的coo(coords,start=1):
G.add_节点(节点,pos=coo)

如果是节点,设置节点之间的边要遵循什么标准?仅仅是连续的节点?@yatu我在具有连续索引的节点之间创建它们。只是因为我的节点代表了绘图和边上某个移动点的坐标——它们之间在时间上的转换。“压缩坐标”是什么?它只是在列表上使用
zip
zip
将多个iterable聚合为元组。因此,在这种情况下,它将给出
(x0,y0),(x1,y1),…
@ArseniiI很抱歉,但我在这里叠加:
pos={node:attr['pos']对于node,attr in nodes}
此时发生了什么?
attr['pos']
是什么意思?希望这更像你想要的@Arseniioh,非常感谢,它看起来很棒)我要用它
nodes = G.nodes(data=True)
pos = {node:attr['pos'] for node, attr in nodes}
plt.figure(figsize=(12,5))
nx.draw(G,
        nodelist=nodelist,
        pos=pos,
        node_size=500,
        node_color='orange')
from sklearn.datasets import load_sample_image
img = load_sample_image('flower.jpg')

x = [4.5 , 420, 67, 620, 68]
y = [6.7, 68, 56, 231, 380]

y_lim, x_lim = img.shape[:-1]
extent = 0, x_lim, 0, y_lim

G = nx.DiGraph()
coords = list(zip(x,y))
for node, coo in enumerate(coords, start=1):
    G.add_node(node, pos=coo)
    if node<len(coords):
        G.add_edge(node, node+1)

fig = plt.figure(frameon=False, figsize=(10,19))

plt.imshow(img, extent=extent, interpolation='nearest')

nodes = G.nodes(data=True)
pos = {node:attr['pos'] for node, attr in nodes}
nx.draw(G,
        nodelist=nodelist,
        pos=pos,
        node_size=200,
        edge_color='w',
        width=4,
        extent=extent,
        node_color='orange',
       interpolation='nearest')
plt.show()