13

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).

Germán Faller
  • 546
  • 7
  • 15
DavidK
  • 2,495
  • 3
  • 23
  • 38
  • 1
    Why the weird hyperlink name? – d33tah May 20 '15 at 09:34
  • 1
    This is what we can give to node_shape : s for "square", o for "circle", ... – DavidK May 20 '15 at 09:47
  • @DavidK: Please do not edit your question to ask something different. Ask a new question instead. – Matt Jun 18 '15 at 19:29
  • @Matt, I didn't ask something different, I already knew what've been answered. My question was how to make a custom shape. (Or how to do to have a shape that contains the text but without overlapping) – DavidK Jun 19 '15 at 08:56
  • The question is "How to change the shape". It's not about "resize". It's about custom shapes. I've already given in my question "so^>v – DavidK Jun 19 '15 at 09:08
  • @DavidK: You edited your question to be narrower than what you initially asked, which invalidated the answers your question had already received. This is unfair on the people who wrote those answers. If you need to narrow the requirements of your ask, ask a new question with those new requirements. – Matt Jun 19 '15 at 09:50
  • @Matt The answer is not what I wanted. Changing the size is the obvious answer, that's not what I wanted and I was clear about that. The goal is to have the best answer to the question, this is not. – DavidK Jun 19 '15 at 10:01
  • @DavidK: There was **nothing** in your original question (http://stackoverflow.com/revisions/30344592/1) which states that you didn't want to change the size, and this is the point I'm trying to make. Your question can stay open, and like it is, but I'm just asking you to bear in mind that fundamentally changing your question is not accepted, in any future questions you may ask. – Matt Jun 19 '15 at 13:38
  • @Matt I've understood your point. I didn't say I don't want to change the size but that's not possible to increase the size with a real graph with many nodes. What I asked was how to have the shape that is good enough to contains nodes labels. Rectangles instead of squares for example. When I receive an answer that does not meet my expectations, what I can do is edit my answer to give more details about what I want to achieve and this allow to get better responses (or allow the user that gave an answer to edit his answer also). – DavidK Jun 19 '15 at 14:25

2 Answers2

18

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

David Koelle
  • 20,726
  • 23
  • 93
  • 130
Yury Wallet
  • 1,474
  • 1
  • 13
  • 24
8

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

Agustín
  • 1,569
  • 1
  • 14
  • 9
Kirell
  • 9,228
  • 4
  • 46
  • 61
  • Thanks, but the name is outside the node shape, is there a way to have it inside ? – DavidK May 20 '15 at 10:03
  • Change the size of the node and the font size. – Kirell May 20 '15 at 10:33
  • Is it possible to specify width and height for nodes ? (in case of a square) – DavidK May 20 '15 at 10:56
  • 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. – Kirell May 20 '15 at 12:21
  • I agree, shape and color of the node should be internal properties. So, it is not confused when plotting. – Soerendip May 23 '18 at 00:00
  • 3
    Is it possible to specify a shape for each node ? – BND Mar 13 '19 at 13:32