@@ -28,8 +28,8 @@ Next we define a function for drawing a graph on an Matplotlib axis. We set the
2828
2929.. code-block :: python
3030
31- def plot_betweenness (g , ax ):
32-
31+ def plot_betweenness (g , fig , ax ):
32+
3333 # Calculate vertex betweenness and scale it to be between 0.0 and 1.0
3434 vertex_betweenness = g.betweenness()
3535 edge_betweenness = g.edge_betweenness()
@@ -39,16 +39,26 @@ Next we define a function for drawing a graph on an Matplotlib axis. We set the
3939 print (f " edges: { min (edge_betweenness)} - { max (edge_betweenness)} " )
4040
4141 # Define color bars
42- cmap1 = LinearSegmentedColormap.from_list(" vertex_cmap" , [" mediumpurple " , " indigo" ])
42+ cmap1 = LinearSegmentedColormap.from_list(" vertex_cmap" , [" pink " , " indigo" ])
4343 cmap2 = LinearSegmentedColormap.from_list(" edge_cmap" , [" lightblue" , " midnightblue" ])
4444
45+ # Create normalising objects to scale the color bars correctly
4546 norm1 = Normalize()
4647 norm1.autoscale(vertex_betweenness)
4748 norm2 = Normalize()
4849 norm2.autoscale(edge_betweenness)
4950
50- plt.colorbar(ScalarMappable(norm = norm1, cmap = cmap1), ax = ax)
51- plt.colorbar(ScalarMappable(norm = norm2, cmap = cmap2), ax = ax)
51+ # Create new axes for color bars
52+ cax1 = fig.add_axes([ax.get_position().x0, ax.get_position().y0- 0.05 , ax.get_position().width, 0.03 ], in_layout = True )
53+ cax2 = fig.add_axes([ax.get_position().x0, ax.get_position().y0- 0.20 , ax.get_position().width, 0.03 ], in_layout = True )
54+
55+ plt.colorbar(ScalarMappable(norm = norm1, cmap = cmap1), cax = cax1, orientation = " horizontal" )
56+ plt.colorbar(ScalarMappable(norm = norm2, cmap = cmap2), cax = cax2, orientation = " horizontal" )
57+
58+ # Add labels for color bars
59+ mid = (ax.get_position().x0 + ax.get_position().x1) / 2
60+ fig.text(mid, 0.3 , ' Vertex Betweenness' , ha = ' center' )
61+ fig.text(mid, 0.15 , ' Edge Betweenness' , ha = ' center' )
5262
5363 # Plot graph
5464 g.vs[" color" ] = [cmap1(betweenness) for betweenness in scaled_vertex_betweenness]
@@ -74,18 +84,17 @@ Finally, we call our function with the two graphs:
7484 g2 = ig.Graph.Watts_Strogatz(dim = 1 , size = 150 , nei = 2 , p = 0.1 )
7585
7686 # Plot the graph
77- fig, axs = plt.subplots(1 , 2 , figsize = (10 , 5 ))
78- plot_betweenness(g1, axs[0 ])
79- plot_betweenness(g2, axs[1 ])
87+ fig, axs = plt.subplots(1 , 2 , figsize = (10 , 8 ))
88+ plt.subplots_adjust(bottom = 0.3 )
89+ plot_betweenness(g1, fig, axs[0 ])
90+ plot_betweenness(g2, fig, axs[1 ])
8091
8192 # Add "a" and "b" labels for panels
8293 fig.text(0.05 , 0.9 , ' a' , va = ' top' )
8394 fig.text(0.55 , 0.9 , ' b' , va = ' top' )
84-
95+
8596 plt.show()
8697
87-
88-
8998 The final output graphs are as follows:
9099
91100.. figure :: ./figures/betweenness.png
0 commit comments