OSMnx 为图表添加标题
OSMnx Add Title to Graph Plot
我用的很精彩OSMnx library created by Geoff Boeing. I am plotting a street network based on one of his tutorials。一切都很完美。但是,我想绘制 40 多个图表,使用不同的中心性。因此,我想为每个地块添加一个带有每个地区和中心名称的标题。目前,它看起来像这样。
Plotted OSMnx Street Network
这就是我的代码的样子。
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 包的函数调用 plt.show()
已经在它们 return fig
和 ax
句柄之前,这意味着您不能再操纵Figure
和 Axes
实例(我的猜测是这样做是为了防止创建后图形失真)。这是使用称为 save_and_show()
的特殊函数完成的,该函数在内部调用。您可以通过将关键字 show=False
和 close=False
传递给相应的绘图函数来防止显示图形(需要 close=False
因为不自动显示的图形默认情况下在 save_and_show()
).使用这些关键字,fig
和 ax
可以在函数调用后进行操作,但现在 plt.show()
必须显式调用。这里仍然是 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
没有。希望这有帮助。
我用的很精彩OSMnx library created by Geoff Boeing. I am plotting a street network based on one of his tutorials。一切都很完美。但是,我想绘制 40 多个图表,使用不同的中心性。因此,我想为每个地块添加一个带有每个地区和中心名称的标题。目前,它看起来像这样。
Plotted OSMnx Street Network
这就是我的代码的样子。
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 包的函数调用 plt.show()
已经在它们 return fig
和 ax
句柄之前,这意味着您不能再操纵Figure
和 Axes
实例(我的猜测是这样做是为了防止创建后图形失真)。这是使用称为 save_and_show()
的特殊函数完成的,该函数在内部调用。您可以通过将关键字 show=False
和 close=False
传递给相应的绘图函数来防止显示图形(需要 close=False
因为不自动显示的图形默认情况下在 save_and_show()
).使用这些关键字,fig
和 ax
可以在函数调用后进行操作,但现在 plt.show()
必须显式调用。这里仍然是 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
没有。希望这有帮助。