Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MatplotLib 'saveFig()' Fullscreen

I used MatplotLib with Cartopy to generate some data images. The problem is that when I set the frame size to fullscreen and use plt.show() the image is perfect and the resolution is fine.

However, when I save this figure using 'plt.savefig()' the image saved keeps with its original size (not fullscreen).

Showing outcome images:

Showing the image - not saving it

Saving the image - not showing it

My code is the following:

def plot_tec_cartopy(descfile): global 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()

Maybe I need to set some parameter into savefig() to say it that it needs to save my modified frame? Can someone help me with this issue?

Thanks in advance.

like image 433
Hollweg Avatar asked Aug 04 '17 20:08

Hollweg


People also ask

How to plot at full resolution with Matplotlib?

To plot at full resolution with matplotlib.pyplot, imshow () and savefig (), we can keep the dpi value from 600 to 1200. Set the figure size and adjust the padding between and around the subplots. Set random values in a given shape. Save the figure with 1200 dpi. To display the figure, use show () method.

What is savefig () method in Matplotlib?

Matplotlib.pyplot.savefig () As the name suggests savefig () method is used to save the figure created after plotting data. The figure created can be saved to our local machines by using this method. Syntax: savefig (fname, dpi=None, facecolor=’w’, edgecolor=’w’, orientation=’portrait’, papertype=None, format=None, transparent=False ...

Can I use Matplotlib with cartopy to generate fullscreen images?

I used MatplotLib with Cartopy to generate some data images. The problem is that when I set the frame size to fullscreen and use plt.show() the image is perfect and the resolution is fine.

How do I save a plot in Matplotlib?

Matplotlib.pyplot.savefig () As the name suggests savefig () method is used to save the figure created after plotting data. The figure created can be saved to our local machines by using this method. Syntax: savefig (fname, dpi=None, facecolor=’w’, edgecolor=’w’, orientation=’portrait’, papertype=None, format=None, transparent=False, ...


1 Answers

Coming from MATLAB, it is not intuitive that your displayed figure does not have to affect the saved one in terms of dimensions, etc. Each one is handled by a different backend, and you can modify the dpi and size_inches as you choose.

Increasing the DPI is definitely going to help you get a large figure, especially with a format like PNG, which does not know about the size in inches. However, it will not help you scale the text relative to the figure itself.

To do that, you will have to use the object oriented API, specifically, figure.set_size_inches, which I don't think has an equivalent in plt. Replace

plt.savefig(figName, dpi=500)

with

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

The size 8.5, 11 is the width and height of the standard paper size in the US, respectively. You can set it to whatever you want. For example, you can use your screen size, but in that case be sure to get the DPI right as well.

like image 67
Mad Physicist Avatar answered Oct 11 '22 20:10

Mad Physicist