0

I am wondering whether it is possible to plot a vertical bar over a 2-dimensional representation of a graph. Say I have a tree and I want to associate with any node a "potential" which can be represented as a vertical bar.

dapias
  • 2,512
  • 3
  • 15
  • 23

2 Answers2

1

NetworkX can do that using the matplotlib drawing tools because the result is a matplotlib figure on which you can use matplotlib to draw anything else you'd like on top of the networkx drawing of the graph.

nx.draw(G)
mpl.plot([xpt, xpt], [ymin, ymax], '--b')
mpl.show()
dschult
  • 184
  • 7
  • Thanks for the matplotlib hint. I understood how to do this because of that. See my own answer – dapias Feb 16 '21 at 14:29
1

This is a minimal example which does what I was looking for (in Python):

import networkx as nx
import random
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

degree = 3
N = 10
g = nx.random_regular_graph(degree, N)


fig = plt.figure(figsize=(10,7))
ax = Axes3D(fig)


for i,j in enumerate(g.edges()):
    x = np.array((positions[j[0]][0], positions[j[1]][0]))
    y = np.array((positions[j[0]][1], positions[j[1]][1]))
    ax.plot(x, y,  c='black', alpha=0.5)


for key, value in positions.items():
    xi = value[0]
    yi = value[1]
    # Scatter plot
    ax.scatter(xi, yi, c= 'red')
    ax.bar3d(xi, yi, 0, 0.01, 0, random.random(), shade=False)
        
    
ax.set_axis_off()

It generates this kind of plot, which can be useful to represent additional information on a graph

graph

dapias
  • 2,512
  • 3
  • 15
  • 23