Skip to content

Commit 0a395bb

Browse files
adamnschFlorentinD
andcommitted
Add docs for coloring by continuous node properties
Co-Authored-By: Florentin Dörre <florentin.dorre@neotechnology.com>
1 parent 77d7d9f commit 0a395bb

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

docs/source/customizing.rst

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,34 @@ The ``color_nodes`` method
3636

3737
By calling the :meth:`neo4j_viz.VisualizationGraph.color_nodes` method, you can color nodes based on a
3838
node property (field).
39-
This method will give a distinct color (if possible) to each unique value of the node ``property`` that you provide as
40-
the first positional argument.
39+
It's possible to color the nodes based on a discrete or continuous property.
40+
In the discrete case, a new color from the ``colors`` provided is assigned to each unique value of the node property.
41+
In the continuous case, the ``colors`` should be a list of colors representing a range that are used to create a gradient of colors based on the values of the node property.
4142

4243
By default the Neo4j color palette that works for both light and dark mode will be used.
4344
If you want to use a different color palette, you can pass a dictionary or iterable of colors as the ``colors``
4445
parameter.
4546
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).
46-
Here is an example of providing a list of custom colors to the ``color_nodes`` method:
47+
48+
If some nodes already have a ``color`` set, you can choose whether or not to override it with the ``override``
49+
parameter.
50+
51+
52+
By discrete node property (field)
53+
*********************************
54+
55+
To not use the default colors, we can provide a list of custom colors based on the discrete node property (field) "caption" to the ``color_nodes`` method:
4756

4857
.. code-block:: python
4958
59+
from neo4j_viz.colors import PropertyType
60+
5061
# VG is a VisualizationGraph object
51-
VG.color_nodes("caption", ["red", "#7fffd4", (255, 255, 255, 0.5), "hsl(270, 60%, 70%)"])
62+
VG.color_nodes(
63+
"caption",
64+
["red", "#7fffd4", (255, 255, 255, 0.5), "hsl(270, 60%, 70%)"],
65+
property_type=PropertyType.DISCRETE
66+
)
5267
5368
The full set of allowed values for colors are listed `here <https://docs.pydantic.dev/2.0/usage/types/extra_types/color_types/>`_.
5469

@@ -60,7 +75,7 @@ this snippet:
6075
from palettable.wesanderson import Moonrise1_5
6176
6277
# VG is a VisualizationGraph object
63-
VG.color_nodes("caption", Moonrise1_5.colors)
78+
VG.color_nodes("caption", Moonrise1_5.colors) # PropertyType.DISCRETE is default
6479
6580
In this case, all nodes with the same caption will get the same color.
6681

@@ -69,12 +84,31 @@ To avoid that, you could use another palette or extend one with additional color
6984
:doc:`Visualizing Neo4j Graph Data Science (GDS) Graphs tutorial <./tutorials/gds-example>` for an example on how
7085
to do the latter.
7186

72-
If some nodes already have a ``color`` set, you can choose whether or not to override it with the ``override``
73-
parameter.
87+
88+
By continuous node property (field)
89+
***********************************
90+
91+
To not use the default colors, we can provide a list of custom colors representing a range to the ``color_nodes`` method:
92+
93+
.. code-block:: python
94+
95+
from neo4j_viz.colors import PropertyType
96+
97+
# VG is a VisualizationGraph object
98+
VG.color_nodes(
99+
"centrality_score",
100+
[(255, 0, 0), (191, 64, 0), (128, 128, 0), (64, 191, 0), (0, 255, 0)] # From red to green
101+
property_type=PropertyType.CONTINUOUS
102+
)
103+
104+
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.
105+
Since we only provided five colors in the range, the granularity of the gradient will be limited to five steps.
106+
107+
`palettable` and `matplotlib` are great libraries to use to create custom color gradients.
74108

75109

76110
Sizing nodes
77-
--------------
111+
------------
78112

79113
Nodes can be given a size directly by providing them with a size property, upon creation.
80114
This can for example be done by passing a size as an integer to the ``size`` parameter of the

0 commit comments

Comments
 (0)