Python 在网络图上绘制示例图像
我使用Plotly显示网络图,并尝试显示属于特定数据点的示例图像(每个数据点是雕塑的64x64亮度贴图)。我有两个问题:Python 在网络图上绘制示例图像,python,numpy,plotly,networkx,Python,Numpy,Plotly,Networkx,我使用Plotly显示网络图,并尝试显示属于特定数据点的示例图像(每个数据点是雕塑的64x64亮度贴图)。我有两个问题: 我使用数据点坐标来定位图像,但它们没有对齐。我尝试使用xanchor/yanchor来修复它,但没有成功 虽然我正在绘制100幅图像,但实际上只有少数几幅是可见的。如果我放大可视化,我可以看到更多的图片,但我想强制Plotly显示所有图片。我尝试了layer='over',但没有成功 如果有一种方法可以将图像与实际数据点链接起来,那就太好了,但我不知道怎么做 def绘图图(G
xanchor/yanchor
来修复它,但没有成功layer='over'
,但没有成功如果坐标==无:
坐标=nx.图纸.弹簧布置图(G,重量=无)
边缘_x=[]
边缘_y=[]
对于G.边()中的边:
x0,y0=坐标[边[0]]
x1,y1=坐标[边[1]]
边x.延伸([x0,x1])
边缘y.延伸([y0,y1])
边缘跟踪=去散点(x=边缘跟踪x,
y=边缘y,
线条=笔迹(宽度=0.5,颜色='#888'),
hoverinfo='none',
模式行'
)
节点_x=[]
节点_y=[]
对于G.nodes()中的节点:
x、 y=坐标[节点]
节点x.append(x)
节点_y.追加(y)
颜色条(厚度=15,标题=KNN密度,'xanchor='left',标题侧='right')
标记器属性=dict(显示比例=真,
colorscale='YlGnBu',
反向刻度=真,
颜色=[],大小=10,
colorbar=colorbar\u属性,
线条宽度=2)
node_trace=go.Scatter(x=node_x,y=node_y,mode='markers',hoverinfo='text',marker=marker\u attrs)
节点_邻接=[]
节点_text=[]
对于节点,枚举中的邻接(G.adjacency()):
node_adjaccines.append(len(adjaccines[1]))
node_text.append('K-最近邻:'+str(len(邻接[1]))
node_trace.marker.color=节点_邻接
node_trace.text=节点_text
fig=go.Figure(数据=[边缘跟踪,节点跟踪],
布局=go.layout(
标题='
{}'。格式(绘图标题),
titlefont_尺寸=18,
showlegend=False,
hovermode='closest',
保证金=dict(b=20,l=5,r=5,t=40),
xaxis=dict(showgrid=False,zeroline=False,showticklabels=False),
yaxis=dict(showgrid=False,zeroline=False,showticklabels=False))
)
num_images=100
num_faces=dataset.shape[0]
sample\u images=np.random.choice(num\u faces,num\u images,replace=False)
greys=cm.get\u cmap('greys\u r')
对于示例_图像中的索引:
greyscale=np。沿_轴应用_(灰色,0,数据集[索引])。重塑((64,64,4))*255
greyscale=greyscale.astype(np.uint8)
im=pilim.fromarray(灰度)
图添加布局图(dict)(
source=im,
x=坐标[索引][0],
y=坐标[索引][1],
sizex=0.03,
sizey=0.03,
“上一层”
))
返回图
这就是我得到的
第一节
由于您没有提供完全可复制的示例,因此很难直接解决您的问题。但我确实有一个建议,就是在上面的例子的基础上构建并使用同样在中使用的图像。该图像在列表中存储多次,因此您应该能够轻松地用对其他图像的引用替换这些图像。我正在从fig['data'][1]['X']
和fig['data'][1]['Y']
抓取X,Y
坐标,但这也应该可以很容易地调整到其他来源
绘图1:
import plotly.graph_objects as go
import networkx as nx
G = nx.random_geometric_graph(200, 0.125)
edge_x = []
edge_y = []
for edge in G.edges():
x0, y0 = G.nodes[edge[0]]['pos']
x1, y1 = G.nodes[edge[1]]['pos']
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
edge_trace = go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color='#888'),
hoverinfo='none',
mode='lines')
node_x = []
node_y = []
for node in G.nodes():
x, y = G.nodes[node]['pos']
node_x.append(x)
node_y.append(y)
node_trace = go.Scatter(
x=node_x, y=node_y,
mode='markers',
hoverinfo='text',
marker=dict(
showscale=True,
colorscale='YlGnBu',
reversescale=True,
color=[],
size=10,
colorbar=dict(
thickness=15,
title='Node Connections',
xanchor='left',
titleside='right'
),
line_width=2))
node_adjacencies = []
node_text = []
for node, adjacencies in enumerate(G.adjacency()):
node_adjacencies.append(len(adjacencies[1]))
node_text.append('# of connections: '+str(len(adjacencies[1])))
node_trace.marker.color = node_adjacencies
node_trace.text = node_text
fig = go.Figure(data=[edge_trace, node_trace])
# sample images
sample_images=["https://raw.githubusercontent.com/michaelbabyn/plot_data/master/benzene.png"]*len(fig['data'][1]['x'])
xVals = fig['data'][1]['x']
yVals = fig['data'][1]['y']
for i in range(0, len(xVals)):
fig.add_layout_image(dict(
source=sample_images[i],
x=xVals[i],
y=yVals[i],
xref="x",
yref="y",
#sizex=0.03,
#sizey=0.03,
#layer='above'
sizex=0.1,
sizey=0.1,
#sizing="stretch",
opacity=0.5,
layer="below"
))
fig.show()
绘图2:放大
现在,如果我正确理解了您的挑战,那么此图符合您关于所有三个标准的要求:
import plotly.graph_objects as go
import networkx as nx
G = nx.random_geometric_graph(200, 0.125)
edge_x = []
edge_y = []
for edge in G.edges():
x0, y0 = G.nodes[edge[0]]['pos']
x1, y1 = G.nodes[edge[1]]['pos']
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
edge_trace = go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color='#888'),
hoverinfo='none',
mode='lines')
node_x = []
node_y = []
for node in G.nodes():
x, y = G.nodes[node]['pos']
node_x.append(x)
node_y.append(y)
node_trace = go.Scatter(
x=node_x, y=node_y,
mode='markers',
hoverinfo='text',
marker=dict(
showscale=True,
colorscale='YlGnBu',
reversescale=True,
color=[],
size=10,
colorbar=dict(
thickness=15,
title='Node Connections',
xanchor='left',
titleside='right'
),
line_width=2))
node_adjacencies = []
node_text = []
for node, adjacencies in enumerate(G.adjacency()):
node_adjacencies.append(len(adjacencies[1]))
node_text.append('# of connections: '+str(len(adjacencies[1])))
node_trace.marker.color = node_adjacencies
node_trace.text = node_text
fig = go.Figure(data=[edge_trace, node_trace])
# sample images
sample_images=["https://raw.githubusercontent.com/michaelbabyn/plot_data/master/benzene.png"]*len(fig['data'][1]['x'])
xVals = fig['data'][1]['x']
yVals = fig['data'][1]['y']
for i in range(0, len(xVals)):
fig.add_layout_image(dict(
source=sample_images[i],
x=xVals[i],
y=yVals[i],
xref="x",
yref="y",
#sizex=0.03,
#sizey=0.03,
#layer='above'
sizex=0.1,
sizey=0.1,
#sizing="stretch",
opacity=0.5,
layer="below"
))
fig.show()
有没有可能举行一次会议?或者尝试简化这个问题,看看是否可以解决?非常感谢!是的,这对我帮助很大。很抱歉这么早就接受了late@JuanDavid没问题。很乐意帮忙!