matplotlib中具有标题,轴标签的确切图形大小 [英] Exact figure size in matplotlib with title, axis labels

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

问题描述

曾经有人问过类似的问题,但我的所有搜索结果都没有解决我的问题.使用以下示例代码:

from matplotlib.pyplot import *图=图(1,图大小=(3.25,3))情节([0,1,5,2,9])标题('标题')xlabel('xAxis')ylabel('yAxis')fig.savefig('test.png',dpi = 600)

结果数字为2040x1890像素,即3.4"x3.15",并带有x标签被切断.在图像编辑器中查看 PNG 文件,似乎轴和刻度标签适合所需的大小.我试过与输出大小和请求大小的差异并将其反馈在(3.25-(3.4-3.25)= 3.10中,但matplotlib似乎添加了一个任意缓冲区,它仍然没有达到所需的大小.一个如何制作所需尺寸的整体图?

解决方案

与 David Robinson 的评论一致,这里生成的图是 3.25 x 3 英寸,由 photoshop 测量,尽管 xlabel 确实显示了截止(mplpython 2.6 64位元版1.1.0(win7)

解决该问题的一种方法是使用 subplot_adjust 手动调整边距:

from matplotlib.pyplot import *图=图(1,图大小=(3.25,3))情节([0,1,5,2,9])标题('title')xlabel('xAxis')ylabel('yAxis')subplots_adjust(bottom=0.14) # <--fig.savefig('test.png', dpi=600)

这些边距的默认值在 matploblibrc 文件中设置,您可以在那里永久修改它.在我的例子中,底部边距的默认值为 0.10.

如果您的图形大小错误或正确,就像我的情况一样,您可以使用 subplot_adjust 为标签提供足够的空间.然后,如果需要,您可以像之前一样计算校正以获得您想要的实际图片或图形大小.

保存的图形的最终视图取决于该图形的大小.如果您 show() 您的图形并从 matplotlib 视图框架中保存它,则会在图像中得到标签截断.但是,如果您手动增加图像的大小,则会看到标签出现,如果您保存它,标签也会出现在保存的图像中.可以说这是所见即所得.你的身材非常小,这会让你的标签被剪掉.因此,另一种方法是使用较低的dpi来制作更大的数字,以保持整体尺寸.这也有效:

from matplotlib.pyplot import *fig =图(1,图大小=(6.5,6))#< ---情节([0,1,5,2,9])标题('标题')xlabel('xAxis')ylabel('yAxis')fig.savefig('test.png',dpi = 300)#< ---

在任何情况下,我都将其视为matplolib错误,因为您可以期望在绘制并保存后获得未切割的图形.

Similar questions have been asked before, but all of my search results don't address my problem. Take the following example code:

from matplotlib.pyplot import *
fig = figure(1, figsize=(3.25, 3))
plot([0,1,5,2,9])
title('title')
xlabel('xAxis')
ylabel('yAxis')
fig.savefig('test.png',dpi=600)

The resulting figure is 2040x1890 pixels, or 3.4"x3.15", and the x-label is cut off. Looking at the PNG file in an image editor, it appears that the axes and tick labels fit the desired size. I've tried taking the difference from the output size and requested size and feeding that back in (3.25 - (3.4-3.25) = 3.10, but matplotlib seems to add an arbitrary buffer and it still doesn't come out to the desired size. How does one make an overall figure that is the desired size?

解决方案

In agreement with the comment from David Robinson, the figure produced here is 3.25 by 3 inches as measured by photoshop, although the xlabel does show cut-off (mpl 1.1.0 in python 2.6 64-bit, win7)

A solution to overcome the problem is to manually adjust the margins with subplot_adjust:

from matplotlib.pyplot import *
fig = figure(1, figsize=(3.25, 3))
plot([0, 1, 5, 2, 9])
title('title')
xlabel('xAxis')
ylabel('yAxis')
subplots_adjust(bottom=0.14)   # <--
fig.savefig('test.png', dpi=600) 

The default value of these margins are set in the matploblibrc file and you can modify it there permanently. The default value for the bottom margin in my case was 0.10.

Either if your figure is of a wrong size or correct, as in my case, you can use subplot_adjust to provide enough space for the label. Then if needed, you can calculate the correction to get the actual picture or figure size you want as you already did.

The final view of the saved figure depends on the size of that figure. If you show() your figure and you save it from the matplotlib view frame you get the label cut-off in the image. But if you increase manually the size of the image you will see the label appearing and if you save it then it will also appear in the saved image. Lets say that is WYSIWYG. Your figure is of a very small size and this makes your label to get cut. So another approach is to make a bigger figure maybe with lower dpi to keep overall size. This also works:

from matplotlib.pyplot import *
fig = figure(1, figsize=(6.5, 6))   # <---
plot([0, 1, 5, 2, 9])
title('title')
xlabel('xAxis')
ylabel('yAxis')
fig.savefig('test.png', dpi=300)    # <---

In any case, I would consider this as a matplolib bug, as you could expect to have an uncut figure after plot and save.

这篇关于matplotlib中具有标题,轴标签的确切图形大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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