2017-08-04 165 views
2

我用Cartp的MatplotLib生成一些數據圖像。 問題是,當我將幀大小設置爲全屏並使用plt.show()時,圖像是完美的,分辨率也很好。MatplotLib'saveFig()'全屏

但是,當我使用'plt.savefig()'保存此圖時,保存的圖像保持其原始尺寸(不是全屏)。

顯示結果圖片:

Showing the image - not saving it

Saving the image - not showing it

我的代碼如下:

高清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()來表示它需要保存我的修改後的幀?有人可以幫我解決這個問題嗎?

在此先感謝。

+0

讓我猜測,MATLAB的用戶誰期望在屏幕上的數字是以某種方式與文件中的數字相關? –

+0

有幫助,但我不認爲它是一個騙局:https://stackoverflow.com/a/4306340/2988730。我現在正在起草一個答案。 –

回答

2

從MATLAB來看,你顯示的圖形不需要在維度等方面影響保存的圖形。每一個都由不同的後端來處理,並且你可以根據你的需要修改dpisize_inches選擇。

增加DPI肯定會幫助你獲得一個大的數字,特別是像PNG這樣的格式,它不知道以英寸爲單位的大小。但是,它不會幫助您將文字相對於數字本身進行縮放。

要做到這一點,您將不得不使用面向對象的API,特別是figure.set_size_inches,我認爲它在plt中沒有等效的功能。替換

plt.savefig(figName, dpi=500) 

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

大小8.5, 11是在美國的寬度和標準紙張大小的高度,分別。你可以將它設置爲任何你想要的。例如,您可以使用屏幕尺寸,但在這種情況下,一定要確保DPI正確。

+0

您的解決方案非常好,解決了問題! 感謝您的回答,瘋狂的物理學家。 :) – Hollweg

+1

傳統上,您可以通過點擊旁邊的複選標記標記答案來慶祝,如果您有信譽,則可以加註。 –

+0

@Hollweg您可能沒有足夠的聲望來回答問題,但您始終可以接受對您有益且有用的答案。接受你的問題的答案也應該從'unanswered'列表中刪除你的問題。 – swatchai