【发布时间】:2020-10-30 17:00:12
【问题描述】:
[编辑] 正如答案中的评论所建议的,这个问题可能与操作系统有关。我在 Windows 10 上。我有 python 3.7.1,我使用 Anaconda/Spyder。
我关注了this 和this 主题,试图在保存之前最大化绘图生成的图像。在我的代码中,有效的是来自 spyder figure viewer 的情节确实被最大化了。但是当文件保存到图像中时,图像没有最大化。
我该如何解决?
我的代码如下:
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()
spyder中的渲染图:
并从保存的图像中:
[编辑]
我从下面的答案中复制粘贴了代码,但显示仍然不同。来自 spyder 的“数字”窗口:
我电脑上保存的图片:
[新编辑]:我在下面的答案的帮助下列出了所有有效的后端。它们是:
valid backends: ['agg', 'nbagg', 'pdf', 'pgf', 'ps', 'qt5agg', 'svg', 'template', 'webagg']
唯一可以在 spyder 中显示图形的是 qt5agg。使用这个图像无法按照说明正确保存。
【问题讨论】:
标签: python image matplotlib