首页 > 解决方案 > 在 Jupyter Notebboks 中保存绘图时出现驱动错误

问题描述

我正在尝试从 Ipython 笔记本中保存文件。我正在使用该geopandas库来创建我的情节。但是我已经在 jupyter 的树中创建了目录。

import os
import geopandas as gpd
from matplotlib import pyplot as plt
import matplotlib as mpl
%matplotlib inline
%matplotlib notebook
MAP_DIR_PATH = os.path.join('maps')
MAP_USA_FILE_PATH = os.path.join(MAP_DIR_PATH, 'USA')

PLOT_DIR_PATH = os.path.join('plots')

PLOT_USA_FILE_PATH = os.path.join(PLOT_DIR_PATH, 'risk_USA_states_top.png')

CMAP = 'YlGnBu'

def plot_settings(fig,ax,zlabel,colormap,norm):
    fig.set_size_inches(12,12)
    plt.xlim(-2.2e6,2.5e6)
    plt.ylim(-2.2e6,1e6)
    plt.xticks([])
    plt.yticks([])
    plt.axis('off')
    #Colorbar
    fig = ax.get_figure()
    cax = fig.add_axes([0.92, 0.27, 0.025, 0.5])
    sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm)
    sm._A = []
    fig.colorbar(sm, cax=cax)
    cax.set_ylabel(zlabel,fontsize=20)
    plt.tick_params(axis='both', which='major', labelsize=15)

# Plot

gdf_plot = gpd.read_file(MAP_USA_FILE_PATH)

fig, ax = plt.subplots()
column = 'IRI'

norm = mpl.colors.Normalize(vmin=0, vmax=1)

gdf_plot.plot(ax=ax, column=column, cmap=CMAP, norm=norm, lw=0, vmin=0, vmax=1)

plot_settings(fig, ax, 'Infodemic Risk Index', CMAP, norm)


# Save figure

plt.savefig(PLOT_USA_FILE_PATH,
            bbox_inches = 'tight',
            definition = 'high', 
            transparent=True)
DriverError: 'maps/USA' not recognized as a supported file format.

结构体 不确定我是否遗漏了什么或做错了什么。

标签: pythonmatplotlibjupyter-notebook

解决方案


推荐阅读