Alignment of text and icon in the legend field (matplotlib)

I want to reduce the size of the text in the legend of my story so that tikz numbers can remain relatively large. The problem is that I am decreasing the font size in the legend, the alignment between the icon in the legend and the text is getting worse. How can I knit them again? Below is the test:

import numpy as np
from  matplotlib import pyplot as plt
plt.rc('text', usetex = True)
font = {'family' : 'normal',
        'weight' : 'normal',
        'size'   : 25}
plt.rc('font', **font)
fig, ax = plt.subplots(1, 1)
a = np.arange(10)
b = np.random.randn(10)
ax.errorbar(a, b, yerr=0.5, fmt='o', color='g', ecolor='g', capthick=1, linestyle = '-', linewidth=2, elinewidth=1, label = "Test")
legend = plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=2, ncol=6, mode="expand", borderaxespad=0.2)
plt.setp(legend.get_texts(), fontsize='15') #legend 'list' fontsize
fig.set_size_inches(14.5, 10.5)
plt.savefig('/Users/Cupitor/Test.png')

As you can see in the figure below, the text in the legend is not more central to the green figure on the left: enter image description here

+1
source share
1 answer

Python 2.7 3.3, Ubuntu 15.04, matplotlib 1.4.2 Agg. , fontsize plt.setp, set_size , , ( "", "", "", " " ).

, , - . , OP, , , :

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 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')

:

enter image description here

va='bottom' plt.setp, -, . , bbox , , , .

+1

Source: https://habr.com/ru/post/1618577/


All Articles