Plotting¶
This page covers plotting the Graph object in various matrix forms using
the ragraph.plot module. In particular we will focus on the ragraph.plot.mdm
function and the ragraph.plot.Style object. The
mdm function can be used to quickly generate multi-domain-matrix (MDM) and
dependency-structure-matrix (DSM) figures of your data set. The Style object
allows one to filter the information to be displayed and set the appearance of the figure.
The Ford Climate Control System dataset is used to illustrate the various plot options.
Basic plotting¶
To create a plot, first the import the module and use the plot function as follows:
The mdm function requires two arguments: leafs, which is the list of nodes
to be displayed on the rows and columns of the matrix, and edges, which is the list of edges
(dependencies) to be displayed within the matrix. The figure can be saved to file using the
fig.write_image(filename) function by Plotly.
The figure below shows the resulting matrix. By default, the different edge kinds are displayed
within the matrix. In this case, the dataset only contains the edge kind edge, as shown within the
legend at the bottom right of the figure.
Note that the leafs are automatically sorted following the hierarchical structure found within the
graph. If one would like to display the leafs within the order as provided, one can pass along the
argument sort=False to the mdm function.
In case one wants to create a non-square matrix or a matrix with different nodes on the rows than on
the columns. One can use the Domain Mapping Matrix ragraph.plot.dmm function:
This results in the figure below, in which we now have more columns than rows.
Tuning the axis nodes¶
A big part of your figures content is the ordering of leaf nodes on the axis. By default the leaf nodes you provide will be sorted according to:
- The hierarchy they are subject to.
- The node kind they belong to.
- Whether or not they are a bus node.
- How big their "width" is in terms of leaf nodes (e.g. biggest clusters first).
You can tweak this behavior like so:
Where sort_args are options to be passed to
ragraph.analysis.sequence.axis.
Or you can also disable this behavior altogether and plot the (leaf) nodes as given:
Information filtering¶
The Graph may contain Edge objects of various kinds
to which various labels and weights are attached. By passing along a Style
object to the style argument of the mdm function one can set the information
that is displayed within the matrix.
In the snippet below, the value of the display key of the piemap property of the
Style object is set to be equal to "weight labels".
The snippet and corresponding figure below show the resulting matrix in which the labels of the different weights attached to the edges are displayed as categories within a pie chart.
By changing the value of display to "weights" the matrix changes from a categorical plot to a
numerical plot as shown in the next figure. Here the actual numerical values of the different
weights are shown rather than the weight labels.
By default, all elements that belong to the display category are shown. One can change this by
setting the fields key of the piemap dictionary as shown in the following snippet, which yields
the figure right after it. Here only the numerical values of the weights "spatial", "energy
flow", and "information flow" are displayed.
In all figures shown so far, the wedges displayed within the pie charts are of equal size. You can
change this by setting the value of the mode key of the piemap dictionary to "relative". As a
result, the size of the wedges is scaled following the numerical value attached to it as shown in
the following:
The examples shown in this section are far from exhaustive. Check out the
ragraph.plot.generic.PieMapStyle documentation for all
properties that one can set within the piemap dictionary.
Information highlighting¶
Highlighting certain rows and columns is possible as well. By default, you can highlight rows and
columns by setting the highlight annotation to True on a Node instance:
You can override certain settings via the Style object, too:
That's a lot of overrides fighting for precedence! In short, when you override the
highlight_row_annotation or highlight_col_annotation keys in the piemap specific options, the
PieMap's highlighting of rows or columns will only look for those annotation keys in row or column
Nodes (e.g. the default one is completely ignored). If these override are unset or None, it will
look for the global highlight annotation key. So:
- ragraph.plot.generic.PieMapStyle's row/column annotation key is considered.
- Only if there is no style entry for that, the
   ragraph.plot.generic.Stylehighlight key is considered.
Secondly, the color's precedence is always:
- Annotation key's value if it's a string.
- ragraph.plot.generic.PieMapStylerow/column override color if set.
- ragraph.plot.generic.Styledefault highlight color.
Matrix styling¶
The colors of the fields displayed within the matrix are automatically generated. However, for easy comparison of multiple plots one may want to explicitly set the color, or color map of a field.
In the snippet below the the color of the displayed weight labels are explicitly set by setting the
value of the fields key within the palettes property of the Style
object. Here one can provide a dictionary mapping weight labels to color hex codes. The result
of setting these colors is shown in the following snippet and figure:
Similarly, one can set the colors for numerical values. Here one has to provide a list of colors (a
color map). The ragraph.colors module provides several functions for generating
such color maps.
In the example below, the functions get_diverging_redblue, get_diverging_orangecyan, and
get_diverging_purplegreen are used to set a diverging color map for the displayed weights in the
next figure.
Check out the documentation of the ragraph.plot.generic.Palettes
object to check all options that can be set using the pallettes argument.
Basic customization¶
The DSM literature presents a create variety of matrix based visualizations. These visualizations
often differ with respect to the information displayed within the matrix. The ragraph.plot
module is built upon the open source Plotly library. As
such, one can customize the generated figures.
In the snippet below numbers and squares are added to the diagonal of the matrix. The numbers are
added by adding a scatter trace to the figure using the add_trace method.
Note the row and col arguments of the add_trace method. The plot consists of six components
which are instances of the Tree,
Labels, PieMap, and
Legend objects. These components are placed within a grid
containing two rows and five columns. By setting the row and col arguments one can add the trace
to one of the six plot components.
The squares are added by using the update_layout method to update the shapes property of the
figure. In creating the figure shapes, the arguments xref and yref are set to x9 and y9,
respectively. This ensures that the shapes are positioned with respect to x-axis nine and y-axis
nine. In this case, being the axes of the matrix (piemap).
Advanced customization¶
One can take customization one step further. As the plot components are placed within a grid, one can expand the grid with additional rows and columns filled with custom components.
The snippet below shows a custom plot component class ColorBar, which inherits its properties from
the ragraph.plot.generic.Component class which is the basic
building block for creating compound Plotly figures.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |  | 
The grid layout of this figure was defined within the custom_mdm method. The figure is composed of
three rows and six columns. The first row contains a ColorBar component within the fourth column.
All other positions are empty within. The second row contains six plot components: a
Tree, Labels, a custom
ColorBar, a PieMap, another ColorBar, and a
Legend component. The third row only contains a ColorBar
component in the right position.
With use of the functions Ragraph
ragraph.plot.utils.get_subplots and
ragraph.plot.utils.process_fig the plot components are joined
into a single Plotly figure object.
Note
Matching the scaling of all figures within a grid plot can be quite tricky. The axis of
neighboring plots are automatically linked. Hence one should take care when setting the range of
the axis of custom plot components. The Style.xstep,
Style.ystep, and Style.boxsize
properties of the Style object are particularly important in matching
the scaling of figures.
Edge direction convention¶
Whilst the example matrix of the climate control system is symmetrical, you might have a directed
graph instead, such as the UCAV dataset. By default, we utilize the
IR-FAD convention (Inputs in Rows, Feedback Above the Diagonal). Lets visualize the default first:
If you would like to follow the opposite convention, IC-FBD (Inputs in Columns, Feedback Below the
Diagonal), that is possible using the convention property of the
Style object.
Chord plot¶
If you'd like to make a chord plot, you can do so, powered by the rachord package!
Take a look at ragraph.plot.chord or the relevant chord style
key that reflects the ragraph.plot.generic.ChordStyle options.