NetworkX - How to change the shape of the node?
Asked Answered
S

2

14

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

Superabound answered 20/5, 2015 at 8:45 Comment(9)
Why the weird hyperlink name?Abortionist
This is what we can give to node_shape : s for "square", o for "circle", ...Superabound
@DavidK: Please do not edit your question to ask something different. Ask a new question instead.Founder
@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)Superabound
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<dph8" to show I already know we can choose from that list. The current answer is not what I was expecting.Superabound
@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.Founder
@Founder 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.Superabound
@DavidK: There was nothing in your original question (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.Founder
@Founder 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).Superabound
S
19

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

Stratagem answered 26/4, 2020 at 12:4 Comment(0)
G
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

Galvez answered 20/5, 2015 at 9:57 Comment(6)
Thanks, but the name is outside the node shape, is there a way to have it inside ?Superabound
Change the size of the node and the font size.Galvez
Is it possible to specify width and height for nodes ? (in case of a square)Superabound
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.Galvez
I agree, shape and color of the node should be internal properties. So, it is not confused when plotting.Overly
Is it possible to specify a shape for each node ?Invalidate

© 2022 - 2024 — McMap. All rights reserved.