Matplotlib轴调整大小以适应文本

3
我一直在尝试使用Matplotlib生成类似于这样的条形图: 1 然而,请注意黑色条上方的数字超过了顶部轴线。我尝试使用 ax.relim()ax.autoscale()ax.autoscale_view() 来解决这个问题,但没有成功。
我的问题是:是否有一种方法可以干净地获取轴大小,以便考虑到由 ax.text 显示的文本的大小,而无需手动计算文本+矩形的高度并手动调整大小?
我正在使用Python 2.7.3和Matplotlib 1.3.x。
编辑:
通过使用从某些渲染器技巧获得的文本大小信息手动调整窗口大小可以解决此问题。下面是代码:
#!/usr/bin/env python

import matplotlib.pyplot as plt

categoryList = ['F', 'L', 'M', 'T', 'W']
categoryColors = {'C': 'y', 'B': 'r', 'F': 'g', 'M': 'b', 'L': 'm', 'T': 'c', 'W': 'k'}
categoryNames = {'C': 'foo1', 'B': 'foo2', 'F': 'foo3', 'M': 'foo4', 'L': 'foo5', 'T': 'foo6', 'W': 'foo7'}
timeList = [60, 165, 60, 10, 570]
fig = plt.figure()
ax = fig.add_subplot(111)
width = 0.35

legendColors = []
legendNames = []
texts = [] 
for ind in range(len(categoryList)):
    category = categoryList[ind]
    rects = ax.bar([ind],timeList[ind],width,color=categoryColors[category],align="center")
    legendColors.append(rects[0])
    legendNames.append(categoryNames[category])

    # Add the text for the number on top of the bar
    for rect in rects:
        texts.append(ax.text(rect.get_x()+rect.get_width()/2,1.05*rect.get_height(),'%d' % timeList[ind],
                    ha='center',va='bottom'))
# The x axis indices
ind = range(0,len(categoryList))

ax.set_ylabel("Time (Minutes)")
ax.set_xlabel("Category")
ax.set_xticks(ind)
ax.set_xticklabels(categoryList)
lgd = ax.legend(legendColors,legendNames,bbox_to_anchor=(1.0, 1.0), loc=2, borderaxespad=0.3)

# Epic hacks for getting renderer
fig.savefig('foo.png',bbox_extra_artists=(lgd,),bbox_inches='tight')
renderer = fig.axes[0].get_renderer_cache()

window_bbox_list = [t.get_window_extent(renderer) for t in texts]
data_bbox_list = [window_bbox.transformed(ax.transData.inverted()) for window_bbox in window_bbox_list]
data_coords_list = [data_bbox.extents for data_bbox in data_bbox_list]
height_list = [coordEntry[-1] for coordEntry in data_coords_list]
max_height = max(height_list)
fig.axes[0].set_ylim(top=max_height*1.05)
fig.savefig('foo.png',bbox_extra_artists=(lgd,),bbox_inches='tight')

fig.axes[0].get_renderer_cache()自matplotlib 3.6版本起已被弃用,相应的代码行现在应该改为renderer = ax.figure.canvas.get_renderer(),但其他部分仍然可以正常工作! - fratajcz
fig.axes[0].get_renderer_cache()自matplotlib 3.6版本起已被弃用,相应的代码行现在应该改为renderer = ax.figure.canvas.get_renderer(),但其他部分仍然可以正常工作! - undefined
1个回答

1

嗯,我真的不认为有一种干净和自动的方法,因为文本是一个对象,它的生命几乎独立于你绘制它的轴。事实上,在你绘制整个图形之前,它并没有实际的尺寸...

方法是跟踪您在图中添加的所有文本,例如执行以下操作:

texts = []
for your_element in your_cycle:
    do something
    t = ax.text(your_text_params)
    texts.append(t)

然后您需要找到文本的最高点。为此,您必须提取窗口坐标信息并将其转换为数据坐标。在此之前,您必须绘制图形,否则文本尚未确定(如果有更好的方法,请纠正我)。

plt.draw()
window_bbox = [t.get_window_extent()  for t in texts]
data_bbox =   [ b.transformed(ax.transData.inverted()) for b in window_bbox ]
data_coords = [ b.extents for b in data_bbox ]

在获取坐标范围后,我们提取每个框的最大值和它们之间的最大值,然后将边框设置为略大于该值。
max_height = [ e[-1] for e in data_coords ]
top = max(max_height)
ax.set_ylim(ymax=top*1.05)

太好了,谢谢!这个方法可行,只需要进行一些渲染技巧来绕过draw()的要求,这样我就可以使用figure.savefig()了。请查看我的编辑以获取完整代码。 - Sam Manzer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接