0

I am drawing a network and I want node sizes to be dependent on specific values for each node, but these values can sometimes be negative. When I input the vector of values as the node_size argument of the draw_networkx_nodes function (and multiply by *300), I get the following output;

Output

"Negative" node sizes are not displayed, the problem being (I think) that the negative values are taken 'as such' for node sizes, and hence considered as "very low".

I want the "scale" of the node sizes to "start" at the lowest value (which will be negative) of the node size vector

For inspiration regarding the desired output : Screenshot of an article with desired result

Sample of code ;

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
nodesize_test=np.array([-0.1,0.8,0.3])
adj_matrix = np.array([[0.        , 0.72105342, 0.76595857],
       [0.72105342, 0.        , 0.85544021],
       [0.76595857, 0.85544021, 0.        ]])
G= nx.Graph(adj_matrix)
nodelist= list(G.nodes()) #Retrieve list of nodes
pos = nx.spring_layout(G)
ec = nx.draw_networkx_edges(G, pos)
nc = nx.draw_networkx_nodes(G, pos, nodelist=nodelist,node_size=nodesize_test*300)
plt.show()

PS. Scaling the vector to [0,1] seems not to be an acceptable solution as it impacts the relative node sizes (i.e. the quotient between two node sizes) and hence the information displayed. For instance, after rescaling using this, the ratio between 2nd and 3rd node sizes shifts from 2.67 (unscaled) to 2.25 (scaled). The information is not the same anymore: node 2 should not be 2.25x bigger but rather 2.67x bigger than node 3.

  • Can you clarify what you mean by relative node sizes? I think you mean that the new node sizes are non-negative and sum to 1, not that the quotient of any two new sizes remains the same as those two for the original sizes. If you want the first one, you can subtract the min and divide by the original range as seen [here](https://stackoverflow.com/a/5295202/13716967) (if min != max). Then divide these by the new sum of items. If you want the second one, I don't think there's a transformation to non-negatives that would preserve a ratio of -8 like you have between your first two node sizes. – cookesd Feb 23 '22 at 01:30
  • Hello @cookesd, thx for your input. My question needed clarification indeed. You are correct, the solution you're offering changes the ratio between two node sizes which is not desirable. For instance, after rescaling to [0,1], the ratio between 2nd and 3rd node sizes shifts from 2.67 (unscaled) to 2.25 (scaled). The information is not the same anymore: node 2 should not be 2.25x bigger but rather 2.67x bigger than node 3. I edited the question. – rvanderveken Feb 23 '22 at 08:34
  • Gotcha, I still think you need to rethink the ratio restriction, because no two non-negative numbers will be able to have a negative ratio like you have between -.1 and -.8. – cookesd Feb 23 '22 at 14:33

0 Answers0