43202

OSMnx Add Title to Graph Plot

Question:

I'm using the wonderful <a href="https://github.com/gboeing/osmnx" rel="nofollow">OSMnx library</a> created by Geoff Boeing. I am plotting a street network based on one of his <a href="https://github.com/gboeing/osmnx-examples/blob/master/notebooks/06-example-osmnx-networkx.ipynb" rel="nofollow">tutorials</a>. 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.

<a href="https://i.stack.imgur.com/SkLym.png" rel="nofollow">Plotted OSMnx Street Network</a>

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.

Answer1:

By default, the functions of the OSMnx package call plt.show() already before they return the fig and ax handles, which means you can no longer manipulate the Figure and Axes instances (my guess is that this is done to prevent distortion of the Figure after creation). This is done using a special function called save_and_show(), which is called internally. You can prevent the showing of the figure by passing the keywords show=False and close=False to the according plotting function (close=False is needed because figures that are not automatically shown are by default closed within save_and_show()). With these keywords used, fig and ax can be manipulated after the function call, but now plt.show() has to be called explicitly. Here still a complete example following the OP:

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, show=False, close=False) ax.set_title('subplot title') fig.suptitle('figure title') plt.show()

Note that not all OSMnx functions accept the show and close keywords. For instance, plot_shape does not. Hope this helps.

Recommend

  • Networkx - Problem with drawing nodes with nodelist
  • how to differentiate animation from single sprite sheet in ios?
  • Python TA-Lib install problems
  • Crash in program using OpenMP, x64 only
  • Grid.GetRow and Grid.GetColumn keep returning 0
  • Negating Regex PO BOX
  • In loopback documentation what does variable 'cb' stands for?
  • Simple linked list-C
  • CakePHP ACL tutorial initDB function warnings
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Highlight and Bold text in JTextPane
  • Xamarin Forms - UWP Fonts
  • Fetching methods from BroadcastReceiver to update UI
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • C# - Serializing and deserializing static member
  • Problems to linebreak with an int in JLabel
  • Date difference with leap year
  • How would I use PHP exceptions to define a redirect?
  • How to add date and time under each post in guestbook in google app engine
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Join two tables and save into third-sql
  • JSON with duplicate key names losing information when parsed
  • Display Images one by one with next and previous functionality
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Matplotlib draw Spline from multiple points
  • Return words with double consecutive letters
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • How can i traverse a binary tree from right to left in java?
  • java string with new operator and a literal