Matplotlib和Ipython-notebook:准确显示将保存的数字 [英] Matplotlib and Ipython-notebook: Displaying exactly the figure that will be saved

查看:274
本文介绍了Matplotlib和Ipython-notebook:准确显示将保存的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用ipython-notebook进行数值分析和数据绘图。在准备出版质量图的过程中,有很多调整来使布局恰到好处,但是我无法让ipython / matplotlib向我展示我将在浏览器中保存的内容。让这个过程比它应该更痛苦,因为我必须继续打开新的输出文件来检查它。

I am using ipython-notebook a lot at the moment for numerical analysis and plotting of data. In the process of preparing publication quality plots there is a lot of tweaking to get the layout just right, however I can't get ipython/matplotlib to show me what I will be saving in the browser. Making the process more painful than it should be because I have to keep opening the new output file to check it.

有没有办法让内联显示的图像与保存的图像相同?

Is there a way to get the image that is displayed inline to be the same as the image that is saved?

示例如下,facecolor ='grey'为清晰起见:

Example as follows, facecolor='gray' for clarity:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

fig = plt.figure(figsize=(6,4),facecolor='gray')
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
x = np.linspace(0,2*np.pi,1000)
y = np.sin(x)
ax.plot(x,y,label=r'$\sin(x)$')
ax.set_xlim(0,2*np.pi)
ax.set_ylim(-1.2,1.2)
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.legend(loc='upper right', frameon=False)
fig.savefig('mypath.png',dpi=300, facecolor='gray')
plt.show()

请注意,我明确选择了我的轴尺寸,使它们与所得图像的两边等距。这在保存的图像中受到尊重,但在笔记本中显示的图像中被忽略:

Note here I have explicity chosen my axes dimensions so that they are equidistant from the two sides of the resulting image. This is respected in the saved image, but ignored in the image shown in the notebook:

笔记本显示图像:

保存图像:

推荐答案

正如@andrew所说,ipython魔术正在强制执行 bbox_inches ='tight'默认情况下。这可以使用其他魔法覆盖,如 ipython文档中所述

As noted by @andrew, the ipython magics are enforcing bbox_inches='tight' by default. This can be overridden using other magics as explained in the ipython documentation:

%matplotlib inline
%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}

生成的内嵌图像与savefig生成的图像相同。

produces an inline image identical to that produced by savefig.

这篇关于Matplotlib和Ipython-notebook:准确显示将保存的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆