Note that there are some explanatory texts on larger screens.

plurals
  1. POMatplotlib axes resizing to accommodate text
    primarykey
    data
    text
    <p>I have been attempting to use matplotlib to generate a bar graph like this</p> <p><img src="https://i.imgur.com/sDRAY.png" alt="1"></p> <p>However, note that the number above the black bar exceeds the top axis line. I have tried using <code>ax.relim()</code>, <code>ax.autoscale()</code>, <code>ax.autoscale_view()</code>, to correct this problem, but without success. </p> <p>My question is: is there a way to cleanly get the axis size to account for the size of the text displayed by ax.text, without manually computing the height of the text+rectangle and manually resizing appropriately? </p> <p>I am using Python 2.7.3 and Matplotlib 1.3.x</p> <p>EDIT: </p> <p>Manually resizing the window using the text size information obtained from some renderer hacks solves this problem. The code is given below: </p> <pre><code>#!/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') </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload