Skip to content

Commit 1ff1e5b

Browse files
committed
Add labels and improved sizing
1 parent aee3be0 commit 1ff1e5b

3 files changed

Lines changed: 37 additions & 17 deletions

File tree

doc/source/tutorials/betweenness/assets/betweenness.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from matplotlib.colors import LinearSegmentedColormap, Normalize
55
import random
66

7-
def plot_betweenness(g, ax):
7+
def plot_betweenness(g, fig, ax):
88

99
# Calculate vertex betweenness and scale it to be between 0.0 and 1.0
1010
vertex_betweenness = g.betweenness()
@@ -18,13 +18,23 @@ def plot_betweenness(g, ax):
1818
cmap1 = LinearSegmentedColormap.from_list("vertex_cmap", ["pink", "indigo"])
1919
cmap2 = LinearSegmentedColormap.from_list("edge_cmap", ["lightblue", "midnightblue"])
2020

21+
# Create normalising objects to scale the color bars correctly
2122
norm1 = Normalize()
2223
norm1.autoscale(vertex_betweenness)
2324
norm2 = Normalize()
2425
norm2.autoscale(edge_betweenness)
2526

26-
plt.colorbar(ScalarMappable(norm=norm1, cmap=cmap1), ax=ax)
27-
plt.colorbar(ScalarMappable(norm=norm2, cmap=cmap2), ax=ax)
27+
# Create new axes for color bars
28+
cax1 = fig.add_axes([ax.get_position().x0, ax.get_position().y0-0.05, ax.get_position().width, 0.03], in_layout=True)
29+
cax2 = fig.add_axes([ax.get_position().x0, ax.get_position().y0-0.20, ax.get_position().width, 0.03], in_layout=True)
30+
31+
plt.colorbar(ScalarMappable(norm=norm1, cmap=cmap1), cax=cax1, orientation="horizontal")
32+
plt.colorbar(ScalarMappable(norm=norm2, cmap=cmap2), cax=cax2, orientation="horizontal")
33+
34+
# Add labels for color bars
35+
mid = (ax.get_position().x0 + ax.get_position().x1) / 2
36+
fig.text(mid, 0.3, 'Vertex Betweenness', ha='center')
37+
fig.text(mid, 0.15, 'Edge Betweenness', ha='center')
2838

2939
# Plot graph
3040
g.vs["color"] = [cmap1(betweenness) for betweenness in scaled_vertex_betweenness]
@@ -45,9 +55,10 @@ def plot_betweenness(g, ax):
4555
g2 = ig.Graph.Watts_Strogatz(dim=1, size=150, nei=2, p=0.1)
4656

4757
# Plot the graph
48-
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
49-
plot_betweenness(g1, axs[0])
50-
plot_betweenness(g2, axs[1])
58+
fig, axs = plt.subplots(1, 2, figsize=(10, 8))
59+
plt.subplots_adjust(bottom=0.3)
60+
plot_betweenness(g1, fig, axs[0])
61+
plot_betweenness(g2, fig, axs[1])
5162

5263
# Add "a" and "b" labels for panels
5364
fig.text(0.05, 0.9, 'a', va='top')

doc/source/tutorials/betweenness/betweenness.rst

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
-1.18 KB
Loading

0 commit comments

Comments
 (0)