plot
Convenience methods for plotting graphs within the cityseer API context. Custom behaviour can be achieved by directly manipulating the underlying NetworkX
and matplotlib
figures. This module is predominately used for basic plots or visual verification of behaviour in code tests. Users are encouraged to use matplotlib or other plotting packages directly where possible.
plot_nx_primal_or_dual
Plot a primal or dual cityseer graph.
Parameters
An optional NetworkX
MultiGraph to plot in the primal representation. Defaults to None.
An optional NetworkX
MultiGraph to plot in the dual representation. Defaults to None.
An optional filepath: if provided, the image will be saved to the path instead of being displayed. Defaults to None.
Whether to display node labels. Defaults to False.
The diameter for the primal graph’s nodes.
Primal node colour or colours. When passing an iterable of colours, the number of colours should match the order and number of nodes in the MultiGraph. The colours are passed to the underlying draw_networkx
method and should be formatted accordingly. Defaults to None.
Primal edge colour or colours. When passing an iterable of colours, the number of colours should match the order and number of edges in the MultiGraph. The colours are passed to the underlying draw_networkx
method and should be formatted accordingly. Defaults to None.
The diameter for the dual graph’s nodes.
Dual node colour or colours. When passing a list of colours, the number of colours should match the order and number of nodes in the MultiGraph. The colours are passed to the underlying draw_networkx
method and should be formatted accordingly. Defaults to None.
Dual edge colour or colours. When passing an iterable of colours, the number of colours should match the order and number of edges in the MultiGraph. The colours are passed to the underlying draw_networkx
method and should be formatted accordingly. Defaults to None.
Linewidths for the primal edge. Defaults to None.
Linewidths for the dual edge. Defaults to None.
Whether to plot the edge geometries. If set to False
, straight lines will be drawn from node-to-node to represent edges. Defaults to True.
A tuple or list with the minimum and maxium x
extents to be plotted. Defaults to None.
A tuple or list with the minimum and maxium y
extents to be plotted. Defaults to None.
An optional matplotlib
ax
to which to plot. If not provided, a figure and ax will be generated.
kwargs
which will be passed to the matplotlib
figure parameters. If provided, these will override the default figure size or dpi parameters.
Notes
Plot either or both primal and dual representations of a networkX MultiGraph
. Only call this function directly if explicitly printing both primal and dual graphs. Otherwise, use the simplified plot_nx
method instead.
from cityseer.tools import mock, graphs, plot
G = mock.mock_graph()
G_simple = graphs.nx_simple_geoms(G)
G_dual = graphs.nx_to_dual(G_simple)
plot.plot_nx_primal_or_dual(G_simple, G_dual, plot_geoms=False)
A dual graph in blue overlaid on the source primal graph in red.
plot_nx
Plot a networkX
MultiGraph.
Parameters
A NetworkX
MultiGraph.
An optional filepath: if provided, the image will be saved to the path instead of being displayed. Defaults to None.
Whether to display node labels. Defaults to False.
The diameter for the graph’s nodes.
Node colour or colours. When passing an iterable of colours, the number of colours should match the order and number of nodes in the MultiGraph. The colours are passed to the underlying draw_networkx
method and should be formatted accordingly. Defaults to None.
Edges colour as a matplotlib
compatible colour string. Defaults to None.
Linewidths for edges. Defaults to None.
Whether to plot the edge geometries. If set to False
, straight lines will be drawn from node-to-node to represent edges. Defaults to True.
A tuple or list with the minimum and maximum x
extents to be plotted. Defaults to None.
A tuple or list with the minimum and maximum y
extents to be plotted. Defaults to None.
An optional matplotlib
ax
to which to plot. If not provided, a figure and ax will be generated.
kwargs
which will be passed to the matplotlib
figure parameters. If provided, these will override the default figure size or dpi parameters.
Notes
from cityseer.tools import mock, graphs, plot, io
from cityseer.metrics import networks
from matplotlib import colors
# generate a MultiGraph and compute gravity
G = mock.mock_graph()
G = graphs.nx_simple_geoms(G)
G = graphs.nx_decompose(G, 50)
nodes_gdf, edges_gdf, network_structure = io.network_structure_from_nx(G)
networks.node_centrality_shortest(network_structure=network_structure, nodes_gdf=nodes_gdf, distances=[800])
G_after = io.nx_from_cityseer_geopandas(nodes_gdf, edges_gdf)
# let's extract and normalise the values
vals = []
for node, data in G_after.nodes(data=True):
vals.append(data["cc_beta_800"])
# let's create a custom colourmap using matplotlib
cmap = colors.LinearSegmentedColormap.from_list(
"cityseer", [(100 / 255, 193 / 255, 255 / 255, 255 / 255), (211 / 255, 47 / 255, 47 / 255, 1 / 255)]
)
# normalise the values
vals = colors.Normalize()(vals)
# cast against the colour map
cols = cmap(vals)
# plot
plot.plot_nx(G_after, node_colour=cols)
Colour plot of 800m gravity index centrality on a 50m decomposed graph.
plot_assignment
Plot a network_structure
and data_gdf
for visualising assignment of data points to respective nodes.
This method is primarily intended for package testing and development.
Parameters
A rustalgos.NetworkStructure
instance.
A NetworkX
MultiGraph.
A data_gdf
GeoDataFrame
with nearest_assigned
and next_nearest_assign
columns.
An optional filepath: if provided, the image will be saved to the path instead of being displayed. Defaults to None.
Node colour or colours. When passing a list of colours, the number of colours should match the order and number of nodes in the MultiGraph. The colours are passed to the underlying draw_networkx
method and should be formatted accordingly. Defaults to None.
Whether to plot the node labels. Defaults to False.
An optional iterable of categorical data labels which will be mapped to colours. The number of labels should match the number of data points in data_layer
. Defaults to None.
kwargs
which will be passed to the matplotlib
figure parameters. If provided, these will override the default figure size or dpi parameters.
Notes
An assignment plot to a 50m decomposed graph, with the data points coloured by categorical labels.
plot_network_structure
Plot a graph from raw cityseer
network structure.
Note that this function is subject to frequent revision pending short-term development requirements. It is used mainly to visually confirm the correct behaviour of particular algorithms during the software development cycle.
Parameters
A rustalgos.NetworkStructure
instance.
A data_gdf
GeoDataFrame
with nearest_assigned
and next_nearest_assign
columns.
An optional polygon. Defaults to None.
plot_scatter
Convenience plotting function for plotting outputs from examples in the Cityseer Guide.
Parameters
A ‘matplotlib’ Ax
to which to plot.
A numpy array of floats representing the x
coordinates for points to plot.
A numpy array of floats representing the y
coordinates for points to plot.
A numpy array of floats representing the data values for the provided points.
A tuple or list containing the [s, w, n, e]
bounding box extents for clipping the plot.
A tuple of two floats, representing the minimum and maximum percentiles at which to clip the data.
A matplotlib
colour map key.
A float representing an exponential for reshaping the values distribution. Defaults to 1 which returns the values as provided. An exponential greater than or less than 1 will shape the values distribution accordingly.
A float representing the minimum size for a plotted point.
A float representing the maximum size for a plotted point.
Whether or not to rasterise the output. Recommended for plots with a large number of points.
A hex or other valid matplotlib
colour value for the ax and figure faces (backgrounds).
plot_nx_edges
Convenience plotting function for plotting outputs from examples in the Cityseer Guide.
Parameters
A ‘matplotlib’ Ax
to which to plot.
A NetworkX
MultiGraph.
An edge key for the provided nx_multigraph
. Plotted values will be retrieved from this edge key.
A tuple or list containing the [s, w, n, e]
bounding box extents for clipping the plot.
A tuple of two floats, representing the minimum and maximum percentiles at which to clip the data.
A matplotlib
colour map key.
A float representing an exponential for reshaping the values distribution. Defaults to 1 which returns the values as provided. An exponential greater than or less than 1 will shape the values distribution accordingly.
A float representing the minimum line width for a plotted edge.
A float representing the maximum line width for a plotted edge.
A key for retrieving categorical labels from edges.
Whether to plot colours by categorical. This requires an edge_label_key
parameter.
The number of categorical values (sorted in decreasing order) to plot.
Whether or not to rasterise the output. Recommended for plots with a large number of edges.
A hex or other valid matplotlib
colour value for the ax and figure faces (backgrounds).
Whether to invert the plot order, e.g. if using an inverse colour map.