matplotlib savefig-切掉的文本 [英] matplotlib savefig - text chopped off

查看:39
本文介绍了matplotlib savefig-切掉的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我创建了一个情节:

 将matplotlib.pyplot导入为pltplt.clf()将numpy导入为npprops = np.random.randint(0,100,(200,))x = np.arange(1,201,1)plt.plot(x,props,c='k')plt.xlabel('等等等等等等\n 等等等等')plt.ylabel('等等等等等等等等等等')图= plt.gcf()fig.set_size_inches(3.75,3.75)#14, 23)plt.savefig('testfig.png',dpi=300)

当使用 Ipython(通过 Spyder)时,情节显示正常.但是,当我查看保存的图像时,它呈现为:

如您所见,文本被截断.处理这种情况的推荐做法是什么?

我通过增加图形大小和之后重新调整大小来解决它.但是,我的目标是生成一组具有一致文本大小(图形大小不同)的图像;所以这种方法并不理想.

注意.虽然存在类似问题,但这个问题的不同之处在于:>

  • 处理xlabelylabel.
  • set_size_inches 功能
  • 结合
  • 力求确保不同图形大小的文本大小保持一致.
  • 寻求找出Ipython输出为何不同的原因从savefig

解决方案

Spyder中的Ipython控制台使用 inline 后端,该后端将图形另存为png并显示输出图像.保存时,它使用选项bbox_inches = "tight".

因此,为了获得与控制台中显示的图形相同的图形,您也可以决定使用此选项 - 它基本上扩展或缩小了边界框,以便显示画布中的所有对象.

plt.savefig('testfig.png',dpi=300, bbox_inches = "tight")

或者,可以在保存或显示图形之前确保所有对象都已经在图形边界内.这可以使用

来完成

  plt.tight_layout()

尝试自动执行此操作,或者您可以使用

plt.subplots_adjust(left=0.3, right=0.9, bottom=0.3, top=0.9)

其中的参数表示图形大小分数单位的每一边距(左边为30%,右边为10%,依此类推).

Say I create a plot:

import matplotlib.pyplot as plt
plt.clf()
import numpy as np
props = np.random.randint(0,100,(200,))
x=np.arange(1,201,1)
plt.plot(x,props,c='k')
plt.xlabel('blah blah blah blah blah blah\n blah blah blah blah blah')
plt.ylabel('blah blah blah blah blah blah blah blah blah')
fig=plt.gcf()
fig.set_size_inches(3.75,3.75)#14, 23)
plt.savefig('testfig.png',dpi=300)

When using Ipython (via Spyder), the plot presents ok. However when I looked at the saved image, it presents thus:

As you can see, the text is cut off. What is recommended practice for dealing with this?

I have got round it by increasing the figure size, and re-sizing afterwards. However, my aim is to produce a set of images with a consistent text size (figure size varies); so this approach is not ideal.

Note. Whilst a similar question exists, this question is distinct in that it:

  • deals with both xlabel and ylabel.
  • combines with set_size_inchesfunction
  • seeks to ensure consistent text size with differing figure sizes.
  • seeks to find out why Ipython output differs from savefig

解决方案

The Ipython console in Spyder uses the inline backend, which saves the figure as png and displays the output image. When saving, it uses the option bbox_inches = "tight".

So in order to obtain the same figure as shown in the console, you may decide to use this option as well - it basically extends or shrinks the bounding box such that all objects in the canvas are displayed.

plt.savefig('testfig.png',dpi=300, bbox_inches = "tight")

Alternatively, you can make sure that all objects are already inside the figure boundaries before saving or showing the figure. This can either be accomplished using

plt.tight_layout()

which tries to do that automatically, or you can use

plt.subplots_adjust(left=0.3, right=0.9, bottom=0.3, top=0.9)

where the parameters denote the margins on each side in units of fractions of figure size (30% space on the left, 10% space on the right, etc.).

这篇关于matplotlib savefig-切掉的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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