Home > Software engineering >  How do I find the size of a legend in a figure?
How do I find the size of a legend in a figure?

Time:08-12

The answer to enter image description here

x = [1,2,3,4,5]
y = [1,2,3,4,5]

fig = plt.figure()
axes = fig.subplots()
axes.plot(x,y, label="test")
legend = plt.legend()
fig.canvas.draw()
print(f'Height: {legend.get_window_extent().height/100} in')
print(f'Width: {legend.get_window_extent().width/100} in')

Output:

Height: 0.18 in
Width: 0.55125 in

enter image description here

  • Related