Friday 20 November 2020

Matplotlib doesn't save image in fullscreen

[edit] As suggested by the comment in the answer, the issue is probably OS related. I am on windows 10. I have python 3.7.1 and I use Anaconda/Spyder.

I followed this and this topic to try to maximize the image produced by a plot before saving it. In my code, what works is that the plot from spyder figure viewer is indeed maximized. But when the file is saved into an image, the image is not maximized.

How can I fix it ?

My code below:

import matplotlib as mpl
mpl.use('TkAgg') # With this line on: it returns me error. Without: image don't save as it has been shown in plot from spyder.
from datetime import datetime
import constants
import numpy as np
from numpy import *
from shutil import copyfile
from matplotlib.pyplot import *
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cbook as cbook
import matplotlib.gridspec as gridspec

plt.rcParams['lines.linewidth'] = 3
plt.rcParams.update({'font.size': 60})
plt.rc('axes', labelsize=80)
plt.rc('xtick', labelsize=80) 
plt.rc('ytick', labelsize=60) 
rcParams["savefig.jpeg_quality"] = 40 

dpi_value = 100
mpl.rcParams["savefig.jpeg_quality"] = dpi_value
plt.rc('axes', labelsize=60)
plt.rc('xtick', labelsize=60)
plt.rc('ytick', labelsize=60)

def plot_surface(zValues,xValues,yValues,title,xLabel,yLabel,titleSave,xlog=True,ylog=True,zlog=True):
    # This function plots a 2D colormap.
    # We set the grid of temperatures
    X,Y =np.meshgrid(xValues,yValues)
    zValues=np.transpose(np.asarray(zValues))
    # We need to transpose because x values= column, y values = line given doc
    # We now do the plots of kopt-1
    fig1,ax1=plt.subplots()
    if zlog==True:
        pcm1=ax1.pcolor(X,Y,zValues,
                        cmap='rainbow',
                        edgecolors='black',
                        norm=colors.LogNorm(vmin=zValues.min(), vmax=zValues.max()))
    else:
        pcm1=ax1.pcolor(X,Y,zValues,
                        cmap='rainbow',
                        edgecolors='black',
                        norm=colors.Normalize(vmin=zValues.min(),vmax=zValues.max()))
    if xlog==True:
        ax1.set_xscale('log', basex=10)
    if ylog==True:
        ax1.set_yscale('log', basey=10)
        
    ax1.set_title(title)
    ax1.set_ylabel(yLabel)
    ax1.set_xlabel(xLabel)
    
    plt.colorbar(pcm1,extend='max')
    
    figManager = plt.get_current_fig_manager()
    figManager.full_screen_toggle()
    # the solution here 
    plt.tight_layout()
    plt.show()

#    if(constants.save_plot_calculation_fct_parameter==True):
    dpi_value=100
    fig1.savefig(titleSave+".jpg",format='jpg',dpi=dpi_value,bbox_inches='tight')

x=np.arange(0,40)
y=np.arange(0,40)
z=np.random.rand(len(x),len(y))
plot_surface(z,x,y,"AA","BB","CC","name",zlog=False)

plt.show()

The rendering in figure from spyder:

enter image description here

And from the image saved:

enter image description here

[edit]

I copy-pasted the code from the answer below and the display are still different. The "figure" window from spyder:

enter image description here

The image saved on my computer:

enter image description here

[New edit]: I listed all the valid backends with the help of the answer below. They are the following:

valid backends:         ['agg', 'nbagg', 'pdf', 'pgf', 'ps', 'qt5agg', 'svg', 'template', 'webagg'] 

The only one that works to display a figure within spyder is qt5agg. And with this one the image doesn't save properly as explained.



from Matplotlib doesn't save image in fullscreen

No comments:

Post a Comment