【问题标题】:MatplotLib 'saveFig()' FullscreenMatplotLib 'saveFig()' 全屏
【发布时间】:2018-01-12 21:29:00
【问题描述】:

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

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

显示结果图片:

我的代码如下:

def plot_tec_cartopy(descfile): 全局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 用户?
  • 有帮助,但我不认为这是一个骗局:stackoverflow.com/a/4306340/2988730。我现在正在为你起草一个答案。

标签: python matplotlib cartopy


【解决方案1】:

来自MATLAB,不直观的是,你显示的图不必影响保存的尺寸等。每个由不同的后端处理,你可以修改dpi和@987654323 @任你选择。

增加 DPI 肯定会帮助您获得较大的数字,尤其是对于像 PNG 这样的格式,它不知道以英寸为单位的大小。但是,它不会帮助您相对于图形本身缩放文本。

为此,您必须使用面向对象的 API,特别是 figure.set_size_inches,我认为在 plt 中没有等效的 API。替换

plt.savefig(figName, dpi=500)

fig = plt.gcf()
fig.set_size_inches((8.5, 11), forward=False)
fig.savefig(figName, dpi=500)

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

【讨论】:

  • 传统上,您会通过单击答案旁边的复选标记来标记答案来庆祝,如果您对此有声誉,则进行投票。
  • @Hollweg 您可能没有足够的声誉来支持答案,但您始终可以接受对您有益且有用的答案。接受您的问题的答案也会将您的问题从unanswered 列表中删除,因为它应该是。
  • 为什么会出现这个错误? AttributeError: 'Figure' 对象没有属性'save'
  • @雷。是savefig还是新版本的什么?
  • @Shai。看起来他们在更高版本中使 Figure 类和 pyplot 之间的名称保持一致。感谢您的检查。
【解决方案2】:

只是为了在@Mad Physicist 答案上添加一些上下文。如果有人尝试将它与新版本的 matplotlib 一起使用,您将收到 AttributeError: 'Figure' object has no attribute 'save'。当你调用plt.show() 时你也需要小心,否则你会得到一个空白图像。您需要更新代码如下:

# Create a plot
plt.barh(range(top), imp, align='center')
plt.yticks(range(top), names)

# Get the current figure like in MATLAB
fig = plt.gcf()
plt.show() # show it here (important, if done before you will get blank picture)
fig.set_size_inches((8.5, 11), forward=False)
fig.savefig(figName, dpi=500) # Change is over here

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    相关资源
    最近更新 更多