spacenet.utils.plot_spatial_network#
- spacenet.utils.plot_spatial_network(spatial_network, node_label_name=None, nodes_to_plot=None, ax=None, edge_width=1, marker_size=10, add_node_cbar=True, edge_weight_name=None, edge_cmap='Greys_r', edge_vmin=None, edge_vmax=None, add_edge_cbar=False, scatter_kwargs={}, figure_kwargs={})#
Plots a spatial network with nodes positioned according to the provided points. Nodes can be colored based on provided labels, and edges can be colored based on their weights.
- Parameters:
- spatial_networknetworkx.Graph
The spatial network to be plotted. Edges should have a weight attribute corresponding to the distance between nodes.
- node_label_namestr, optional
The name of the node attribute in the spatial network that corresponds to the labels for coloring nodes. If None, nodes will not be colored based on labels. Default is None.
- nodes_to_plotlist, tuple, or numpy array, optional
A list, tuple, or numpy array of node indices to plot. If None, all nodes in the spatial network will be plotted. Default is None.
- axmatplotlib.axes.Axes, optional
An existing matplotlib Axes object to plot on. If None, a new figure and axes will be created. Default is None.
- edge_widthfloat, optional
The width of the edges in the plot. Default is 1.
- marker_sizefloat, optional
The size of the node markers in the plot. Default is 10.
- add_node_cbarbool, optional
Whether to add a legend for the node labels. Default is False.
- edge_weight_namestr, optional
The name of the edge attribute in the spatial network that corresponds to the distance between nodes. Default is None.
- edge_cmapstr or matplotlib.colors.Colormap, optional
The colormap to use for coloring edges based on their weights. Default is ‘Greys_r’.
- edge_vminfloat, optional
The minimum value for edge weight coloring. If None, the minimum edge weight will be used. Default is None.
- edge_vmaxfloat, optional
The maximum value for edge weight coloring. If None, the maximum edge weight will be used. Default is None.
- add_edge_cbarbool, optional
Whether to add a colorbar for the edge weights. Default is False.
- scatter_kwargsdict, optional
Additional keyword arguments to pass to the scatter function when plotting nodes. Default is an empty dictionary.
- figure_kwargsdict, optional
Additional keyword arguments to pass to plt.figure() when creating a new figure. Default is an empty dictionary.
- Returns:
- figmatplotlib.figure.Figure
The figure object containing the plot.
- axmatplotlib.axes.Axes
The axes object containing the plot.
Examples
You can use the plot_spatial_network function to visualize a spatial network with nodes colored by their labels and edges colored by their weights. Below is a few examples of how to use this function to plot a spatial network generated from a set of points. The basic usage of the function is as follows:
import spacenet as sn # get data from the Spiral dataset sprial_df = sn.datasets.load_dataset('spiral') points = sprial_df[['x','y']].values # generate a spatial network using the delaunay method G = sn.utils.spatial_network_from_points(points,network_type='delaunay',max_edge_distance=75) # plot the spatial network sn.utils.plot_spatial_network(G)
The same functionality works for 3D spatial networks as well.
import spacenet as sn # get data from the Cylinder dataset sprial_df = sn.datasets.load_dataset('cylinder') points = sprial_df[['x','y','z']].values # generate a spatial network using the delaunay method G = sn.utils.spatial_network_from_points(points,network_type='delaunay',max_edge_distance=30) # plot the spatial network sn.utils.plot_spatial_network(G)
Node labels can also be shown on these plots by providing the node_label_name argument, the visualisation will infer whether this is a categorical or continuous label. For example, if we have a categorical label called ‘Marker (categorical)’ in the spatial network, we can plot the spatial network with nodes colored by this label as follows:
import spacenet as sn # get data from the Spiral dataset sprial_df = sn.datasets.load_dataset('spiral') points = sprial_df[['x','y']].values categorical_labels = sprial_df['Marker (categorical)'].values # generate a spatial network using the delaunay method and add labels G = sn.utils.spatial_network_from_points(points,network_type='delaunay',max_edge_distance=75) sn.utils.add_node_labels(G,categorical_labels,node_label_name='Marker (categorical)') # plot the spatial network with nodes coloured by their categorical label sn.utils.plot_spatial_network(G,node_label_name='Marker (categorical)',add_node_cbar=True)
If we have a continuous label called ‘Marker (continuous)’ in the spatial network, we can plot the spatial network with nodes colored by this label as follows:
import spacenet as sn # get data from the Cylinder dataset sprial_df = sn.datasets.load_dataset('cylinder') points = sprial_df[['x','y','z']].values categorical_labels = sprial_df['Marker (continuous)'].values # generate a spatial network using the delaunay method and add labels G = sn.utils.spatial_network_from_points(points,network_type='delaunay',max_edge_distance=35) sn.utils.add_node_labels(G,categorical_labels,node_label_name='Marker (continuous)') # plot the spatial network with nodes coloured by their continuous label sn.utils.plot_spatial_network(G,node_label_name='Marker (continuous)',add_node_cbar=True)