OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

create legend in networkX

  • Thread starter Thread starter user1717931
  • Start date Start date
U

user1717931

Guest
There are many ways to create legend (way too many to search). I am able to plot a graph with node-IDs as labels and color the nodes based on their "type" ('cell-phone' or 'tablet'). But, I want a legend telling me that color_1 -> cellphone and color_2 -> tablet.

The below code automatically assigns colors. I want a legend corresponding to that auto assigned colors.

Code:
def plot_graph_3(nxg, y):
    print(f'\nNXG Nodes: {nxg.nodes(data=True)}')
    node_ids = [item[0] for item in nxg.nodes(data=True)]
    print(f'\nNode IDs: {node_ids}')                # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    node_labels = y.tolist()
    print(f'\nNXG Labels: {node_labels}')           # [0, 0, 0, 1, 0, 0, 0, 0, 1, 0]
    label_dict = {
        0: "cell phone",
        1: "tablet"
    }

    plt.figure(figsize=(12, 12))
    plt.axis('off')
    nx.draw_networkx(nxg,
                     pos=nx.spring_layout(nxg, seed=0),
                     with_labels=True,
                     node_size=800,
                     node_color=y,
                     cmap="hsv",
                     vmin=-2,
                     vmax=3,
                     width=0.8,
                     edge_color="grey",
                     font_size=14
                     )

    plt.savefig('../output/graph.png')

my current output graph
<p>There are many ways to create legend (way too many to search). I am able to plot a graph with node-IDs as labels and color the nodes based on their "type" ('cell-phone' or 'tablet'). But, I want a legend telling me that color_1 -> cellphone and color_2 -> tablet.</p>
<p>The below code automatically assigns colors. I want a legend corresponding to that auto assigned colors.</p>
<pre class="lang-py prettyprint-override"><code>
def plot_graph_3(nxg, y):
print(f'\nNXG Nodes: {nxg.nodes(data=True)}')
node_ids = [item[0] for item in nxg.nodes(data=True)]
print(f'\nNode IDs: {node_ids}') # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
node_labels = y.tolist()
print(f'\nNXG Labels: {node_labels}') # [0, 0, 0, 1, 0, 0, 0, 0, 1, 0]
label_dict = {
0: "cell phone",
1: "tablet"
}

plt.figure(figsize=(12, 12))
plt.axis('off')
nx.draw_networkx(nxg,
pos=nx.spring_layout(nxg, seed=0),
with_labels=True,
node_size=800,
node_color=y,
cmap="hsv",
vmin=-2,
vmax=3,
width=0.8,
edge_color="grey",
font_size=14
)

plt.savefig('../output/graph.png')
</code></pre>
<p><a href="https://i.sstatic.net/JjruWL2C.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/JjruWL2C.png" alt="my current output graph" /></a></p>
 

Latest posts

Top