使用坐标在图像上重叠图形 [英] Overlap graph on image using coordinates

查看:51
本文介绍了使用坐标在图像上重叠图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

将 networkx 导入为 nxlist_of_coordinates_x = list(4.5, 4, 67, 578, 68) #随机数list_of_coordinates_y = 列表(6.7、3、12、45.555、692)G=nx.MultiGraph()对于我在范围内(len(list_of_coordinates_x)):G.add_node(i, x = list_of_coordinates_x[i], y = list_of_coordinates_y[i])如果我>0:G.add_edge(i-1,i)nx.draw(G,node_size=10,with_labels=False)

但是我通过这种方式获得的所有图形看起来像是随机放置在平面上的节点集.我怎样才能通过我在图像上的坐标来固定它们?我该如何使用我自己的 1680x1050 .jpg 文件呢?使用问题第一部分的答案后,我得到了这张图:

但我想把它放在这样的图片上:

解决方案

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

x = [4.5, 420, 67, 620, 68]y = [6.7, 68, 56, 231, 380]G = nx.DiGraph()坐标 = 列表(zip(x,y))对于节点,枚举中的 coo(coords, start=1):G.add_node(节点,pos=coo)如果节点

然后你可以创建一个字典作为 node:(x,y) 这是 nx.draw 中的 pos 期望的格式,并且节点以这种方式位于指定的坐标上:

nodes = G.nodes(data=True)pos = {node:attr['pos'] 用于节点,节点中的 attr}plt.figure(figsize=(12,5))nx.draw(G,节点列表=节点列表,位置=位置,节点大小=500,node_color='橙色')

为了在现有图像上重叠图形,您必须确保它们共享相同的范围.这在

I have two lists of coordinates: first for x-coordinates and second for y-coordinates. I trying to use them as nodes of a graph.

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)

But the all graphs that I have by this way looks like sets of nodes put on the plane randomly. How can I fixate them by my coordinates on the image? And how can I use my own 1680x1050 .jpg file for that? After using the answer on the first part of the question, I have this graph:

But I want to put it on a picture like this:

解决方案

For that you need to set the node positions as attributes, and just use a placeholder as node name. So one approach could be to just enumerate the nodes in the order they appear, and follow the same logic as yours, but adding the zipped coordinate elements as pos attributes:

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)

Then you could create a dictionary as node:(x,y) which is the format that pos in nx.draw is expecting, and the nodes are in this way located on the specified coordinates:

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')

In order to overlap the graph on an existing image you have to make sure that they both share the same extent. This is nicely explained here:

When layering multiple images, the images need to have the same extent. This does not mean they need to have the same shape, but they both need to render to the same coordinate system determined by xmin, xmax, ymin, ymax

In order to do so, you can enforce a certain extent to both the graph coordinates and the image. This value will depend on the image size, so you'll have to adjust the extent of both the graph and image to the actual size of the image. I'll use a sample image from klearn.datasets.load_sample_image as an example, but for your own image you can just load it with matplotlib.image.imread('my_image.jpg').

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()

这篇关于使用坐标在图像上重叠图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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