如何在我的地块周围放置更多空白? [英] How to put more whitespace around my plots?

查看:52
本文介绍了如何在我的地块周围放置更多空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图形,其中包含两行两列的子图,像这样:

I have a figure that contains two subplots in two rows and one column like so:

fig, (ax1, ax2) = subplots(
    nrows=2,
    ncols=1,
)

这两个子图是饼图,因此我希望它们的轴为正方形.浏览API后,以下应该是调整每个子图轴的正确方法:

The two subplots are pie charts, therefore I want their axes to be square. After browsing the API, the following should be the correct way to adjust the axes of each subplot:

ax1.axis([0, 8, 0, 8])
ax2.axis([0, 8, 0, 8])

然后,我调整整个图形的大小.由于每个子图的宽度均为8英寸,因此我也将图形的宽度设置为8英寸,并将图形的高度设置为2 * 8 = 16英寸:

Then I adjust the size of the entire figure. Since the width of each subplot is 8 inches, I set the width of the figure to 8 inches as well and the height of the figure to 2*8=16 inches:

fig.set_size_inches(8, 16)

这些设置会生成格式良好的饼图.但是,当我将图保存到PDF文件时,在图的右边缘会切掉一个馅饼楔形的标签.在保存的文件中,该图比在预览窗口中更窄.这就是为什么我想沿 x 轴加宽图形但保留子图的轴.如果我只是尝试通过执行以下操作来扩大数字:

These settings result in nicely formatted pie charts. However, a label for one of the wedges of a pie is cut off at the right edge of the figure when I save the figure to a PDF file. The figure is more narrow in the saved file than in the preview window. That's why I want to widen the figure along the x axis but by retaining the axes of the subplots. If I just try to widen the figure by doing:

fig.set_size_inches(12, 16)

现在这个数字越来越宽,但不幸的是,这两个饼图也是如此.根据我的理解, axis() 调用应该保留子图的尺寸,但它们没有.在阅读并尝试了很多有关自动缩放,xlim,ylim等的内容之后,我感到更加困惑,并且没有任何效果.

The figure gets wider now but, unfortunately, the two pie charts as well. From my understanding, the axis() calls should retain the dimensions of the subplots but they don't. After reading and trying out a lot about autoscale, xlim, ylim etc., I'm even more confused and nothing works.

我只是想通过保留子图的尺寸来加宽图形,以便沿x轴有更多的空白,并且可以显示标签而不会将其剪掉.我该怎么办?

I simply want to widen the figure by retaining the dimensions of the subplots so that there is more whitespace along the x axis and the labels can be displayed without cutting them off. How do I do that?

推荐答案

我建议使用 fig.tight_layout.你有大约两条路径,要么调整 pad

fig.tight_layout(pad=2)

将所有边缘从图形边缘移入或

which while move all of your edges in from the edge of the figure or

fig.tight_layout(rect=[0,0,.8,1]) 

这将调整子图被挤入的框的大小(以图形的分数为单位).这仅适用于较新的(1.2 或更高版本)版本.

which will adjust the size of the box (in units of fraction of the figure) that the sub-plots are squeezed into. This only works in newer (1.2 or later) versions.

您也可以手动创建子图(这很烦人,但可以让您完全控制)或使用

You can also create you sub-plots by hand (which is super annoying, but gives you complete control) or use gridspec. Layout out sub-plots seems to be a problem matplotlib has a large number of ways to solve.

这样做的烦人的方式是

fig = plt.figure()


top_offset = .07
left_offset = .15
right_offset = .2
bottom_offset = .13
hgap = .1
ax_width = 1-left_offset - right_offset
ax_height = (1-top_offset - bottom_offset - hgap)/2

ax1 = fig.add_axes([left_offset, bottom_offset + ax_height + hgap, ax_width, ax_height])
ax2 = fig.add_axes([left_offset, bottom_offset, ax_width, ax_height])

调整了5个参数,使您的人物看起来更好.

with the 5 parameters tweaked to be what ever makes your figures look good.

这篇关于如何在我的地块周围放置更多空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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