Legend outside the axis


In [1]:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
import numpy as np
%matplotlib inline

In [2]:
x = np.linspace(0.0, 2.0*np.pi, 100)
y = np.sin(x)

In [3]:
f = plt.figure()

ax_sidebox = f.add_axes([0.0, 0.05, 0.22, 0.9])
ax = f.add_axes([0.32, 0.05, 0.65, 0.9])

ax_sidebox.grid(False)
ax_sidebox.set_xticks(())
ax_sidebox.set_yticks(())


sidebox_text = ['text1', 'text2', 'text3']
text_xpos       = 0.03
text_ypos_start = 0.85
text_vspace     = 0.1

for i,text in enumerate(sidebox_text):
    f.text(text_xpos, text_ypos_start-i*text_vspace, text, transform=ax.transAxes, fontsize=18)
    
line1 = ax.plot(x,y)
line2 = ax.plot(x,0.5*y)

ax_sidebox.legend(line1, (r'$y_{1}$',), loc=(0.1, 0.5), fontsize=15)
f.legend(line2, (r'$y_{2}$',), loc=(0.045, 0.4), fontsize=15)


Out[3]:
<matplotlib.legend.Legend at 0x7f5d90738e10>

Top caption box with a single axis


In [4]:
f,a = plt.subplots(figsize=(9,6))
a.plot(x,y)

# - Top caption box
f.subplots_adjust(top=0.8)
caption_box = mpatches.Rectangle((0.0, 1.0), 1.0, 0.2, clip_on=False, transform=a.transAxes,  edgecolor='k', facecolor='white', linewidth=1.0)
a.text(0.4, 1.09, 'This text is inside the top figure caption box', horizontalalignment='center', transform=a.transAxes)
a.add_artist(caption_box)

# - Custom legend
curve = mlines.Line2D([], [], linestyle="-", color='C0', label=r'$\sin\alpha$')
legend_handles = [curve]
a.legend(handles=legend_handles, loc=(0.8, 1.07));