I am using Plotly to display a network graph and I'm trying to display sample images belonging to specific data points (each data point is a 64x64 luminosity map of a sculpture). I have two problems:
xanchor/yanchor to fix it but it didn't work.layer='above' but didn't work.def plot_graph(G, plot_title, dataset, coordinates=None):
if coordinates == None:
coordinates = nx.drawing.spring_layout(G, weight=None)
edge_x = []
edge_y = []
for edge in G.edges():
x0, y0 = coordinates[edge[0]]
x1, y1 = coordinates[edge[1]]
edge_x.extend([x0, x1])
edge_y.extend([y0, y1])
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 = coordinates[node]
node_x.append(x)
node_y.append(y)
colorbar_attrs = dict(thickness=15, title='KNN Density', xanchor='left', titleside='right')
marker_attrs = dict(showscale=True,
colorscale='YlGnBu',
reversescale=True,
color=[], size=10,
colorbar=colorbar_attrs,
line_width=2)
node_trace = go.Scatter(x=node_x, y=node_y, mode='markers', hoverinfo='text', marker=marker_attrs)
node_adjacencies = []
node_text = []
for node, adjacencies in enumerate(G.adjacency()):
node_adjacencies.append(len(adjacencies[1]))
node_text.append('K-Nearest Neighbors: '+str(len(adjacencies[1])))
node_trace.marker.color = node_adjacencies
node_trace.text = node_text
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title='<br> {}'.format(plot_title),
titlefont_size=18,
showlegend=False,
hovermode='closest',
margin=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_images = np.random.choice(num_faces, num_images, replace=False)
greys = cm.get_cmap('Greys_r')
for index in sample_images:
greyscale = np.apply_along_axis(greys, 0, dataset[index]).reshape((64, 64, 4))*255
greyscale = greyscale.astype(np.uint8)
im = pilim.fromarray(greyscale)
fig.add_layout_image(dict(
source=im,
x=coordinates[index][0],
y=coordinates[index][1],
sizex=0.03,
sizey=0.03,
layer='above'
))
return fig
This is what I'm getting

First pitch
Since you haven't provided a fully reproducible example, it's difficult to solve your problem directly. But I do have a suggestion building on the top example from plot.ly/python/network-graphs/ and using the image benze.png that's also used in other plotly examples. The image is stored multiple times in a list, so you should be able to replace those easily with references to other images. I'm grabbing the X, Y coordinates from fig['data'][1]['x'] and fig['data'][1]['y'] but that should be easily adjustable to other sources too.
Plot 1:

Plot 2: Zoomed in

Now, if I'm understanding your challenge correctly, this plot meets your requirements regarding all three criteria:
Please don't hesitate to let me know how this works out for you!
Code:
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With