Skip to main content

Convert networkX graphs into Ipe drawings

Project description

networkX to Ipe

Convert networkX graphs into Ipe drawings.

1. Create a graph

You can style the different elements of your drawing by adding attributes to your networkX graph object.

X and Y attributes are required and used to fix the position of nodes in the graph. Additional attributes can be added but are not required. All properties of the drawing are customized in the settings.ini file.

import networkx as nx
from nx2ipe.nx2ipe import IpeConverter

G = nx.Graph()

G.add_node(0, Label='a', X='0', Y='50', Glyph = 'mark/cross(sx)')
G.add_node(1, Label='asdf', X='50', Y='100', Stroke = 'purple', Fill = 'yellow')
G.add_node(2, Label='v_1', X='50', Y='0', Size = '5.0')
G.add_node(3, Label='v_{12}', X='200', Y='50', Layer = 'layer1')

G.add_edge(0,1, Label='sas')
G.add_edge(0,2, Pen='fat', Stroke='green')
G.add_edge(1,3)
G.add_edge(2,3)

2. Draw the graph

converter = IpeConverter()
converter.createDrawing(G, 'undirected.xml')

It is also possible to add a path to a custom global settings file. Furthermore, a list of paths to style sheets can be added if you want to add references to custom style properties (e.g. colors, glyps). The basic Ipe style sheet is automatically loaded.

converter = IpeConverter(settings_path = 'path/to/costum/settings.ini', styles = ['mycolors.xml', 'myglyphs.xml'])

3. More examples

Different approaches exist to assign color to vertices and edges. Generally, one can assign the color names that are defined in the basic ipe style sheet. Furthermore, it is also possible to reference colors by their names in defined style sheets. Finally, it is possible to assign Hex colors which are automatically converted to a style sheet.

G = nx.DiGraph()

G.add_node(0, Label='v_{1}', X='0', Y='100', Color='green')
G.add_node(1, Label='v_{2}', X='100', Y='200')
G.add_node(2, Label='v_{3}', X='100', Y='0')
G.add_node(3, Label='v_{4}', X='200', Y='100', Color='#000000', Fill='#99FF00')

G.add_edge(0,1, Label = '5', Color='red')
G.add_edge(0,2, Label = '10', Color='#FF0099')
G.add_edge(1,3, Label = '20', Color='#F09')
G.add_edge(2,3, Label = '15')
G.add_edge(3,2, Label = '8')

converter.createDrawing(G, 'colored_graph.xml')

Directed graphs can be visualized by setting the _GRAPH_DIRECTED property to True.

G = nx.DiGraph()

G.add_node(0, Label='v_{1}', X='0', Y='100')
G.add_node(1, Label='v_{2}', X='100', Y='200')
G.add_node(2, Label='v_{3}', X='100', Y='0')
G.add_node(3, Label='v_{4}', X='200', Y='100')

G.add_edge(0,1, Label = '5')
G.add_edge(0,2, Label = '10')
G.add_edge(1,3, Label = '20')
G.add_edge(2,3, Label = '15')
G.add_edge(3,2, Label = '8')

converter._options._GRAPH_DIRECTED = True

converter.createDrawing(G, 'directed.xml')

There is also a draw style that uses arcs instead of straight lines to connect adjacent nodes.

converter._options._DRAWING_USE_ARCS = True
converter._options._DRAWING_UNBOUND = False

converter.createDrawing(G, 'directed_arcs.xml')

The graph can also be drawn on a specifically sized canvas. In this case 2 * A4. The graph is automatically scaled to fit while maintaining its aspect ratio.

converter._options._DRAWING_WIDTH = 2 * 596
converter._options._DRAWING_HEIGHT = 2 * 843
converter._options._DRAWING_MARGIN = 100

converter.createDrawing(G, 'directed_arcs_A3.xml')

It is also possible to snap nodes to the closest Ipe grid point when setting a grid size and the snap property.

converter._options._DRAWING_WIDTH = 596 / 2
converter._options._DRAWING_HEIGHT = 843 / 2
converter._options._DRAWING_GRID_SIZE = 56
converter._options._DRAWING_SNAP_TO_GRID = True

converter.createDrawing(G, 'snap_to_grid.xml')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nx2ipe-0.0.7.tar.gz (11.3 kB view details)

Uploaded Source

File details

Details for the file nx2ipe-0.0.7.tar.gz.

File metadata

  • Download URL: nx2ipe-0.0.7.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for nx2ipe-0.0.7.tar.gz
Algorithm Hash digest
SHA256 c764bac2bfbe215ef789a1dfbb3b07eb2e23f242b52c072a7bee1a959d2b1c1c
MD5 bbde298fd326b01c6ebfaf01748e18f7
BLAKE2b-256 7d4d9c0a04b71a7e209472e2a96667ff50239a0082af9194ab7ae4365e19abb4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page