I'm facing trouble adding a legend based on the edge colors as I am new to python and networkX. I have an excel sheet that feeds the model where one of the edge attributes is Color, indicating the color of the edge (it's yellow if it's truck, blue for ship, etc.). However, my code is not properly executing the legend logic right. The legend is shown for another figure.
I tried using nx.draw_networkx_edges but I could not figure it out.
This is the code:
fig, ax = plt.subplots(figsize=(10, 5))
plt.title('Transportation Network', size=15)
#nx.draw_networkx_labels(GR, pos_dict)
nx.draw(g, pos=node_positions, edge_color=edge_colors, node_size=40, node_color='black', ax=ax)
plt.xlabel('Time ( 1 epoch= 15 mins)')
plt.ylabel("Cities")
ax.set_axis_on()
ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True )
plt.locator_params(axis="y", nbins=3)
plt.locator_params(axis="x", nbins=30)
ax.set_yticks([10,20,30])
ax.set_yticklabels(['DB','AD','AA'])
plt.show()
plt.plot(edgelist["Color"], "Yellow", label="Truck")
plt.plot(edgelist["Color"], "Green", label="Train")
plt.plot(edgelist["Color"], "Blue", label="Ship")
plt.legend()
CodePudding user response: