Skip to content

Commit 3801725

Browse files
committed
Add tutorial for configuration and visual style
1 parent 9ea92c3 commit 3801725

7 files changed

Lines changed: 175 additions & 2 deletions

File tree

doc/source/gallery.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
.. _gallery:
44

5-
========
5+
=======
66
Gallery
7-
========
7+
=======
88

99
This page contains short examples showcasing the functionality of |igraph|:
1010

@@ -16,6 +16,8 @@ This page contains short examples showcasing the functionality of |igraph|:
1616
- :ref:`tutorials-maxflow`
1717
- :ref:`tutorials-shortest-paths`
1818
- :ref:`tutorials-cliques`
19+
- :ref:`tutorials-configuration`
20+
- :ref:`tutorials-visual-style`
1921
- :ref:`tutorials-online-user-actions`
2022
- :ref:`tutorials-visualize-communities`
2123

@@ -32,5 +34,7 @@ This page contains short examples showcasing the functionality of |igraph|:
3234
tutorials/ring_animation/ring_animation
3335
tutorials/shortest_paths/shortest_paths
3436
tutorials/visualize_cliques/visualize_cliques
37+
tutorials/configuration/configuration
38+
tutorials/visual_style/visual_style
3539
tutorials/online_user_actions/online_user_actions
3640
tutorials/visualize_communities/visualize_communities
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import igraph as ig
2+
import matplotlib.pyplot as plt
3+
import math
4+
import random
5+
6+
# Get the configuration instance
7+
config = ig.Configuration().instance()
8+
9+
# Set configuration variables
10+
config["general.verbose"] = True
11+
config["plotting.backend"] = "matplotlib"
12+
config["plotting.layout"] = "fruchterman_reingold"
13+
config["plotting.palette"] = "heat"
14+
15+
# Generate a graph
16+
random.seed(1)
17+
g = ig.Graph.Barabasi(n=100, m=1)
18+
19+
# Calculate colors between 0-255 for all nodes
20+
betweenness = g.betweenness()
21+
colors = [math.floor(i * 255 / max(betweenness)) for i in betweenness]
22+
23+
# Plot the graph
24+
ig.plot(g, vertex_color=colors, vertex_size=1, edge_width=0.3)
25+
plt.show()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.. include:: ../../include/global.rst
2+
3+
.. _tutorials-configuration:
4+
5+
======================
6+
Configuration Instance
7+
======================
8+
9+
This example shows how to use |igraph|'s `configuration instance <https://igraph.org/python/doc/api/igraph.configuration.Configuration.html>`_ to set default |igraph| settings. This is useful for setting global settings so that they don't need to be explicitly stated at the beginning of every |igraph| project you work on.
10+
11+
First we define the default plotting backend, layout, and color palette, and save them. By default, ``config.save()`` will save files to ``~/.igraphrc`` on Linux and Max OS X systems, or in ``C:\Documents and Settings\username\.igraphrc`` for Windows systems.
12+
13+
.. code-block:: python
14+
15+
import igraph as ig
16+
17+
# Get the configuration instance
18+
config = ig.Configuration().instance()
19+
20+
# Set configuration variables
21+
config["general.verbose"] = True
22+
config["plotting.backend"] = "matplotlib"
23+
config["plotting.layout"] = "fruchterman_reingold"
24+
config["plotting.palette"] = "rainbow"
25+
26+
# Save configuration to ~/.igraphrc
27+
config.save()
28+
29+
This script only needs to be run once, and can then be deleted. Afterwards any time you use |igraph|, it will read the config from the saved file and use them as the defaults. For example:
30+
31+
.. code-block:: python
32+
33+
import igraph as ig
34+
import matplotlib.pyplot as plt
35+
import random
36+
37+
# Generate a graph
38+
random.seed(1)
39+
g = ig.Graph.Barabasi(n=100, m=1)
40+
41+
# Calculate a color value between 0-200 for all nodes
42+
betweenness = g.betweenness()
43+
colors = [int(i * 200 / max(betweenness)) for i in betweenness]
44+
45+
# Plot the graph
46+
ig.plot(g, vertex_color=colors, vertex_size=1, edge_width=0.3)
47+
plt.show()
48+
49+
Note that we do not never explicitly state the backend, layout or palette, yet the final plots look like this:
50+
51+
.. figure:: ./figures/configuration.png
52+
:alt: A 100 node graph colored to show betweenness
53+
:align: center
54+
55+
Graph colored based on each node's betweenness centrality measure.
56+
57+
The full list of config settings can be found `here <https://igraph.org/python/doc/api/igraph.configuration.Configuration.html>`_.
58+
59+
.. note::
60+
61+
- Note that you can specify your own location by passing in a filepath to ``config.save``, e.g. ``config.save("./path/to/config/file")``. You can then load it later using ``ig.Configuration().instance().load("./path/to/config/file")``
62+
- If you want an efficient way to set the visual style between individual graphs (such as vertex sizes, colors, layout etc.) check out :ref:`tutorials-visual-style`.
43.6 KB
Loading
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import igraph as ig
2+
import matplotlib.pyplot as plt
3+
import math
4+
import random
5+
6+
# Configure visual style
7+
visual_style = {
8+
"edge_width": 0.3,
9+
"vertex_size": 1,
10+
"palette": "heat",
11+
"layout": "fruchterman_reingold"
12+
}
13+
14+
# Generate graphs
15+
random.seed(1)
16+
g1 = ig.Graph.Barabasi(n=100, m=1)
17+
g2 = ig.Graph.Barabasi(n=100, m=1)
18+
19+
# Calculate colors between 0-255 for all nodes
20+
betweenness1 = g1.betweenness()
21+
betweenness2 = g2.betweenness()
22+
colors1 = [int(i * 255 / max(betweenness1)) for i in betweenness1]
23+
colors2 = [int(i * 255 / max(betweenness2)) for i in betweenness2]
24+
25+
# Plot the graphs
26+
fig, axs = plt.subplots(1, 2)
27+
ig.plot(g1, target=axs[0], vertex_color=colors1, **visual_style)
28+
ig.plot(g2, target=axs[1], vertex_color=colors2, **visual_style)
29+
plt.show()
30+
62 KB
Loading
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
.. include:: ../../include/global.rst
2+
3+
.. _tutorials-visual-style:
4+
5+
=================================================
6+
Making Multiple Graphs with the Same Visual Style
7+
=================================================
8+
9+
This example shows how to use dictionary unpacking in order to easily use the same visual style across multiple graphs. This is a quick and easy way to quickly share a single visual style across multiple graphs, without having to copy and paste each of the individual attributes over and over again for each graph you plot.
10+
11+
.. code-block:: python
12+
13+
import igraph as ig
14+
import matplotlib.pyplot as plt
15+
import math
16+
import random
17+
18+
# Configure visual style for use in both graphs
19+
visual_style = {
20+
"edge_width": 0.3,
21+
"vertex_size": 1,
22+
"palette": "heat",
23+
"layout": "fruchterman_reingold"
24+
}
25+
26+
# Generate graphs
27+
random.seed(1)
28+
g1 = ig.Graph.Barabasi(n=100, m=1)
29+
g2 = ig.Graph.Barabasi(n=100, m=1)
30+
31+
# Calculate colors between 0-255 for all nodes
32+
betweenness1 = g1.betweenness()
33+
betweenness2 = g2.betweenness()
34+
colors1 = [int(i * 255 / max(betweenness1)) for i in betweenness1]
35+
colors2 = [int(i * 255 / max(betweenness2)) for i in betweenness2]
36+
37+
# Plot the graphs, using the same predefined visual style for both
38+
fig, axs = plt.subplots(1, 2)
39+
ig.plot(g1, target=axs[0], vertex_color=colors1, **visual_style)
40+
ig.plot(g2, target=axs[1], vertex_color=colors2, **visual_style)
41+
plt.show()
42+
43+
The plots looks like this:
44+
45+
.. figure:: ./figures/visual_style.png
46+
:alt: Two graphs plotted using the same palette and layout algorithm
47+
:align: center
48+
49+
Two graphs using the same palette and layout algorithm.
50+
51+
.. note::
52+
If you would like to set global defaults, for example, always using the Matplotlib plotting backend, or using a particular color palette by default, you can use |igraph|'s `configuration instance <https://igraph.org/python/doc/api/igraph.configuration.Configuration.html>`_. A quick example on how to use it can be found here: :ref:`tutorials-configuration`

0 commit comments

Comments
 (0)