You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/antora/modules/ROOT/pages/customizing.adoc
+62-23Lines changed: 62 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,35 +49,35 @@ for node in VG.nodes:
49
49
node.caption = str(caption)
50
50
----
51
51
52
-
== Coloring nodes
52
+
== Coloring the graph
53
53
54
-
Nodes can be colored directly by providing them with a color field, upon creation.
55
-
This can for example be done by passing a color as a string to the `color` parameter of the
56
-
link:{api-docs-uri}/node[Node] object.
54
+
Nodes and relationships can be colored directly by providing them with a color field, upon creation.
55
+
This can be done by passing a color as a string to the `color` parameter of the
56
+
link:{api-docs-uri}/node[Node] or link:{api-docs-uri}/relationship[Relationship] object.
57
57
58
-
Alternatively, you can color nodes based on a field or property of the nodes after a `VisualizationGraph` object has been
58
+
Alternatively, you can color nodes or relationships based on a field or property after a `VisualizationGraph` object has been
59
59
created.
60
60
61
-
=== The `color_nodes` method
61
+
=== The `color_nodes` and `color_relationships` methods
62
62
63
-
By calling the link:{api-docs-uri}/visualization-graph/#neo4j_viz.VisualizationGraph.color_nodes[`neo4j_viz.VisualizationGraph.color_nodes()`] method, you can color nodes based on a node field or property (members of the `Node.properties` map).
63
+
By calling the link:{api-docs-uri}/visualization-graph/#neo4j_viz.VisualizationGraph.color_nodes[`neo4j_viz.VisualizationGraph.color_nodes()`] or link:{api-docs-uri}/visualization-graph/#neo4j_viz.VisualizationGraph.color_relationships[`neo4j_viz.VisualizationGraph.color_relationships()`] method, you can color nodes or relationships based on a field or property (members of the `Node.properties` or `Relationship.properties` map).
64
64
65
-
It's possible to color the nodes based on a discrete or continuous color space (see link:{api-docs-uri}/colors[`ColorSpace`]).
66
-
In the discrete case, a new color from the `colors` provided is assigned to each unique value of the node field/property.
65
+
It's possible to color them based on a discrete or continuous color space (see link:{api-docs-uri}/colors[`ColorSpace`]).
66
+
In the discrete case, a new color from the `colors` provided is assigned to each unique value of the field/property.
67
67
In the continuous case, the `colors` should be a list of colors representing a range that are used to
68
-
create a gradient of colors based on the values of the node field/property.
68
+
create a gradient of colors based on the values of the field/property.
69
69
70
70
By default the Neo4j color palette, that works for both light and dark mode, will be used.
71
71
If you want to use a different color palette, you can pass a dictionary or iterable of colors as the `colors`
72
72
parameter.
73
73
A color value can for example be either strings like "blue", or hexadecimal color codes like "#FF0000", or even a tuple of RGB values like (255, 0, 255).
74
74
75
-
If some nodes already have a `color` set, you can choose whether or not to override it with the `override`
75
+
If some nodes or relationships already have a `color` set, you can choose whether or not to override it with the `override`
76
76
parameter.
77
77
78
78
==== By discrete color space
79
79
80
-
To not use the default colors, we can provide a list of custom colors based on the discrete node field "caption" to the `color_nodes` method:
80
+
To not use the default colors, we can provide a list of custom colors based on a discrete field to the coloring methods:
81
81
82
82
[source, python]
83
83
----
@@ -86,9 +86,12 @@ from neo4j_viz.colors import ColorSpace
The full set of allowed values for colors are listed link:https://docs.pydantic.dev/2.0/usage/types/extra_types/color_types/[here].
@@ -100,43 +103,54 @@ Instead of defining your own colors, you could also for example use the color pa
100
103
from palettable.wesanderson import Moonrise1_5
101
104
102
105
# VG is a VisualizationGraph object
103
-
VG.color_nodes(field="caption", Moonrise1_5.colors) # PropertyType.DISCRETE is default
106
+
VG.color_nodes(field="caption", colors=Moonrise1_5.colors) # ColorSpace.DISCRETE is default
104
107
----
105
108
106
-
In theses cases, all nodes with the same caption will get the same color.
109
+
In theses cases, all items with the same field value will get the same color.
107
110
108
-
If there are fewer colors than unique values for the node `field` or `property` provided, the colors will be reused in a cycle.
111
+
If there are fewer colors than unique values for the `field` or `property` provided, the colors will be reused in a cycle.
109
112
To avoid that, you could use a larger palette or extend one with additional colors.
110
113
Refer to the link:{tutorials-docs-uri}/gds-example[Visualizing Neo4j Graph Data Science (GDS) Graphs tutorial] for an example on how
111
114
to do the latter.
112
115
113
116
==== By continuous color space
114
117
115
-
To not use the default colors, we can provide a list of custom colors representing a range to the `color_nodes` method:
118
+
To not use the default colors, we can provide a list of custom colors representing a range:
116
119
117
120
[source, python]
118
121
----
119
-
from neo4j_viz.colors import PropertyType
122
+
from neo4j_viz.colors import ColorSpace
120
123
121
124
# VG is a VisualizationGraph object
122
125
VG.color_nodes(
123
126
property="centrality_score",
124
-
[(255, 0, 0), (191, 64, 0), (128, 128, 0), (64, 191, 0), (0, 255, 0)] # From red to green
127
+
colors=[(255, 0, 0), (191, 64, 0), (128, 128, 0), (64, 191, 0), (0, 255, 0)], # From red to green
128
+
color_space=ColorSpace.CONTINUOUS
129
+
)
130
+
131
+
# Color relationships based on a "weight" property
132
+
VG.color_relationships(
133
+
property="weight",
134
+
colors=["#E0E0E0", "#000000"], # From light grey to black
125
135
color_space=ColorSpace.CONTINUOUS
126
136
)
127
137
----
128
138
129
-
In this case, the nodes will be colored based on the value of the "centrality_score" property, with the lowest values being colored red and the highest values being colored green.
139
+
In these cases, the nodes or relationships will be colored based on the value of the property, with the lowest values being colored with the first color and the highest values being colored with the last color.
130
140
Since we only provided five colors in the range, the granularity of the gradient will be limited to five steps.
131
141
132
142
`palettable` and `matplotlib` are great libraries to use to create custom color gradients.
133
143
134
-
== Sizing nodes
144
+
== Sizing nodes and relationships
135
145
136
146
Nodes can be given a size directly by providing them with a size field, upon creation.
137
-
This can for example be done by passing a size as an integer to the `size` parameter of the link:{api-docs-uri}/node[Node] object.
147
+
This can be done by passing a size as an integer to the `size` parameter of the link:{api-docs-uri}/node[Node] object.
148
+
138
149
139
-
Alternatively, you can size nodes after a `VisualizationGraph` object has been created.
150
+
Relationships can be given a width directly by providing them with a width field, upon creation.
151
+
This can be done by passing a size as an integer to the `width` parameter of the link:{api-docs-uri}/relationship[Relationship] object.
152
+
153
+
Alternatively, you can resize them after a `VisualizationGraph` object has been created.
Note that means that also the node with ID 42 will be scaled to be between 5 and 20 pixels in size.
168
182
183
+
=== The `resize_relationships` method
184
+
185
+
By calling the link:{api-docs-uri}/visualization-graph/#neo4j_viz.VisualizationGraph.resize_relationships[`neo4j_viz.VisualizationGraph.resize_relationships()`] method, you can resize relationship widths by:
186
+
187
+
* passing new widths as a dictionary `widths`, mapping relationship IDs to widths in pixels, or
188
+
* providing a `property` name of the relationships to use for sizing.
189
+
190
+
In the following example, we resize the relationship with ID "r1" to have a width of 5:
191
+
192
+
[source, python]
193
+
----
194
+
# VG is a VisualizationGraph object
195
+
VG.resize_relationships(widths={"r1": 5})
196
+
----
197
+
198
+
You can also resize relationships based on a property:
199
+
200
+
[source, python]
201
+
----
202
+
# VG is a VisualizationGraph object
203
+
VG.resize_relationships(property="weight")
204
+
----
205
+
206
+
Unlike `resize_nodes`, relationship widths are used as-is and are not scaled to a specific range.
207
+
169
208
== Pinning nodes
170
209
171
210
Nodes can be pinned to their current position in the visualization, so that they will not be moved by the force-directed
0 commit comments