njs
2015-05-22 19:30:10 UTC
I've tried several methods on stackoverflow
(http://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box)
and I'm still seeing issues with matplotlib cutting off my legend. The
figure and code are posted below, note that I am using
fig.savefig(fname,bbox_extra_artists = (lgd,),bbox_inches = "tight")
Also, the legend handler doesn't appear to be working correctly and the
suptitle get's cut off which makes me think there's something major I'm
messing up that I haven't yet found. Oddly, adding fig.tight_layout() causes
overlap and the legend to get pulled back inside the figure (see second
figure).
Note that I'm also using mpl 1.4.3. Thanks for any help offered, and
apologies for asking a question that has appeared many times!
Nick
<Loading Image...>
<Loading Image...>
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import h5py as h5
from matplotlib.legend_handler import HandlerLine2D
from matplotlib.ticker import MultipleLocator,FormatStrFormatter
majorLocator = MultipleLocator(5)
majorFormatter = FormatStrFormatter('%d')
minorLocator = MultipleLocator(1)
LagLabel = ['','-3 to 3','2 to 8','7 to 13','12 to 18','17 to 23','22 to
28','27 to 33']
rc = plt.rcParams
rc['font.family'] = 'arial'
rc['xtick.direction'] = 'out'
rc['xtick.major.width'] = 2
rc['xtick.labelsize'] = 'medium'
rc['ytick.major.width'] = 2
rc['ytick.direction'] = 'out'
rc['ytick.labelsize'] = 'medium'
rc['grid.linewidth'] = 1
rc['grid.linestyle'] = ':'
#rc['axes.labelweight'] = 'regular'
rc['axes.linewidth'] = 2
rc['axes.labelsize'] = 'large'
rc['legend.fancybox'] = True
fig,ax = plt.subplots(3,1,sharex = True)
fig.subplots_adjust(right = 0.75)
l1, = ax[0].plot(BSBin1[0,:],linewidth = 2,color = '#66c2a5',
marker = 'o',label = varNames[0])
l2, = ax[0].plot(BSBin1[1,:],linewidth = 2,color = '#fc8d62',
marker = 'o',label = varNames[1])
l3, = ax[0].plot(BSBin1[2,:],linewidth = 2,color = '#8da0cb',
marker = 'o',label = varNames[2])
l4, = ax[0].plot(BSBin1[3,:],linewidth = 2,color = '#e78ac3',
marker = 'o',label = varNames[3])
l5, = ax[0].plot(BSBin1[4,:],linewidth = 2,color = '#a6d854',
marker = 'o',label = varNames[4])
l1, = ax[1].plot(BSBin2[0,:],linewidth = 2,color = '#66c2a5',
marker = 'o',label = varNames[0])
l2, = ax[1].plot(BSBin2[1,:],linewidth = 2,color = '#fc8d62',
marker = 'o',label = varNames[1])
l3, = ax[1].plot(BSBin2[2,:],linewidth = 2,color = '#8da0cb',
marker = 'o',label = varNames[2])
l4, = ax[1].plot(BSBin2[3,:],linewidth = 2,color = '#e78ac3',
marker = 'o',label = varNames[3])
l5, = ax[1].plot(BSBin2[4,:],linewidth = 2,color = '#a6d854',
marker = 'o',label = varNames[4])
l1, = ax[2].plot(BSBin3[0,:],linewidth = 2,color = '#66c2a5',
marker = 'o',label = varNames[0])
l2, = ax[2].plot(BSBin3[1,:],linewidth = 2,color = '#fc8d62',
marker = 'o',label = varNames[1])
l3, = ax[2].plot(BSBin3[2,:],linewidth = 2,color = '#8da0cb',
marker = 'o',label = varNames[2])
l4, = ax[2].plot(BSBin3[3,:],linewidth = 2,color = '#e78ac3',
marker = 'o',label = varNames[3])
l5, = ax[2].plot(BSBin3[4,:],linewidth = 2,color = '#a6d854',
marker = 'o',label = varNames[4])
l6, = ax[0].plot(BSClimo1,linewidth = 2,color = 'k',
marker = 'o',label = 'Climo')
l6, = ax[1].plot(BSClimo2,linewidth = 2,color = 'k',
marker = 'o',label = 'Climo')
l6, = ax[2].plot(BSClimo3,linewidth = 2,color = 'k',
marker = 'o',label = 'Climo')
# Set Titles
ax[0].set_title('a. Below Normal',fontsize = 12)
ax[1].set_title('b. Normal',fontsize = 12)
ax[2].set_title('c. Above Normal',fontsize = 12)
ax[1].set_ylabel('Brier Score')
ax[2].set_xlabel('Lag')
ax[0].grid(True); ax[1].grid(True); ax[2].grid(True)
ax[0].set_ylim((.1,.25)); ax[1].set_ylim((.1,.25));
ax[2].set_ylim((.1,.25))
ax[2].set_xticks(np.arange(0,31,5))
ax[2].xaxis.set_major_locator(majorLocator)
ax[2].xaxis.set_minor_locator(minorLocator)
ax[2].xaxis.set_ticks_position('bottom')
ax[2].set_xticklabels(LagLabel,rotation = 45,ha = 'right')
ax[0].xaxis.set_ticks_position('bottom')
ax[1].xaxis.set_ticks_position('bottom')
ax[2].xaxis.set_ticks_position('bottom')
plt.suptitle('{0} Brier Score | 1979-2013'.format(season),fontsize = 14,
fontweight = 'bold')
handles,labels = ax[0].get_legend_handles_labels()
lgd = fig.legend(handles,labels,bbox_to_anchor = (1.05,.75),loc =
'center right',
handler_map = {l1: HandlerLine2D(numpoints = 1),
l2: HandlerLine2D(numpoints = 1),
l3: HandlerLine2D(numpoints = 1),
l4: HandlerLine2D(numpoints = 1),
l5: HandlerLine2D(numpoints = 1),
l6: HandlerLine2D(numpoints = 1)})
fname = 'idp_brier_scores.jpeg'
fig.savefig(fname,bbox_extra_artists = (lgd,),bbox_inches = "tight")
plt.close('all')
--
View this message in context: http://matplotlib.1069221.n5.nabble.com/Legend-cut-off-figure-tp45595.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
(http://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box)
and I'm still seeing issues with matplotlib cutting off my legend. The
figure and code are posted below, note that I am using
fig.savefig(fname,bbox_extra_artists = (lgd,),bbox_inches = "tight")
Also, the legend handler doesn't appear to be working correctly and the
suptitle get's cut off which makes me think there's something major I'm
messing up that I haven't yet found. Oddly, adding fig.tight_layout() causes
overlap and the legend to get pulled back inside the figure (see second
figure).
Note that I'm also using mpl 1.4.3. Thanks for any help offered, and
apologies for asking a question that has appeared many times!
Nick
<Loading Image...>
<Loading Image...>
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import h5py as h5
from matplotlib.legend_handler import HandlerLine2D
from matplotlib.ticker import MultipleLocator,FormatStrFormatter
majorLocator = MultipleLocator(5)
majorFormatter = FormatStrFormatter('%d')
minorLocator = MultipleLocator(1)
LagLabel = ['','-3 to 3','2 to 8','7 to 13','12 to 18','17 to 23','22 to
28','27 to 33']
rc = plt.rcParams
rc['font.family'] = 'arial'
rc['xtick.direction'] = 'out'
rc['xtick.major.width'] = 2
rc['xtick.labelsize'] = 'medium'
rc['ytick.major.width'] = 2
rc['ytick.direction'] = 'out'
rc['ytick.labelsize'] = 'medium'
rc['grid.linewidth'] = 1
rc['grid.linestyle'] = ':'
#rc['axes.labelweight'] = 'regular'
rc['axes.linewidth'] = 2
rc['axes.labelsize'] = 'large'
rc['legend.fancybox'] = True
fig,ax = plt.subplots(3,1,sharex = True)
fig.subplots_adjust(right = 0.75)
l1, = ax[0].plot(BSBin1[0,:],linewidth = 2,color = '#66c2a5',
marker = 'o',label = varNames[0])
l2, = ax[0].plot(BSBin1[1,:],linewidth = 2,color = '#fc8d62',
marker = 'o',label = varNames[1])
l3, = ax[0].plot(BSBin1[2,:],linewidth = 2,color = '#8da0cb',
marker = 'o',label = varNames[2])
l4, = ax[0].plot(BSBin1[3,:],linewidth = 2,color = '#e78ac3',
marker = 'o',label = varNames[3])
l5, = ax[0].plot(BSBin1[4,:],linewidth = 2,color = '#a6d854',
marker = 'o',label = varNames[4])
l1, = ax[1].plot(BSBin2[0,:],linewidth = 2,color = '#66c2a5',
marker = 'o',label = varNames[0])
l2, = ax[1].plot(BSBin2[1,:],linewidth = 2,color = '#fc8d62',
marker = 'o',label = varNames[1])
l3, = ax[1].plot(BSBin2[2,:],linewidth = 2,color = '#8da0cb',
marker = 'o',label = varNames[2])
l4, = ax[1].plot(BSBin2[3,:],linewidth = 2,color = '#e78ac3',
marker = 'o',label = varNames[3])
l5, = ax[1].plot(BSBin2[4,:],linewidth = 2,color = '#a6d854',
marker = 'o',label = varNames[4])
l1, = ax[2].plot(BSBin3[0,:],linewidth = 2,color = '#66c2a5',
marker = 'o',label = varNames[0])
l2, = ax[2].plot(BSBin3[1,:],linewidth = 2,color = '#fc8d62',
marker = 'o',label = varNames[1])
l3, = ax[2].plot(BSBin3[2,:],linewidth = 2,color = '#8da0cb',
marker = 'o',label = varNames[2])
l4, = ax[2].plot(BSBin3[3,:],linewidth = 2,color = '#e78ac3',
marker = 'o',label = varNames[3])
l5, = ax[2].plot(BSBin3[4,:],linewidth = 2,color = '#a6d854',
marker = 'o',label = varNames[4])
l6, = ax[0].plot(BSClimo1,linewidth = 2,color = 'k',
marker = 'o',label = 'Climo')
l6, = ax[1].plot(BSClimo2,linewidth = 2,color = 'k',
marker = 'o',label = 'Climo')
l6, = ax[2].plot(BSClimo3,linewidth = 2,color = 'k',
marker = 'o',label = 'Climo')
# Set Titles
ax[0].set_title('a. Below Normal',fontsize = 12)
ax[1].set_title('b. Normal',fontsize = 12)
ax[2].set_title('c. Above Normal',fontsize = 12)
ax[1].set_ylabel('Brier Score')
ax[2].set_xlabel('Lag')
ax[0].grid(True); ax[1].grid(True); ax[2].grid(True)
ax[0].set_ylim((.1,.25)); ax[1].set_ylim((.1,.25));
ax[2].set_ylim((.1,.25))
ax[2].set_xticks(np.arange(0,31,5))
ax[2].xaxis.set_major_locator(majorLocator)
ax[2].xaxis.set_minor_locator(minorLocator)
ax[2].xaxis.set_ticks_position('bottom')
ax[2].set_xticklabels(LagLabel,rotation = 45,ha = 'right')
ax[0].xaxis.set_ticks_position('bottom')
ax[1].xaxis.set_ticks_position('bottom')
ax[2].xaxis.set_ticks_position('bottom')
plt.suptitle('{0} Brier Score | 1979-2013'.format(season),fontsize = 14,
fontweight = 'bold')
handles,labels = ax[0].get_legend_handles_labels()
lgd = fig.legend(handles,labels,bbox_to_anchor = (1.05,.75),loc =
'center right',
handler_map = {l1: HandlerLine2D(numpoints = 1),
l2: HandlerLine2D(numpoints = 1),
l3: HandlerLine2D(numpoints = 1),
l4: HandlerLine2D(numpoints = 1),
l5: HandlerLine2D(numpoints = 1),
l6: HandlerLine2D(numpoints = 1)})
fname = 'idp_brier_scores.jpeg'
fig.savefig(fname,bbox_extra_artists = (lgd,),bbox_inches = "tight")
plt.close('all')
--
View this message in context: http://matplotlib.1069221.n5.nabble.com/Legend-cut-off-figure-tp45595.html
Sent from the matplotlib - users mailing list archive at Nabble.com.