- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
[编辑] 正如答案中的评论所建议的,该问题可能与操作系统有关。我在 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的渲染图:
valid backends: ['agg', 'nbagg', 'pdf', 'pgf', 'ps', 'qt5agg', 'svg', 'template', 'webagg']
唯一可以在 spyder 中显示图形的是 qt5agg。有了这个,图像没有按照解释正确保存。
最佳答案
解决方法是添加plt.tight_layout()
就在之前 plt.show
.
运行下面的代码 (请注意,在我的 Mac 上,我必须使用 figManager.full_screen_toggle()
而不是 figManager.window.showMaximized()
)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cbook as cbook
import matplotlib.gridspec as gridspec
# comment this part
plt.rcParams['lines.linewidth'] = 3
plt.rcParams.update({'font.size': 20})
#plt.rc('axes', labelsize=60)
#plt.rc('xtick', labelsize=60)
#plt.rc('ytick', labelsize=60)
#rcParams["savefig.jpeg_quality"] = 40
# I don't have the file so I commented this part
# mpl.rcParams['text.usetex'] = True
# mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']
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()
保存的图像将与弹出的图形相同。
matplotlib backend
引起的您正在使用。基于
this post ,
也许尝试另一个后端会解决这个问题 .例如,在我的 Mac 上,如果我使用
TkAgg
后端由
import matplotlib as mpl
mpl.use('TkAgg')
弹出屏幕中的数字与保存的数字不同。为了知道哪些可用
matplotlib backend
在您的机器上,您可以使用
from matplotlib.rcsetup import all_backends
all_backends
是所有可用的列表
backend
你可以试试。
import matplotlib.backends
import matplotlib.pyplot as plt
import os.path
def is_backend_module(fname):
"""Identifies if a filename is a matplotlib backend module"""
return fname.startswith('backend_') and fname.endswith('.py')
def backend_fname_formatter(fname):
"""Removes the extension of the given filename, then takes away the leading 'backend_'."""
return os.path.splitext(fname)[0][8:]
# get the directory where the backends live
backends_dir = os.path.dirname(matplotlib.backends.__file__)
# filter all files in that directory to identify all files which provide a backend
backend_fnames = filter(is_backend_module, os.listdir(backends_dir))
backends = [backend_fname_formatter(fname) for fname in backend_fnames]
print("supported backends: \t" + str(backends),'\n')
# validate backends
backends_valid = []
for b in backends:
try:
plt.switch_backend(b)
backends_valid += [b]
except Exception as e:
print('backend %s\n%s\n' % (b,e))
continue
print("valid backends: \t" + str(backends_valid),'\n')
运行这个脚本,
打印出来的后端 valid backends
是那些可以应用的。 对于那些支持但无效的后端,也会打印出它们无效的原因。例如,在我的 Mac 上,虽然
wxcairo
是受支持的后端,它无效,因为
No module named 'wx'
.
关于python - Matplotlib 不全屏保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64612733/
我正在尝试使用 Qt 制作游戏,因为它太棒了 ;) 而且您可以免费获得所需的所有东西。唯一的问题是更改系统分辨率和设置 QWidget(或 QGLWidget)“真正的”全屏。 你们中有人成功地做到了
有没有办法以全屏模式(没有工具栏、导航)启动 gwt-app? 我只找到了打开新窗口的提示: Window.open("SOMEURL","SOMETITLE", "fullscreen=yes
if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscr
我想解释一下我现在在做什么。我开发了一个 aar(sdk) 并且运行良好。但是,我希望我的 sdk 在被调用时占据全屏,即使调用者有一个工具栏。我尝试了此链接中的示例 [ How to set act
我是 Blazor 的新手,我正在尝试了解如何以全屏模式打开浏览器。我知道我可以执行 Javascript 中断并运行 Javascript,但这违背了 Blazor 的目的。 如何在 Blazor
我正在创建一个全屏应用程序,并且想知道是否有某种方法可以使 NSAlert 位于我创建的 CGDisplayCapture 之上。现在,NSAlert 显示在显示捕获后面。我的主窗口显示得很好(在使用
有人可以解释一下下面的代码是如何工作的吗? 元素指的是视频,全屏指的是页面上的链接。 我无法理解 if 语句 var element = document.getElementById('elemen
我想要实现的是类似于 Instagram 应用内网络浏览器,在您点击广告时使用: 我所做的是,我使用了 WebView bottomSheetDialogFragment,并覆盖了 onCreateD
第二个 ViewController 的顶部有这个空间,它几乎在手机上显示为可关闭的弹出窗口。如何使全屏显示(删除橙色箭头指向的空间)? 最佳答案 这是 iOS 13 的一项更改。用户将开始期望能够滑
我正在尝试在全屏模式下使用 emacs 并使用合适的字体。我有一台运行 Ubuntu 的基于 nvidia 的笔记本电脑。首次加载时,字体很大,认为是 16pt 字体。我使用菜单选项设置了合适的字体(
我有一个 GridView,我使用 BaseAdapter 来调整我的 GridView 中的图像和文本。我有一个问题,当我运行我的应用程序时,我的“GridView”有左、右、上和下边距。我不需要这
是否可以在全屏模式下使用 Android Studio 例如只有我们编码的那个字段应该显示在整个屏幕上。 像这样: 最佳答案 您可以使用“无干扰模式”: 关于全屏 Android Studio,我们在
我正在制作一个应用程序,当手机处于纵向时,我需要 fragment 显示菜单栏(带有设置快捷方式等),但当它处于横向时,我需要全屏。 因此,我有一个管理 2 个 fragment 的 Activity
我想知道是否可以在没有标签栏和导航栏的情况下全屏推送 NavigationController 的 ViewController。我的问题如下: 我的应用在 TabBarController 中嵌入了
我制作了一个1920 * 1048的flash(16:9的屏幕),并将其嵌入到html中喜欢: 问题是: 在 4:3 屏幕中,flash 的宽度为 100%,但顶部和底部有白色填充。看来闪光灯
我想制作一个同时包含多个文本的HTML5视频。 文本应该以不同的css布局(文本颜色、字体、阴影)显示在视频的不同位置(同时) 我试过这个教程http://www.artandlogic.com/bl
需要一些帮助。 我需要水平滑动一次长图像,然后再水平滑动回来...我在这里学习了一个教程 https://css-tricks.com/creating-a-css-sliding-backgroun
我正在制作一个简单的登录页面,并试图将以下图像作为背景全屏:image 我的 CSS 目前看起来像这样: body { background-image: url('/images/bg.jp
我想用透明色覆盖我的背景图片,但颜色没有覆盖背景图片。 这是我的演示: http://jsfiddle.net/farna/73kx2/ CSS 代码: .overlay{ backgroun
有什么方法可以隐藏状态栏并保持操作栏可见吗? 最佳答案 是的,有。您应该在 Activity 的 onCreate 方法中设置 FLAG_FULLSCREEN。 @Override public vo
我是一名优秀的程序员,十分优秀!