使用坐标在图像上重叠图形
Overlap graph on image using coordinates
我有两个坐标列表:第一个是 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 文件呢?
使用问题第一部分的答案后,我得到了这张图:
但是我想把它放在这样的图片上:
为此,您需要将节点位置设置为属性,并仅使用占位符作为节点名称。因此,一种方法可能是 enumerate
按节点出现的顺序排列节点,并遵循与您相同的逻辑,但将压缩坐标元素添加为 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)
然后你可以创建一个字典作为 node:(x,y)
这是 pos
in nx.draw
所期望的格式,并且节点以这种方式位于指定的坐标上:
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')
为了在现有图像上重叠图表,您必须确保它们共享相同的 extent
。这很好地解释了 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
为此,您可以对图形坐标和图像进行一定程度的强制处理。该值将取决于图像大小,因此您必须将图形和图像的范围调整为图像的实际大小。我将使用 klearn.datasets.load_sample_image
中的示例图像作为示例,但对于您自己的图像,您可以使用 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()
我有两个坐标列表:第一个是 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 文件呢? 使用问题第一部分的答案后,我得到了这张图:
但是我想把它放在这样的图片上:
为此,您需要将节点位置设置为属性,并仅使用占位符作为节点名称。因此,一种方法可能是 enumerate
按节点出现的顺序排列节点,并遵循与您相同的逻辑,但将压缩坐标元素添加为 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)
然后你可以创建一个字典作为 node:(x,y)
这是 pos
in nx.draw
所期望的格式,并且节点以这种方式位于指定的坐标上:
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')
为了在现有图像上重叠图表,您必须确保它们共享相同的 extent
。这很好地解释了 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
为此,您可以对图形坐标和图像进行一定程度的强制处理。该值将取决于图像大小,因此您必须将图形和图像的范围调整为图像的实际大小。我将使用 klearn.datasets.load_sample_image
中的示例图像作为示例,但对于您自己的图像,您可以使用 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()