一种控制新的 axes 应该在 figure 对象中的位置的方法是使用 add_axes 方法.此方法接受一个包含 4 个值的列表或元组,这些值代表 [左、底部、宽度、高度],其中所有数量都是图形宽度和高度的分数.例如:
One way to control where in the figure object a new axes is supposed to go is by using the add_axes method. This method takes in a list or tuple of 4 values which represent [left, bottom, width, height] where all quantities are in fractions of figure witdth and height. For example:
import matplotlib fig = plt.figure() fig.add_axes([0.1,0.2,0.3, 0.4])在指定的相对坐标中添加一个新的 axes 对象.如果我继续尝试提取这些坐标:
Adds a new axes object in the specified relative coordinates. If I then go on to try to extract these coordinates:
fig.axes[0].get_position()结果是一个 Bbox 对象:
>>> Bbox([[0.1, 0.2], [0.4, 0.6000000000000001]])问题是我不需要Bbox对象,我想找回我的相对坐标,这样我就可以使用 set_axes 对其进行操作了(它再次接受了左侧,底部宽度和高度)以重新定位我的坐标轴.
The issue is I don't want a Bbox object, I want to get my relative coordinates back so that I can operate on them using set_axes (which takes in left, bottom width and height again) to relocate my axes.
反正有没有将 Bbox 转换为我需要的格式,或者可能是强制 get_position()方法吐出相对坐标?>
Is there anyway to convert the Bbox to the format that I need, or perhaps to force the get_position() method to spit out relative coordinates like so?
[0.1,0.2,0.3,0.4] 推荐答案图的 add_axes 方法将 rect
add_axes(* args,** kwargs) 在 rect [left, bottom, width, height]
axes.get_position()返回 BBox 对象.从其文档中可以看到,该属性
axes.get_position() returns a BBox object. This has, as seen from its documentation, a property
界限 (属性)返回(x0, y0, width, height).
所以你想打电话
ax.get_position().bounds获取轴矩形的边界.
更多推荐
Matplotlib中使用get和set axis方法的相对坐标
发布评论