Draw Matplotlib¶
-
jgrapht.drawing.draw_matplotlib.
draw
(g, positions=None, ax=None, **kwds)[source]¶ Draw a graph using Matplotlib.
Draws the graph as a simple representation with no vertex labels or edge labels and using the full Matplotlib figure area and no axis labels by default. See draw_jgrapht() for more full-featured drawing that allows title, axis labels etc.
- Parameters
g (
Graph
) – graphpositions (dict, optional) – vertices positions
kwargs (dict) – additional arguments to pass through
ax (Matplotlib Axes object, optional) – draw the graph in the specified Matplotlib axes
Examples
>>> import matplotlib.pyplot as plt >>> g = jgrapht.create_graph(directed=False, weighted=True) >>> for i in range(0,5):g.add_vertex() >>> e1 = g.add_edge(0, 1) >>> e2 = g.add_edge(0, 2) >>> e3 = g.add_edge(0, 3) >>> e4 = g.add_edge(0, 4) >>> drawing.draw(g, positions=drawing.layout(g, name="random")) >>> plt.show()
-
jgrapht.drawing.draw_matplotlib.
draw_circular
(g, area=0, 0, 10, 10, radius=5, vertex_comparator_cb=None, axis=True, **kwargs)[source]¶ Draw the graph g with a circular layout
- Parameters
g (
Graph
) – grapharea (tuple) – the two dimensional area as a tuple (minx, miny, width, height)
axis (bool, optional (default=True)) – whether to draw the axes
radius (double) – radius of the circle
vertex_comparator_cb (function) – a vertex comparator. Should be a function which accepts two vertices v1, v2 and returns -1, 0, 1 depending of whether v1 < v2, v1 == v2, or v1 > v2 in the ordering
kwargs (dict) – additional arguments passed through
- Returns
vertex positions
- Return type
dict
Examples
>>> import matplotlib.pyplot as plt >>> g = jgrapht.create_graph(directed=False, weighted=True) >>> for i in range(0,5):g.add_vertex() >>> e1 = g.add_edge(0, 1) >>> e2 = g.add_edge(0, 2) >>> e3 = g.add_edge(0, 3) >>> e4 = g.add_edge(0, 4) >>> drawing.draw_circular(g) >>> plt.show()
-
jgrapht.drawing.draw_matplotlib.
draw_fruchterman_reingold
(g, area=0, 0, 10, 10, iterations=100, normalization_factor=0.5, seed=None, theta=0.5, tolerance=None, indexed=False, axis=True, **kwargs)[source]¶ Draw the graph g with a Fruchterman-Reingold layout.
- Parameters
g (
Graph
) – grapharea (tuple) – the two dimensional area as a tuple (minx, miny, width, height)
iterations (int) – number of iterations
normalization_factor (double) – normalization factor when calculating optimal distance
seed (integer) – seed for the random number generator. If None the system time is used
theta (double) – parameter for approximation using the Barnes-Hut technique
indexed (bool, optional (default=False)) – whether to use the Barnes-Hut approximation
axis (bool, optional (default=True)) – whether to draw the axes
tolerance – tolerance used when comparing floating point values
kwargs (dict) – additional arguments passed through
- Returns
vertex positions
- Return type
dict
Examples
>>> import matplotlib.pyplot as plt >>> g = jgrapht.create_graph(directed=False, weighted=True) >>> for i in range(0,5):g.add_vertex() >>> e1 = g.add_edge(0, 1) >>> e2 = g.add_edge(0, 2) >>> e3 = g.add_edge(0, 3) >>> e4 = g.add_edge(0, 4) >>> drawing.draw_fruchterman_reingold(g) >>> plt.show()
-
jgrapht.drawing.draw_matplotlib.
draw_jgrapht
(g, positions=None, vertex_labels=None, edge_labels=None, axis=True, **kwargs)[source]¶ Draw the graph g using Matplotlib.
Draw the graph with Matplotlib with options for vertex positions, labels, titles, and many other drawing features. See draw() for simple drawing without labels or axes.
- Parameters
g (
Graph
) – graphpositions (dict, optional) – vertices positions
axis (bool, optional (default=True)) – Draw the axes
vertex_labels (dict, optional (default=None)) – vertex labels
edge_labels (dict, optional (default=None)) – edge labels
kwargs (dict) – additional arguments to pass through
Examples
>>> import matplotlib.pyplot as plt >>> g = jgrapht.create_graph(directed=False) >>> for i in range(0,5):g.add_vertex() >>> e1 = g.add_edge(0, 1) >>> e2 = g.add_edge(0, 2) >>> e3 = g.add_edge(0, 3) >>> e4 = g.add_edge(0, 4) >>> drawing.draw_jgrapht(g, positions=drawing.layout(g, name="random")) >>> plt.show()
-
jgrapht.drawing.draw_matplotlib.
draw_jgrapht_edge_labels
(g, positions, labels=None, draw_edge_weights=False, edge_weight_format='{:.2f}', horizontalalignment='center', verticalalignment='center', edge_fontsize=12, edge_font_color='black', edge_font_weight='normal', edge_font_family='sans-serif', alpha=1, axis=False, bbox={'boxstyle': 'round,pad=0.03', 'ec': 'white', 'fc': 'white'}, ax=None, **kwargs)[source]¶ Draw only the edge labels on the graph g.
If no labels are provided then this method uses the string representation of the edges or the weight if explicitly requested by the parameters. If the parameter labels is a dictionary, then only labels for the contained edges are drawn. If labels is a list then labels for all edges must be provided.
- Parameters
g (
Graph
) – graphpositions (dict) – vertices positions
labels (dict, optional) – edge labels
draw_edge_weights (bool, optional (default=False)) – whether to use edge weights as edge labels
edge_weight_format (str, optional (default=2 decimal points)) – format for the edge weights
edge_fontsize (int, optional (default=12)) – Font size for text labels
edge_font_color (str, optional (default='black')) – Font color string
edge_font_weight (str, optional (default='normal')) – Font weight ( ‘normal’ | ‘bold’ | ‘heavy’ | ‘light’ | ‘ultralight’)
edge_font_family (str, optional (default='sans-serif')) – Font family (‘cursive’, ‘fantasy’, ‘monospace’, ‘sans’, ‘sans serif’, ‘sans-serif’, ‘serif’)
verticalalignment ({'center', 'top', 'bottom', 'baseline', 'center_baseline'}) – Vertical alignment (default=’center’)
horizontalalignment ({'center', 'right', 'left'}) – Horizontal alignment (default=’center’)
alpha (float, optional (default=1.0)) – label transparency
bbox (Matplotlib bbox) – Matplotlib bbox, specify text box shape and colors.
ax (Matplotlib Axes object, optional) – Draw the graph in the specified Matplotlib axes
axis (bool, optional (default=False)) – Draw the axes
kwargs (dict) – See draw_jgrapht
Examples
>>> import matplotlib.pyplot as plt >>> g = jgrapht.create_graph(directed=False, weighted=True) >>> for i in range(0,5):g.add_vertex() >>> e1 = g.add_edge(0, 1) >>> e2 = g.add_edge(0, 2) >>> e3 = g.add_edge(0, 3) >>> e4 = g.add_edge(0, 4) >>> drawing.draw_jgrapht_edge_labels(g, position=drawing.layout(g, name="random")) >>> plt.show()
See also
draw()
,draw_jgrapht()
,draw_jgrapht_vertices()
,draw_jgrapht_edges()
,draw_jgrapht_vertex_labels()
,--------()
-
jgrapht.drawing.draw_matplotlib.
draw_jgrapht_edges
(g, positions, edge_list=None, edge_labels=False, edge_title=None, edge_color='black', edge_cmap=None, edge_linewidth=1.3, line_style='solid', arrow_size=1, arrow_style='-|>', arrow_color='black', arrow_line='-', arrow_head=20, alpha=1, axis=False, connection_style=None, bbox={'boxstyle': 'round,pad=0.03', 'ec': 'white', 'fc': 'white'}, ax=None, **kwargs)[source]¶ Draw the edges of the graph g.
This draws only the edges of the graph g.
- Parameters
g (
Graph
) – graphpositions (dictionary, optional) – vertices positions
edge_list (list, optional (default: edge_list=None)) – Draw only specified edges
edge_color (color or array of colors (default='black')) – Edge color
edge_cmap (list, optional (default:edge_cmap=None | example: edge_cmap =plt.cm.Greens(np.linspace(edge_vmin,edge_vmax,len(g.edges))))) – Colormap for mapping intensities of edges
edge_linewidth (float, optional (default=1.3)) – Line width of edges
line_style (string, optional (default='solid')) – Edge line style (solid|dashed|dotted|dashdot)
arrow_size (int, optional (default=1)) – size of arrow
arrow_style (str, optional (default=’-|>’)) – choose the style of the arrowsheads.(Fancy|Simple|Wedge etc)
arrow_color (color or array of colors (default='black')) – arrow color
arrow_line (string, optional (default='solid')) – Edge line style (solid|dashed|dotted|dashdot)
arrow_head (int, optional (default=20)) – size of arrowhead
alpha (float, optional (default=1.0)) – edge transparency
axis (bool, optional (default=False)) – Draw the axes
edge_title (list, optional (default:None)) – Label for graph legend
edge_labels (bool, optional (default=False)) – draw labels on the edges.
connection_style (str, optional (default=None | example: connection_style="arc3,rad=-0.3")) – Pass the connection_style parameter to create curved arc of rounding radius rad
bbox (Matplotlib bbox) – Matplotlib bbox,specify text box shape and colors.
ax (Matplotlib Axes object, optional) – Draw the graph in the specified Matplotlib axes
kwargs (dict) – additional arguments
Examples
>>> import matplotlib.pyplot as plt >>> g = jgrapht.create_graph(directed=False) >>> for i in range(0,5):g.add_vertex() >>> e1 = g.add_edge(0, 1) >>> e2 = g.add_edge(0, 2) >>> e3 = g.add_edge(0, 3) >>> e4 = g.add_edge(0, 4) >>> drawing.draw_jgrapht_edges(g, positions=drawing.layout(g, name="random")) >>> plt.show()
See also
draw()
,draw_jgrapht()
,draw_jgrapht_vertices()
,draw_jgrapht_vertex_labels()
,draw_jgrapht_edge_labels()
,-----()
-
jgrapht.drawing.draw_matplotlib.
draw_jgrapht_vertex_labels
(g, positions, labels=None, vertex_fontsize=12, vertex_font_color='black', vertex_font_weight='normal', vertex_font_family='sans-serif', horizontalalignment='center', verticalalignment='center', alpha=1, axis=False, bbox={'boxstyle': 'round,pad=0.03', 'ec': 'white', 'fc': 'white'}, ax=None, **kwargs)[source]¶ Draw only the vertex labels on the graph g.
If no labels are provided then this method uses the string representation of the vertices. If the parameter labels is a dictionary, then only labels for the contained vertices are drawn. If labels is a list then labels for all vertices must be provided.
- Parameters
g (
Graph
) – graphpositions (dict) – vertices positions
labels (dict or list, optional) – vertices labels
vertex_fontsize (int, optional (default=12)) – Font size for text labels
vertex_font_color (string, optional (default='black')) – Font color string
vertex_font_weight (string, optional (default='normal')) – Font weight ( ‘normal’ | ‘bold’ | ‘heavy’ | ‘light’ | ‘ultralight’)
vertex_font_family (string, optional (default='sans-serif')) – Font family (‘cursive’, ‘fantasy’, ‘monospace’, ‘sans’, ‘sans serif’, ‘sans-serif’, ‘serif’)
verticalalignment ({'center', 'top', 'bottom', 'baseline', 'center_baseline'}) – Vertical alignment (default=’center’)
horizontalalignment ({'center', 'right', 'left'}) – Horizontal alignment (default=’center’)
alpha (float, optional (default=1.0)) – label transparency
axis (bool, optional (default=False)) – Draw the axes
ax (Matplotlib Axes object, optional) – Draw the graph in the specified Matplotlib axes
bbox – Matplotlib bbox,specify text box shape and colors.
kwargs (dict) – additional arguments
Examples
>>> import matplotlib.pyplot as plt >>> g = jgrapht.create_graph(directed=False, weighted=True) >>> for i in range(0,5):g.add_vertex() >>> e1 = g.add_edge(0, 1) >>> e2 = g.add_edge(0, 2) >>> e3 = g.add_edge(0, 3) >>> e4 = g.add_edge(0, 4) >>> drawing.draw_jgrapht_vertex_labels(g, positions=drawing.layout(g, name="random")) >>> plt.show()
-
jgrapht.drawing.draw_matplotlib.
draw_jgrapht_vertices
(g, positions, vertex_list=None, axis=False, vertex_linewidths=1.0, vertex_title=None, vertex_size=450, vertex_color='green', vertex_cmap=None, vmin=None, vmax=None, vertex_shape='o', vertex_edge_color='face', alpha=1, ax=None, **kwargs)[source]¶ Draw only the vertices of the graph g.
- Parameters
g (
Graph
) – graphpositions (dict, optional) – vertices positions
vertex_list (list, optional (default: vertex_list=None)) – Draw only the vertices in this list
axis (bool, optional (default=False)) – Draw the axes
vertex_linewidths (float,(default:1.0)) – Line width of symbol border
vertex_title (list, optional (default:None)) – Label for graph legend
vertex_size (scalar or array, optional (default=450)) – Size of vertices
vertex_color (color or array of colors (default='green')) – vertex color
vertex_cmap (Matplotlib colormap, optional (default=None | example:vertex_cmap=plt.cm.Greens)) – Colormap for mapping intensities of vertices
vmin (float, optional (default=None)) – Minimum for vertex colormap scaling
vmax (float, optional (default=None)) – Maximum for vertex colormap scaling positions
vertex_shape (string, optional (default='o')) – The shape of the vertex
vertex_edge_color (string, optional (default='face')) – color the edge of vertex
alpha (float, optional (default=1.0)) – The vertex transparency
ax (Matplotlib Axes object, optional) – Draw the graph in the specified Matplotlib axes
kwargs (dict) – Additional arguments to pass through
Examples
>>> import matplotlib.pyplot as plt >>> g = jgrapht.create_graph(directed=False) >>> for i in range(0,5):g.add_vertex() >>> e1 = g.add_edge(0, 1) >>> e2 = g.add_edge(0, 2) >>> e3 = g.add_edge(0, 3) >>> e4 = g.add_edge(0, 4) >>> drawing.draw_jgrapht_vertices(g, positions=drawing.layout(g, name="random")) >>> plt.show()
-
jgrapht.drawing.draw_matplotlib.
draw_random
(g, area=0, 0, 10, 10, seed=None, axis=True, **kwargs)[source]¶ Draw the graph g with a random layout.
- Parameters
g (
Graph
) – grapharea (tuple) – the two dimensional area as a tuple (minx, miny, width, height)
seed (integer) – seed for the random number generator. If None the system time is used
axis (bool, optional (default=True)) – whether to draw the axes
kwargs (dict) – additional arguments passed through
- Returns
vertex positions
- Return type
dict
Examples
>>> import matplotlib.pyplot as plt >>> g = jgrapht.create_graph(directed=False, weighted=True) >>> for i in range(0,5):g.add_vertex() >>> e1 = g.add_edge(0, 1) >>> e2 = g.add_edge(0, 2) >>> e3 = g.add_edge(0, 3) >>> e4 = g.add_edge(0, 4) >>> drawing.draw_random(g) >>> plt.show()
-
jgrapht.drawing.draw_matplotlib.
layout
(g, name=None, area=0, 0, 10, 10, **kwargs)[source]¶ Compute the positions of vertices for a particular layout.
- Parameters
g (
Graph
) – the graph to drawname (str) – circular|random|fruchterman_reingold|fruchterman_reingold_indexed
area (tuple) – the two dimensional area as a tuple (minx, miny, width, height)
kwargs (dict) – additional arguments passed through
- Returns
vertex positions
- Return type
dict
Examples
>>> import matplotlib.pyplot as plt >>> g = jgrapht.create_graph(directed=False, weighted=True) >>> for i in range(0,5):g.add_vertex() >>> e1 = g.add_edge(0, 1) >>> e2 = g.add_edge(0, 2) >>> e3 = g.add_edge(0, 3) >>> e4 = g.add_edge(0, 4) >>> position = drawing.layout(g, seed=10, name="random") >>> drawing.draw_jgrapht(g, position=position) >>> plt.show()