Osmnx 图具有透明背景

Omsnx plot has transparent background

有一个新版本的 osmnx,当我尝试用我的颜色条绘制我的图形时,我得到的图像具有透明背景。在早期版本中,这没有发生。谁能解释一下为什么会出现这个透明背景?

代码如下:

city = 'Portugal, Lisbon'
G = ox.graph_from_place(city, network_type='drive', simplify=True)
G_nx = nx.relabel.convert_node_labels_to_integers(G)
G_1 = ox.project_graph(G_nx)
cmap = plt.cm.get_cmap('inferno_r')
norm=plt.Normalize(vmin=min_r, vmax=max_r)
sm = mpl.cm.ScalarMappable(norm=norm, cmap=cmap)
sm.set_array([])

fig, ax = ox.plot.plot_graph(G_1, node_color=nc, node_size=ns, edge_linewidth=0.5, figsize = (40,40), bgcolor = 'white')
cb = fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax, orientation='horizontal', aspect=30, fraction = 0.2)
cb.set_label('r', fontsize = 60)
cb.ax.tick_params(labelsize=60)
fig.savefig('demo1z.png')

请将节点颜色视为随机列表,因为这只是一个示例,节点大小也是如此。

图片如下:

提前致谢。

OSMnx 将 fig/ax 大小限制在特定的绘图区域。如果您通过添加其他功能(例如颜色条)来扩展图的大小,只需打开图形框架并在保存时为其指定颜色。

import matplotlib as mpl
import networkx as nx
import osmnx as ox
ox.config(use_cache=True, log_console=True)

# get graph
city = 'Lisbon, Portugal'
G = ox.graph_from_place(city, network_type='drive', simplify=True)

# get colors
cmap = 'plasma'
nc = ox.plot.get_node_colors_by_attr(G, attr='y', cmap=cmap)
norm = mpl.colors.Normalize(vmin=min(nx.get_node_attributes(G, 'y').values()),
                            vmax=max(nx.get_node_attributes(G, 'y').values()))

# plot graph and add colorbar
fig, ax = ox.plot_graph(G, edge_linewidth=0, node_size=1, node_color=nc, show=False, bgcolor='lightgray')
cbar = fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap))

# turn figure frame on and save to disk
fig.set_frameon(True)
fig.savefig('fig.png', facecolor=fig.get_facecolor(), bbox_inches='tight')