GraphWidget#

class graph_tool.draw.GraphWidget(g, pos, vprops=None, eprops=None, vorder=None, eorder=None, nodesfirst=False, update_layout=False, multilevel=False, layout_args={}, layout_K=None, layout_init_step=None, coarse_method=None, sfdp_args=None, display_props=None, display_props_size=11, fit_view=True, fit_view_ink=None, bg_color=None, max_render_time=300, layout_callback=None, key_press_callback=None, highlight_color=None, **kwargs)[git][source]#

Bases: DrawingArea

Interactive GTK+ widget displaying a given graph.

Parameters:
gGraph

Graph to be drawn.

posVertexPropertyMap

Vector-valued vertex property map containing the x and y coordinates of the vertices.

vpropsdict (optional, default: None)

Dictionary with the vertex properties. Individual properties may also be given via the vertex_<prop-name> parameters, where <prop-name> is the name of the property.

epropsdict (optional, default: None)

Dictionary with the edge properties. Individual properties may also be given via the edge_<prop-name> parameters, where <prop-name> is the name of the property.

vorderVertexPropertyMap (optional, default: None)

If provided, defines the relative order in which the vertices are drawn.

eorderEdgePropertyMap (optional, default: None)

If provided, defines the relative order in which the edges are drawn.

nodesfirstbool (optional, default: False)

If True, the vertices are drawn first, otherwise the edges are.

update_layoutbool (optional, default: False)

If True, the layout will be updated dynamically, with hfd_layout().

multilevelbool (optional, default: False)

If True, the layout is obtained hierarchically (parameter hierarchical of hfd_layout()), and the coarse graph of every level of the hierarchy is shown while it is laid out. Otherwise, the given positions are refined directly.

layout_argsdict (optional, default: {})

Additional parameters to be passed to hfd_layout() (e.g. b or rank).

layout_KNone (optional, default: None)

Deprecated and ignored (the scale is set by hfd_layout()).

layout_init_stepNone (optional, default: None)

Deprecated and ignored.

coarse_methodNone (optional, default: None)

Deprecated and ignored.

sfdp_argsNone (optional, default: None)

Deprecated and ignored (use layout_args).

display_propslist of VertexPropertyMap instances (optional, default: None)

List of properties to be displayed when the mouse passes over a vertex.

display_props_sizefloat (optional, default: 11.)

Font size used to display the vertex properties.

fit_viewbool, float, or tuple (optional, default: True)

If True, the layout will be scaled to fit the entire clip region. If a float value is given, it will be interpreted as True, and in addition the viewport will be scaled out by that factor. If a tuple value is given, it should have four values (x, y, w, h) that specify the view in user coordinates.

fit_view_inkbool (optional, default: None)

If True, and fit_view == True the drawing will be performed once to figure out the bounding box, before the actual drawing is made. Otherwise, only the vertex positions will be used for this purpose. If the value is None, then it will be assumed True for networks of size 1,000 nodes or less, otherwise it will be assumed False.

bg_colorstr or sequence (optional, default: None)

Background color. The default is white.

max_render_timeint (optional, default: 300)

Maximum amount of time (in milliseconds) spent rendering portions of the graph.

layout_callbackfunction (optional, default: None)

User-supplied callback to be called whenever the positions of the layout have changed. It needs to have the following signature:

def callback(widget, g, picked, pos, vprops, eprops):
    ...

where widget is the GraphWidget instance, g is the graph being drawn, picked is either a single vertex or a boolean vertex property map representing the vertices currently selected, pos the vertex positions, and vprops and eprops are dictionaries with the vertex and edge properties currently being used by the layout.

key_press_callbackfunction (optional, default: None)

User-supplied callback to be called whenever a key-press event has happened. It needs to have the following signature:

def callback(widget, g, keyval, picked, pos, vprops, eprops):
    ...

where widget is the GraphWidget instance, g is the graph being drawn, keyval is the key id, picked is either a single vertex or a boolean vertex property map representing the vertices currently selected, pos the vertex positions, and vprops and eprops are dictionaries with the vertex and edge properties currently being used by the layout.

highlight_colorstr or sequence (optional, default: None)

Color used to highlight the currently selected node and its incident edges. The default is red.

vertex_*VertexPropertyMap or arbitrary types (optional, default: None)

Parameters following the pattern vertex_<prop-name> specify the vertex property with name <prop-name>, as an alternative to the vprops parameter.

edge_*EdgePropertyMap or arbitrary types (optional, default: None)

Parameters following the pattern edge_<prop-name> specify the edge property with name <prop-name>, as an alternative to the eprops parameter.

**kwargs

Any extra parameters are passed to cairo_draw().

Notes

The graph drawing can be panned by dragging with the middle mouse button pressed, or with the left button while holding the “control” key. The graph may be zoomed by scrolling with the mouse wheel, or equivalent, or with a pinch gesture (if the “shift” key is held while scrolling, the vertex/edge sizes are scaled accordingly). The layout may be rotated by scrolling while holding the “control” key (in finer steps if “shift” is also held), or with a two-finger rotation gesture; with a touchscreen, it may also be panned by dragging with one finger. Pressing the “r” key centers and zooms the layout around the graph. By pressing the “a” key, the current translation, scaling and rotation transformations are applied to the vertex positions themselves, and the transformation matrix is reset (if this is never done, the given position properties are never modified).

Individual vertices may be selected by pressing the left mouse button. The currently selected vertex follows the mouse pointer. To stop the selection, the right mouse button must be pressed. Alternatively, a group of vertices may be selected by holding the “shift” button while the pointer is dragged while pressing the left button. The selected vertices may be moved by dragging the pointer with the left button pressed. They may be rotated by holding the “control” key and scrolling with the mouse. If the key “z” is pressed, the layout is zoomed to fit the selected vertices only.

If the key “s” is pressed, the dynamic spring-block layout is activated, starting from the current positions with a small random jitter. Vertices which are currently selected are not updated. The amplitude of the jitter is doubled at every press, so that pressing the key repeatedly perturbs the layout increasingly. As the layout settles, the amplitude returns to its initial value: its excess over the initial value is proportional to the displacement of the vertices in the last step of the layout algorithm, relative to the largest one since the key was pressed.

Methods

adjust_default_sizes(geometry[, force])

Set the default sizes, according to the window geometry, or to the layout, if the sizes are given in layout units.

angle_changed(gesture, angle, angle_delta)

apply_transform()

Apply current transform matrix to vertex coordinates.

button_press_event(widget, event)

Handle button press.

button_release_event(widget, event)

Handle button release.

cleanup(*args)

Cleanup callbacks.

drag_gesture_begin(gesture, seq)

drag_gesture_end(gesture, seq)

drag_gesture_update(gesture, dx, dy)

draw(da, cr)

Redraw the widget.

fit_to_window([ink, g])

Fit graph to window.

init_picked()

Init picked vertices.

key_press_event(widget, event)

Handle key press.

key_release_event(widget, event)

Handle release event.

layout_callback()

Perform one step of the layout algorithm.

limit_render_time()

Render with a short time limit while the view is being changed, restoring the full limit after two seconds.

motion_notify_event(widget, event)

Handle pointer motion.

pos_from_device(pos[, dist, surface, cr])

Convert a position from the widget space to the graph space.

pos_to_device(pos[, dist, surface, cr])

Convert a position from the graph space to the widget space.

regenerate_surface([reset, complete])

Redraw the graph surface.

reset_layout([hierarchical])

Reset the layout algorithm, which refines the current positions, or lays out the graph from scratch if hierarchical == True.

rotate_begin(gesture, seq)

rotate_end(gesture, seq)

scale_changed(gesture, scale)

scroll_event(widget, event)

Handle scrolling.

update([pos, vprops, eprops, vorder, ...])

update_jitter(X_prev, X)

Update the jitter amplitude according to the displacement of the vertices in a sweep, given their positions before and after it (as 2 x N arrays).

zoom_at(center, zoom)

Zoom the layout by the factor zoom, keeping the device point center fixed.

zoom_begin(gesture, seq)

zoom_end(gesture, seq)

Attributes

adjust_default_sizes(geometry, force=False)[git][source]#

Set the default sizes, according to the window geometry, or to the layout, if the sizes are given in layout units.

angle_changed(gesture, angle, angle_delta)[git][source]#
apply_transform()[git][source]#

Apply current transform matrix to vertex coordinates.

button_press_event(widget, event)[git][source]#

Handle button press.

button_release_event(widget, event)[git][source]#

Handle button release.

cleanup(*args)[git][source]#

Cleanup callbacks. This is called when the widget is destroyed, so that the layout does not keep running after the window is closed.

drag_gesture_begin(gesture, seq)[git][source]#
drag_gesture_end(gesture, seq)[git][source]#
drag_gesture_update(gesture, dx, dy)[git][source]#
draw(da, cr)[git][source]#

Redraw the widget.

fit_to_window(ink=False, g=None)[git][source]#

Fit graph to window.

init_picked()[git][source]#

Init picked vertices.

key_press_event(widget, event)[git][source]#

Handle key press.

key_release_event(widget, event)[git][source]#

Handle release event.

layout_callback()[git][source]#

Perform one step of the layout algorithm.

limit_render_time()[git][source]#

Render with a short time limit while the view is being changed, restoring the full limit after two seconds.

motion_notify_event(widget, event)[git][source]#

Handle pointer motion.

pos_from_device(pos, dist=False, surface=False, cr=None)[git][source]#

Convert a position from the widget space to the graph space.

pos_to_device(pos, dist=False, surface=False, cr=None)[git][source]#

Convert a position from the graph space to the widget space.

regenerate_surface(reset=False, complete=False)[git][source]#

Redraw the graph surface.

reset_layout(hierarchical=False)[git][source]#

Reset the layout algorithm, which refines the current positions, or lays out the graph from scratch if hierarchical == True.

rotate_begin(gesture, seq)[git][source]#
rotate_end(gesture, seq)[git][source]#
scale_changed(gesture, scale)[git][source]#
scroll_event(widget, event)[git][source]#

Handle scrolling.

update(pos=None, vprops=None, eprops=None, vorder=None, eorder=None, nodesfirst=None, display_props=None, fit_view=True, bg_color=None, **kwargs)[git][source]#
update_jitter(X_prev, X)[git][source]#

Update the jitter amplitude according to the displacement of the vertices in a sweep, given their positions before and after it (as 2 x N arrays).

zoom_at(center, zoom)[git][source]#

Zoom the layout by the factor zoom, keeping the device point center fixed.

zoom_begin(gesture, seq)[git][source]#
zoom_end(gesture, seq)[git][source]#
dummy#
g_type_instance#
parent_instance#
priv#
props = <gi._gi.GProps object>#
qdata#
ref_count#
widget#