In order to construct a directed network graph, Plotly's current approach seems to be using annotations. This works when there are few edges and one can manually populate each one through the figure layout, e.g., this example.
But if I'm creating a much more complicated graph, is there a good way to iteratively define the arrow coordinates for all the edges (I can only think of constructing a string and then use eval(), although I know it's bad practice)? (edit: it seems this approach of concatenating iteratively generated dict() definition strings doesn't work -- worked only for one dict() definition)
Edit: adding a code snippet to better illustrate the scenario (with the eval() line commented out for comparison):
import plotly.offline as py
import plotly.graph_objs as go
trace = go.Scatter(
x=[1, 2, 2, 1],
y=[3, 4, 3, 4],
mode='markers',
marker=dict(size=[100, 100, 100, 100])
)
fig = go.Figure(
data=[trace],
layout=go.Layout(
annotations = [
dict(
ax=1, ay=3, axref='x', ayref='y',
x=2, y=4, xref='x', yref='y'
),
# eval("dict(ax=2, ay=3, axref='x', ayref='y', x=1, y=4, xref='x', yref='y')")
]
)
)
py.plot(fig)
I'm open to try other visualization packages as well, if there is a good way in doing this under Bokeh or others.
I'm the author of gravis, an interactive graph visualization package in Python. It recognizes graph objects from several network analysis packages such as NetworkX, igraph or graph-tool.
Here's a minimal example of creating a directed graph with NetworkX and visualizing it with gravis.
import gravis as gv
import networkx as nx
g = nx.DiGraph()
g.add_edges_from([(1, 2), (2, 3), (2, 4), (4, 5), (4, 7), (5, 6), (1, 6), (6, 7)])
gv.d3(g)
Below is a sample of using loop to create arrows in a Plotly graph, which is easily applicable for NetworkX visualization of directed graphs.
import plotly.offline as py
import plotly.graph_objs as go
trace = go.Scatter(
x=[1, 2, 2, 1],
y=[3, 4, 3, 4],
mode='markers',
marker=dict(size=[100, 100, 100, 100])
)
# Edges
x0 = [1, 2]
y0 = [3, 3]
x1 = [2, 1]
y1 = [4, 4]
fig = go.Figure(
data=[trace],
layout=go.Layout(
annotations = [
dict(ax=x0[i], ay=y0[i], axref='x', ayref='y',
x=x1[i], y=y1[i], xref='x', yref='y',
showarrow=True, arrowhead=1,) for i in range(0, len(x0))
]
)
)
py.plot(fig)
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