NetworkX - How to change the shape of the node?

17,939

Solution 1

I can suggest to have a bbox around the label instead of changing shape of the node. In this case node will "contain the name" inside the box and to acheive this you need specify bbox parameters as dictionary

nx.draw(G, pos=pos, with_labels=True,  node_shape="s",  node_color="none", bbox=dict(facecolor="skyblue", edgecolor='black', boxstyle='round,pad=0.2'))

You may also consider this solution here

Solution 2

You can draw nodes and their labels with the following code:

nx.draw_networkx_nodes(G, pos, node_size=600, node_color='w', alpha=0.4, node_shape='d')
nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif')

For a complete example you can look at the code of the networkx gallery here.

Edit: To fit the name inside the node, you have to play with the node size and font size.

Proof:

example

Share:
17,939

Related videos on Youtube

DavidK
Author by

DavidK

Data Scientist @ Paris

Updated on June 15, 2022

Comments

  • DavidK
    DavidK 12 months

    I know that we can choose the shape from so^>v<dph8.

    Is there a way to modify the shape of a node so that it contains the name of the node ?

    I'm interested by custom shapes (or that can have a size that adapts to the text it contains).

    • d33tah
      d33tah about 8 years
      Why the weird hyperlink name?
    • DavidK
      DavidK about 8 years
      This is what we can give to node_shape : s for "square", o for "circle", ...
  • DavidK
    DavidK about 8 years
    Thanks, but the name is outside the node shape, is there a way to have it inside ?
  • Kirell
    Kirell about 8 years
    Change the size of the node and the font size.
  • DavidK
    DavidK about 8 years
    Is it possible to specify width and height for nodes ? (in case of a square)
  • Kirell
    Kirell about 8 years
    It is a square not a rectangle. If you want to draw a custom shape I suggest to look at the source code of networkx which is just calling matplotlib functions. It should be pretty simple to change the shape.
  • Soren
    Soren about 5 years
    I agree, shape and color of the node should be internal properties. So, it is not confused when plotting.
  • BND
    BND about 4 years
    Is it possible to specify a shape for each node ?

Related