Matplotlib:图的左边缘和y轴之间的固定间距(Matplotlib: fixed spacing between left edge of figure and y

编程入门 行业动态 更新时间:2024-10-10 19:20:21
Matplotlib:图的左边缘和y轴之间的固定间距(Matplotlib: fixed spacing between left edge of figure and y-axis) python

我用Python 2.7在Matplotlib中制作了2个图。 这些图保存为* .png文件。 保存后,两个图像的分辨率相同 - 宽度= 1099像素,高度= 619像素。

但是,当我垂直对齐保存的* .png图像(附加,下方)时,图像的y轴和最左边的点之间的间距不同 - 请参见下图中的ab

我的意思是,从图像左边到y轴的距离不相同(a不等于b)。

单击图像放大并查看。

问题:有没有办法强制y轴从相对于图像左侧的特定位置开始?

注意:我不关心刻度标签和轴标签之间的空间 - 我可以使用ax.yaxis.labelpad(25)类的东西来调整它。 但是,我不知道如何修复图像左边和y轴之间的空间。

注2:我用以下方法创建我的情节:

fig = plt.figure(1) ax = fig.add_subplot(111) fig.tight_layout()

I have produced 2 plots in Matplotlib with Python 2.7. The plots were saved to *.png files. After saving them, both images have the same resolution - width = 1099 pixels, height = 619 pixels.

However, when I align the saved *.png images vertically (attached, below), the spacing between the y-axis and the left most point of the images is not the same - see a and b in the image below.

By this I mean, the distance from the left of the image to the y-axis is not the same (a is not equal to b).

Click on the image to zoom in and see this.

Question: Is there a way to force the y-axis to start at a particular position relative to the left of the image?

NOTE: I am not concerned about the space between the tick label and the axis label - I can adjust this using something like ax.yaxis.labelpad(25). However, I do not know how to fix the space between the left of the image and the y-axis.

NOTE 2: I create my plot using:

fig = plt.figure(1) ax = fig.add_subplot(111) fig.tight_layout()

最满意答案

如果我想在matplotlib中对图形边距的大小进行精细控制,这就是我通常设置代码的方法。 另外,我展示了如何设置ylabel的位置,这样您就可以轻松地将两个数字的ylabels对齐在一起。

import matplotlib.pyplot as plt plt.close('all') #---- create figure ---- fwidth = 8. # total width of the figure in inches fheight = 4. # total height of the figure in inches fig = plt.figure(figsize=(fwidth, fheight)) #---- define margins -> size in inches / figure dimension ---- left_margin = 0.95 / fwidth right_margin = 0.2 / fwidth bottom_margin = 0.5 / fheight top_margin = 0.25 / fheight #---- create axes ---- # dimensions are calculated relative to the figure size x = left_margin # horiz. position of bottom-left corner y = bottom_margin # vert. position of bottom-left corner w = 1 - (left_margin + right_margin) # width of axes h = 1 - (bottom_margin + top_margin) # height of axes ax = fig.add_axes([x, y, w, h]) #---- Define the Ylabel position ---- # Location are defined in dimension relative to the figure size xloc = 0.25 / fwidth yloc = y + h / 2. ax.set_ylabel('yLabel', fontsize=16, verticalalignment='top', horizontalalignment='center') ax.yaxis.set_label_coords(xloc, yloc, transform = fig.transFigure) plt.show(block=False) fig.savefig('figure_margins.png')

这导致8英寸x 4英寸的数字,在图的左侧,右侧,底部和顶部具有正好0.95,0.2,0.5和0.25英寸的边距。 这种方法的一个好处是边距的大小以绝对单位(英寸)定义,这意味着即使您更改图形的大小,它们也将保持一致。

至于ylabel,水平地,标签的顶部位于距图的左边缘0.25英寸处,而标签的中心垂直地对应于轴的中心。 请注意,由于ylabel上的90度旋转, verticalalignment和horizontalalignment的含义实际上是反转的。

下面显示了上面代码的输出,y轴限制分别设置为[0,1]和[0,18]。

在此处输入图像描述 在此处输入图像描述

This is how I usually setup my code if I want to have a fine control over the size of the figure's margins in matplotlib. In addition, I show how the position of the ylabel can be setup, so you can easily align the ylabels of your two figures together.

import matplotlib.pyplot as plt plt.close('all') #---- create figure ---- fwidth = 8. # total width of the figure in inches fheight = 4. # total height of the figure in inches fig = plt.figure(figsize=(fwidth, fheight)) #---- define margins -> size in inches / figure dimension ---- left_margin = 0.95 / fwidth right_margin = 0.2 / fwidth bottom_margin = 0.5 / fheight top_margin = 0.25 / fheight #---- create axes ---- # dimensions are calculated relative to the figure size x = left_margin # horiz. position of bottom-left corner y = bottom_margin # vert. position of bottom-left corner w = 1 - (left_margin + right_margin) # width of axes h = 1 - (bottom_margin + top_margin) # height of axes ax = fig.add_axes([x, y, w, h]) #---- Define the Ylabel position ---- # Location are defined in dimension relative to the figure size xloc = 0.25 / fwidth yloc = y + h / 2. ax.set_ylabel('yLabel', fontsize=16, verticalalignment='top', horizontalalignment='center') ax.yaxis.set_label_coords(xloc, yloc, transform = fig.transFigure) plt.show(block=False) fig.savefig('figure_margins.png')

This results in a 8in x 4in figure, with margins of exactly 0.95, 0.2, 0.5, and 0.25 inch at the left, right, bottom and top of the figure. One benefit of this approach is that the size of the margins are defined in absolute units (inches), meaning they will remain consistent even if you change the size of the figure.

As for the ylabel, horizontally, the top of the label is located 0.25 inch from the left edge of the figure, while vertically the centre of the label corresponds to the centre of the axe. Note that due to the 90 degrees rotation on the ylabel, the meaning of the verticalalignment and horizontalalignment are in reality inverted.

Below are shown outputs of the code above with the yaxis limits set to [0, 1] and to [0, 18] respectively.

enter image description here enter image description here

更多推荐

本文发布于:2023-04-29 01:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334442.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间距   边缘   fixed   Matplotlib   edge

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!