I want to reduce the size of the text in the legend of my plot so that the tikz numbers could stay relatively large. The problem is when I reduce the size of the font in the leg
I am experiencing the same issue in Python 2.7 and 3.3, Ubuntu 15.04, matplotlib 1.4.2, and using the Agg backend. It seems that after setting directly the fontsize
of the legend's text artists, either through plt.setp
or using the set_size
method, the text does not properly center-aligned vertically with its corresponding handle, no matter the option used for the vertical alignment of the text ('center', 'top', 'bottom', 'baseline').
The only way I've found to address this issue is to manually align the height of the bounding boxes of the text to the height of the bounding boxes of the handles. Based on the code you provided in your OP, below is an example that shows how this can be done:
import matplotlib as mpl
mpl.use('TkAgg')
import numpy as np
from matplotlib import pyplot as plt
plt.close('all')
plt.rc('text', usetex = True)
font = {'family' : 'normal',
'weight' : 'normal',
'size' : 25}
plt.rc('font', **font)
fig = plt.figure()
fig.set_size_inches(14.5, 10.5)
ax = fig.add_axes([0.1, 0.1, 0.85, 0.75])
#---- plot some data ----
ax.plot(np.arange(10), np.random.randn(10), 'rd', ms=15,
label='The quick brown fox...')
ax.errorbar(np.arange(10), np.random.randn(10), yerr=0.5, fmt='o',
color='g', ecolor='g', capthick=1,ls = '-', lw=2, elinewidth=1,
label = "... jumps over the lazy dog.")
#---- plot legend ----
legend = plt.legend(bbox_to_anchor=(0., 1.02, 1., 0.), handletextpad=0.3,
loc='lower left', ncol=6, mode="expand", borderaxespad=0,
numpoints=1, handlelength=0.5)
#---- adjust fontsize and va ----
plt.setp(legend.get_texts(), fontsize='15', va='bottom')
#---- set legend's label vert. pos. manually ----
h = legend.legendHandles
t = legend.texts
renderer = fig.canvas.get_renderer()
for i in range(len(h)):
hbbox = h[i].get_window_extent(renderer) # bounding box of handle
tbbox = t[i].get_window_extent(renderer) # bounding box of text
x = tbbox.x0 # keep default horizontal position
y = (hbbox.height - tbbox.height) / 2. + hbbox.y0 # vertically center the
# bbox of the text to the bbox of the handle.
t[i].set_position((x, y)) # set new position of the text
plt.show(block=False)
plt.savefig('legend_text_va.png')
which results in:
Setting va='bottom'
in plt.setp
seems to yield better results. I believe that the bbox of the text is more accurate when the vertical alignment is set to bottom, but I haven't investigated further to confirm this.