.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/quickstart.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_quickstart.py: .. _tutorials-quickstart: =========== Quick Start =========== For the eager folks out there, this intro will give you a quick overview of the following operations: - Construct a graph - Set attributes of nodes and edges - Plot a graph using matplotlib - Save the plot as an image - Export and import a graph as a ``.gml`` file To find out more features that igraph has to offer, check out the :ref:`gallery`! .. GENERATED FROM PYTHON SOURCE LINES 18-72 .. image-sg:: /tutorials/images/sphx_glr_quickstart_001.png :alt: quickstart :srcset: /tutorials/images/sphx_glr_quickstart_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/igraph/envs/develop/lib/python3.11/site-packages/igraph/__init__.py:1496: RuntimeWarning: The boolean edge attribute 'married' was converted to numeric. Location: src/io/gml.c:1252 return writer(f, *args, **kwds) | .. code-block:: Python import igraph as ig import matplotlib.pyplot as plt # Construct a graph with 5 vertices n_vertices = 5 edges = [(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (3, 4)] g = ig.Graph(n_vertices, edges) # Set attributes for the graph, nodes, and edges g["title"] = "Small Social Network" g.vs["name"] = [ "Daniel Morillas", "Kathy Archer", "Kyle Ding", "Joshua Walton", "Jana Hoyer", ] g.vs["gender"] = ["M", "F", "F", "M", "F"] g.es["married"] = [False, False, False, False, False, False, False, True] # Set individual attributes g.vs[1]["name"] = "Kathy Morillas" g.es[0]["married"] = True # Plot in matplotlib # Note that attributes can be set globally (e.g. vertex_size), or set individually using arrays (e.g. vertex_color) fig, ax = plt.subplots(figsize=(5, 5)) ig.plot( g, target=ax, layout="circle", # print nodes in a circular layout vertex_size=30, vertex_color=[ "steelblue" if gender == "M" else "salmon" for gender in g.vs["gender"] ], vertex_frame_width=4.0, vertex_frame_color="white", vertex_label=g.vs["name"], vertex_label_size=7.0, edge_width=[2 if married else 1 for married in g.es["married"]], edge_color=["#7142cf" if married else "#AAA" for married in g.es["married"]], ) plt.show() # Save the graph as an image file fig.savefig("social_network.png") fig.savefig("social_network.jpg") fig.savefig("social_network.pdf") # Export and import a graph as a GML file. g.save("social_network.gml") g = ig.load("social_network.gml") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.165 seconds) .. _sphx_glr_download_tutorials_quickstart.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: quickstart.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: quickstart.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: quickstart.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_