I'm using the wonderful OSMnx library created by Geoff Boeing. I am plotting a street network based on one of his tutorials. Everything works perfectly. However, I would like to plot more than 40 graphs, using different centralities. Therefore, I would like to add a title with each district and centrality name to each plot. Currently, it looks like this.
This is what my code looks like.
def display_most_important_node(G_centralities_sorted_dict, G_dictionary, district, centrality_measure='betweenness_centrality'):
node_color = ['red' if node == G_centralities_sorted_dict[district][centrality_measure][0][0] else '#336699' for node in ox.project_graph(G_dictionary[district]).nodes()]
node_size = [40 if node == G_centralities_sorted_dict[district][centrality_measure][0][0] else 20 for node in ox.project_graph(G_dictionary[district]).nodes()]
fig, ax = ox.plot_graph(ox.project_graph(G_dictionary[district]), annotate=False, edge_linewidth=1.5, node_size=node_size, fig_height=10, node_color=node_color, node_zorder=2)
Thank you guys.
By default, the functions of the OSMnx package call
plt.show()
already before they return thefig
andax
handles, which means you can no longer manipulate theFigure
andAxes
instances (my guess is that this is done to prevent distortion of the Figure after creation). This is done using a special function calledsave_and_show()
, which is called internally. You can prevent the showing of the figure by passing the keywordsshow=False
andclose=False
to the according plotting function (close=False
is needed because figures that are not automatically shown are by default closed withinsave_and_show()
). With these keywords used,fig
andax
can be manipulated after the function call, but nowplt.show()
has to be called explicitly. Here still a complete example following the OP:Note that not all OSMnx functions accept the
show
andclose
keywords. For instance,plot_shape
does not. Hope this helps.