Skip to content

Commit 2cbb597

Browse files
authored
Merge pull request #485 from Gomango999/tutorial-config
Configuration Instance + Visual Style Tutorials
2 parents c4cf96f + 1cbe4a8 commit 2cbb597

File tree

8 files changed

+181
-7
lines changed

8 files changed

+181
-7
lines changed

doc/source/gallery.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,47 @@
22

33
.. _gallery:
44

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

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

1111
- :ref:`tutorials-quickstart`
12+
- :ref:`tutorials-articulation-points`
1213
- :ref:`tutorials-betweenness`
1314
- :ref:`tutorials-bipartite-matching`
1415
- :ref:`tutorials-bipartite-matching-maxflow`
16+
- :ref:`tutorials-bridges`
1517
- :ref:`tutorials-cliques`
18+
- :ref:`tutorials-configuration`
1619
- :ref:`tutorials-maxflow`
1720
- :ref:`tutorials-random`
1821
- :ref:`tutorials-ring-animation`
1922
- :ref:`tutorials-shortest-paths`
2023
- :ref:`tutorials-online-user-actions`
2124
- :ref:`tutorials-topological-sort`
25+
- :ref:`tutorials-visual-style`
2226
- :ref:`tutorials-visualize-communities`
23-
- :ref:`tutorials-articulation-points`
24-
- :ref:`tutorials-bridges`
2527

2628

2729
.. toctree::
2830
:maxdepth: 2
2931
:hidden:
3032

3133
tutorials/quickstart/quickstart
34+
tutorials/articulation_points/articulation_points
3235
tutorials/betweenness/betweenness
3336
tutorials/bipartite_matching/bipartite_matching
3437
tutorials/bipartite_matching_maxflow/bipartite_matching_maxflow
38+
tutorials/bridges/bridges
39+
tutorials/configuration/configuration
3540
tutorials/erdos_renyi/erdos_renyi
3641
tutorials/maxflow/maxflow
42+
tutorials/online_user_actions/online_user_actions
3743
tutorials/ring_animation/ring_animation
3844
tutorials/shortest_paths/shortest_paths
3945
tutorials/topological_sort/topological_sort
40-
tutorials/online_user_actions/online_user_actions
46+
tutorials/visual_style/visual_style
4147
tutorials/visualize_cliques/visualize_cliques
4248
tutorials/visualize_communities/visualize_communities
43-
tutorials/articulation_points/articulation_points
44-
tutorials/bridges/bridges
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import igraph as ig
2+
3+
# Set configuration variables
4+
ig.config["plotting.backend"] = "matplotlib"
5+
ig.config["plotting.layout"] = "fruchterman_reingold"
6+
ig.config["plotting.palette"] = "rainbow"
7+
8+
# Save configuration to ~/.igraphrc
9+
ig.config.save()
10+
11+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import igraph as ig
2+
import matplotlib.pyplot as plt
3+
import random
4+
5+
# Generate a graph
6+
random.seed(1)
7+
g = ig.Graph.Barabasi(n=100, m=1)
8+
9+
# Calculate a color value between 0-200 for all nodes
10+
betweenness = g.betweenness()
11+
colors = [int(i * 200 / max(betweenness)) for i in betweenness]
12+
13+
# Plot the graph
14+
ig.plot(g, vertex_color=colors, vertex_size=1, edge_width=0.3)
15+
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, ``ig.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+
# Set configuration variables
18+
ig.config["plotting.backend"] = "matplotlib"
19+
ig.config["plotting.layout"] = "fruchterman_reingold"
20+
ig.config["plotting.palette"] = "rainbow"
21+
22+
# Save configuration to ~/.igraphrc
23+
ig.config.save()
24+
25+
This script only needs to be run once (to store the new config options into the ``.igraphrc`` file). Whenever you use |igraph| and this file exists, |igraph| will read its content and use those options as defaults. For example:
26+
27+
.. code-block:: python
28+
29+
import igraph as ig
30+
import matplotlib.pyplot as plt
31+
import random
32+
33+
# Generate a graph
34+
random.seed(1)
35+
g = ig.Graph.Barabasi(n=100, m=1)
36+
37+
# Calculate a color value between 0-200 for all nodes
38+
betweenness = g.betweenness()
39+
colors = [int(i * 200 / max(betweenness)) for i in betweenness]
40+
41+
# Plot the graph
42+
ig.plot(g, vertex_color=colors, vertex_size=1, edge_width=0.3)
43+
plt.show()
44+
45+
Note that we do not never explicitly state the backend, layout or palette, yet the final plots look like this:
46+
47+
.. figure:: ./figures/configuration.png
48+
:alt: A 100 node graph colored to show betweenness
49+
:align: center
50+
51+
Graph colored based on each node's betweenness centrality measure.
52+
53+
The full list of config settings can be found `here <https://igraph.org/python/doc/api/igraph.configuration.Configuration.html>`_.
54+
55+
.. note::
56+
57+
You can have multiple config files: specify each location via ``ig.config.save("./path/to/config/file")``. To load a specific config, import igraph and then call ``ig.config.load("./path/to/config/file")``
58+
59+
60+
.. note::
61+
62+
To use a consistent style between individual plots (e.g. 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)