Python MatplotLib';saveFig()';全屏

Python MatplotLib';saveFig()';全屏,python,matplotlib,cartopy,Python,Matplotlib,Cartopy,我使用MatplotLib和Cartopy生成了一些数据图像。 问题是,当我将帧大小设置为全屏并使用plt.show()时,图像非常完美,分辨率也很好 但是,当我使用“plt.savefig()”保存此图时,保存的图像保持其原始大小(不是全屏) 显示结果图像: 我的代码如下: def plot_tec_cartopy(描述文件): 全局matrixLon、matrixLat、matrixTec ax = plt.axes(projection=cartopy.crs.PlateCarree(

我使用MatplotLib和Cartopy生成了一些数据图像。 问题是,当我将帧大小设置为全屏并使用plt.show()时,图像非常完美,分辨率也很好

但是,当我使用“plt.savefig()”保存此图时,保存的图像保持其原始大小(不是全屏)

显示结果图像:

我的代码如下:

def plot_tec_cartopy(描述文件): 全局matrixLon、matrixLat、matrixTec

ax = plt.axes(projection=cartopy.crs.PlateCarree())

v = np.linspace(0, 80, 46, endpoint=True)
cp = plt.contourf(matrixLon, matrixLat, matrixTec, v, cmap=plt.cm.rainbow)
plt.clim(0, 80)
plt.colorbar(cp)

ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.set_extent([-85, -30, -60, 15])

# Setting X and Y labels using LON/LAT format
ax.set_xticks([-85, -75, -65, -55, -45, -35])
ax.set_yticks([-60, -55, -50, -45, -40, -35, -30, -25, -20, -15, -10, -5, 0, 5, 10, 15])
lon_formatter = LongitudeFormatter(number_format='.0f',
                                   degree_symbol='',
                                   dateline_direction_label=True)
lat_formatter = LatitudeFormatter(number_format='.0f',
                                  degree_symbol='')
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)

plt.title('Conteúdo Eletrônico Total', style='normal', fontsize='12')

# Acquiring Date
year, julianday = check_for_zero(descfile.split('.')[2]), descfile.split('.')[3]
hour, minute = descfile.split('.')[4], descfile.split('.')[5].replace('h','')
date = datetime.datetime(int(year), 1, 1, int(hour), int(minute)) + datetime.timedelta(int(julianday)-1)
month = date.month
day = date.day

# Set common labels
ax.text(1.22, 1.05, 'TEC', style='normal',
    verticalalignment='top', horizontalalignment='right',
    transform=ax.transAxes,
    color='black', fontsize=11)
ax.text(1, 0.005, 'EMBRACE/INPE', style='italic',
    verticalalignment='bottom', horizontalalignment='right',
    transform=ax.transAxes,
    color='black', fontsize=10)
ax.text(1, 0.995, str(date) + ' UT', style='italic',
    verticalalignment='top', horizontalalignment='right',
    transform=ax.transAxes,
    color='black', fontsize=10)
ax.text(0.5, -0.08, 'Copyright \N{COPYRIGHT SIGN} 2017 INPE - Instituto Nacional de',
    style='oblique', transform=ax.transAxes,
    verticalalignment='bottom', horizontalalignment='center',
    color='black', fontsize=8)
ax.text(0.5, -0.108, 'Pesquisas Espacias. Todos direitos reservados',
    style='oblique', transform=ax.transAxes,
    verticalalignment='bottom', horizontalalignment='center',
    color='black', fontsize=8)

manager = plt.get_current_fig_manager()
manager.resize(*manager.window.maxsize())

figName = 'tec.map' + '.' + str(year) + '.' + str(julianday) + '.' + str(hour) + '.' + str(minute) + 'h.png'
#plt.show()
plt.savefig(figName, dpi=500)
plt.clf()
也许我需要在savefig()中设置一些参数,表示需要保存修改后的帧?有人能帮我解决这个问题吗


提前感谢。

来自MATLAB,显示的图形不必在尺寸等方面影响保存的图形是不直观的。每个图形都由不同的后端处理,您可以根据自己的选择修改
dpi
size\u inches

增加DPI肯定会帮助你得到一个大的数字,特别是对于PNG这样的格式,它不知道以英寸为单位的大小。但是,它不会帮助您相对于地物本身缩放文本

要做到这一点,您必须使用面向对象的API,具体来说,我认为它在
plt
中没有等价物。替换

plt.savefig(figName, dpi=500)


尺寸
8.5、11
分别是美国标准纸张尺寸的宽度和高度。您可以将其设置为您想要的任何值。例如,您可以使用屏幕大小,但在这种情况下,请确保DPI也正确。

只需在@Mad Physician answer上添加一些上下文即可。如果有人试图将其与新版本的matplotlib一起使用,您将获得AttributeError:“Figure”对象没有属性“save”。在调用
plt.show()
时,还需要小心,否则会得到一个空白图像。您需要按如下方式更新代码:

#创建绘图
plt.barh(范围(顶部),imp,align='center')
plt.yticks(范围(顶部),名称)
#像在MATLAB中一样获取当前图形
图=plt.gcf()
plt.show()#在此处显示(如果在获得空白图片之前完成此操作,则很重要)
图设置尺寸英寸((8.5,11),正向=假)
fig.savefig(figName,dpi=500)#变化在这里

希望有帮助未回答列表中删除。为什么我会出现此错误?AttributeError:“Figure”对象没有属性“save”@Lei。它是
savefig
还是新版本的什么东西?@Shai。看起来他们使Figure类和pyplot在更高版本中的名称保持一致。谢谢你的检查。
fig = plt.gcf()
fig.set_size_inches((8.5, 11), forward=False)
fig.savefig(figName, dpi=500)