.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/visual_style.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_visual_style.py: .. _tutorials-visual-style: Visual styling =========================== This example shows how to change the visual style of network plots. .. GENERATED FROM PYTHON SOURCE LINES 9-14 .. code-block:: Python import igraph as ig import matplotlib.pyplot as plt import random .. GENERATED FROM PYTHON SOURCE LINES 15-17 To configure the visual style of a plot, we can create a dictionary with the various setting we want to customize: .. GENERATED FROM PYTHON SOURCE LINES 17-24 .. code-block:: Python visual_style = { "edge_width": 0.3, "vertex_size": 15, "palette": "heat", "layout": "fruchterman_reingold", } .. GENERATED FROM PYTHON SOURCE LINES 25-26 Let's see it in action! First, we generate four random graphs: .. GENERATED FROM PYTHON SOURCE LINES 26-29 .. code-block:: Python random.seed(1) gs = [ig.Graph.Barabasi(n=30, m=1) for i in range(4)] .. GENERATED FROM PYTHON SOURCE LINES 30-32 Then, we calculate a color colors between 0-255 for all nodes, e.g. using betweenness just as an example: .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: Python betweenness = [g.betweenness() for g in gs] colors = [[int(i * 255 / max(btw)) for i in btw] for btw in betweenness] .. GENERATED FROM PYTHON SOURCE LINES 36-37 Finally, we can plot the graphs using the same visual style for all graphs: .. GENERATED FROM PYTHON SOURCE LINES 37-44 .. code-block:: Python fig, axs = plt.subplots(2, 2) axs = axs.ravel() for g, color, ax in zip(gs, colors, axs): ig.plot(g, target=ax, vertex_color=color, **visual_style) plt.show() .. image-sg:: /tutorials/images/sphx_glr_visual_style_001.png :alt: visual style :srcset: /tutorials/images/sphx_glr_visual_style_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-51 .. note:: 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 :class:`igraph.configuration.Configuration`. A quick example on how to use it can be found here: :ref:`tutorials-configuration`. .. GENERATED FROM PYTHON SOURCE LINES 53-57 In the matplotlib backend, igraph creates a special container :class:`igraph.drawing.matplotlib.graph.GraphArtist` which is a matplotlib Artist and the first child of the target Axes. That object can be used to customize the plot appearance after the initial drawing, e.g.: .. GENERATED FROM PYTHON SOURCE LINES 57-67 .. code-block:: Python g = ig.Graph.Barabasi(n=30, m=1) fig, ax = plt.subplots() ig.plot(g, target=ax) artist = ax.get_children()[0] # Option 1: artist.set(vertex_color="blue") # Option 2: artist.set_vertex_color("blue") plt.show() .. image-sg:: /tutorials/images/sphx_glr_visual_style_002.png :alt: visual style :srcset: /tutorials/images/sphx_glr_visual_style_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. note:: The :meth:`igraph.drawing.matplotlib.graph.GraphArtist.set` method can be used to change multiple properties at once and is generally more efficient than multiple calls to specific ``artist.set_...`` methods. .. GENERATED FROM PYTHON SOURCE LINES 74-76 In the matplotlib backend, you can also specify the size of self-loops, either as a number or a sequence of numbers, e.g.: .. GENERATED FROM PYTHON SOURCE LINES 76-92 .. code-block:: Python g = ig.Graph(n=5) g.add_edge(2, 3) g.add_edge(0, 0) g.add_edge(1, 1) fig, ax = plt.subplots() ig.plot( g, target=ax, vertex_size=20, edge_loop_size=[ 0, # ignored, the first edge is not a loop 30, # loop for vertex 0 80, # loop for vertex 1 ], ) plt.show() .. image-sg:: /tutorials/images/sphx_glr_visual_style_003.png :alt: visual style :srcset: /tutorials/images/sphx_glr_visual_style_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.122 seconds) .. _sphx_glr_download_tutorials_visual_style.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: visual_style.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: visual_style.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: visual_style.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_