OSMnx将标题添加到图形图 [英] OSMnx Add Title to Graph Plot
问题描述
我正在使用Geoff Boeing创建的精彩的 OSMnx库.我正在根据他的
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.
这就是我的代码.
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)
谢谢你们.
推荐答案
默认情况下,OSMnx程序包的函数在返回fig
和ax
句柄之前已经调用了plt.show()
,这意味着您不能再操作Figure
和Axes
实例(我的猜测是这样做是为了防止图形在创建后失真).这是通过使用称为save_and_show()
的特殊功能(在内部调用)来完成的.您可以通过将关键字show=False
和close=False
传递给相应的绘图功能来防止图形显示(需要close=False
,因为未自动显示的图形默认情况下会在save_and_show()
中关闭).使用这些关键字后,可以在函数调用后操作fig
和ax
,但是现在必须显式调用plt.show()
.这里还是OP之后的完整示例:
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()
请注意,并非所有OSMnx函数都接受show
和close
关键字.例如,plot_shape
不会.希望这会有所帮助.
Note that not all OSMnx functions accept the show
and close
keywords. For instance, plot_shape
does not. Hope this helps.
这篇关于OSMnx将标题添加到图形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!