gpt4 book ai didi

python - matplotlib:使用 bbox_inches ='tight' 保存无花果忽略注释

转载 作者:太空宇宙 更新时间:2023-11-04 05:58:44 27 4
gpt4 key购买 nike

似乎 savefig 中的 bbox_inches='tight' 选项忽略了没有文本的注释。这是我的示例代码

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([0.2, 0.2, 0.6, 0.6])
arrow1 = ax.annotate('text', xy = [0.5,-0.2], xycoords = 'axes fraction', \
xytext = [-72,0], textcoords = 'offset points', \
arrowprops = dict(arrowstyle = '-|>', mutation_scale = 10.0, \
shrinkA = 0, shrinkB = 0, linewidth = 1))
arrow2 = ax.annotate('', xy = [0.5,1.2], xycoords = 'axes fraction', \
xytext = [-72,0], textcoords = 'offset points', \
arrowprops = dict(arrowstyle = '-|>', mutation_scale = 10.0, \
shrinkA = 0, shrinkB = 0, linewidth = 1))
fig.savefig('test.png')
fig.savefig('test-tight.png', bbox_inches = 'tight')

这里是test.png,显示我有两个注释。一个注释,带文本,位于坐标轴下方,另一种注释,不带文本,位于坐标轴上方。

enter image description here

这里是test-tight.png,只显示了一个注解。轴上方没有文本的注释已被忽略。

enter image description here

查看源代码,bbox_inches='tight' 尝试通过调用 artist.get_window_extent() 找到艺术家的大小和位置。当我尝试 arrow1.get_window_extent() 时,我得到一个似乎与文本相对应的边界框。当我尝试 arrow2.get_window_extent() 时,我得到了一个零高度和零宽度的边界框。因此,问题的根源是 .get_window_extent() 不包含箭头。

关于如何以合理稳健的方式解决这个问题的任何想法?如果我能以某种方式获得整个注释的正确边界框,那么我就可以做生意了。但是,我什至无法从 arrow2 中获取线或补丁对象。

以防万一,我使用的是 matplotlib 1.4.0、Python 2.7.6 和 Mac OS X 10.8.5

最佳答案

我实际上将此错误报告为 issue 3816早在 2014 年,它就在 matplotlib v1.4.3 中得到修复。很抱歉没有早点发布这个结果。

关于python - matplotlib:使用 bbox_inches ='tight' 保存无花果忽略注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26321932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com