问题描述:

I am creating python subplots. I have 10 figures and I want to place legend out side of my plot. After trying alot I have came up with this code. However, It is placing the legend in middle, which is not looking nice.

 thres = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

lav_tom= [69.89, 76.92, 77.79, 78.6, 77.17, 72.67, 67.29, 58.73, 38.01]

lav_cloud = [81.12, 85.94, 87.2, 87.8,87.86, 85.77,82.72, 78.02,61.68]

lav_hd =[63.18, 70.07, 71.18, 71.33, 68.9, 63.56, 58.05, 46.51, 26.31]

ax1 =plt.subplot(5,2,1) # the first subplot in the first figure

ax1.plot( thres, lav_tom, 'ks--', label = "TC")

ax1.plot( thres, lav_cloud, 'ko--', label ="CS")

ax1.plot( thres, lav_hd, 'k*-', label ="HD")

ax1.set_ylim(0,100)

ax1.set_xlim(0.1, 0.9)

ax1.xaxis.set_visible(False)

ax1.set_title("LAV")

fontP = FontProperties()

fontP.set_size('small')

**plt.legend( loc= 'upper left', bbox_to_anchor = (0.54,0.02), ncol=3)**

lmv_tom = [69.89, 76.92, 77.79, 78.6, 77.17, 72.67, 67.29, 58.73, 38.01]

lmv_cloud = [81.12, 85.94, 87.2, 87.8,87.86, 85.77,82.72, 78.02,61.68]

lmv_hd = [63.18, 70.07, 71.18, 71.33, 68.9, 63.56, 58.05, 46.51, 26.31]

ax2 =plt.subplot(5,2,2) # the second subplot in the first figure

ax2.plot( thres, lmv_tom, 'ks--', label = "TC")

ax2.plot( thres, lmv_cloud, 'ko--', label ="CS")

ax2.plot( thres, lmv_hd, 'k*-', label ="HD")

ax2.set_ylim(0,100)

ax2.set_xlim(0.1, 0.9)

ax2.xaxis.set_visible(False)

ax2.yaxis.set_visible(False)

ax2.set_title("LMV")

lmxv_tom = [69.89, 76.92, 77.79, 78.6, 77.17, 72.67, 67.29, 58.73, 38.01]

lmxv_cloud = [81.12, 85.94, 87.2, 87.8,87.86, 85.77,82.72, 78.02,61.68]

lmxv_hd = [63.18, 70.07, 71.18, 71.33, 68.9, 63.56, 58.05, 46.51, 26.31]

ax3 =plt.subplot(5,2,3) # the second subplot in the first figure

ax3.plot( thres, lmxv_tom, 'ks--', label = "TC")

ax3.plot( thres, lmxv_cloud, 'ko--', label ="CS")

ax3.plot( thres, lmxv_hd, 'k*-', label ="HD")

ax3.set_ylim(0,100)

ax3.set_xlim(0.1, 0.9)

ax3.xaxis.set_visible(False)

ax3.title.set_text("LMXV")

lst_tom = [69.89, 76.92, 77.79, 78.6, 77.17, 72.67, 67.29, 58.73, 38.01]

lst_cloud = [81.12, 85.94, 87.2, 87.8,87.86, 85.77,82.72, 78.02,61.68]

lst_hd = [65.78,72.24,72.94,72.36,70.83,68.24,64.04,56.07,39.9]

ax4 =plt.subplot(5,2,4) # the second subplot in the first figure

ax4.plot( thres, lst_tom, 'ks--', label = "TC")

ax4.plot( thres, lst_cloud, 'ko--', label ="CS")

ax4.plot( thres, lst_hd, 'k*-', label ="HD")

ax4.set_ylim(0,100)

ax4.set_xlim(0.1, 0.9)

ax4.xaxis.set_visible(False)

ax4.yaxis.set_visible(False)

ax4.set_title("LST")

lbaj_tom = [69.89, 76.92, 77.79, 78.6, 77.17, 72.67, 67.29, 58.73, 38.01]

lbaj_cloud = [81.12, 85.94, 87.2, 87.8,87.86, 85.77,82.72, 78.02,61.68]

lbaj_hd = [62.52,69,71.72,71.13,67.96,62.56,54.28,42.67,27.54]

ax5 =plt.subplot(5,2,5) # the second subplot in the first figure

ax5.plot( thres, lbaj_tom, 'ks--', label = "TC")

ax5.plot( thres, lbaj_cloud, 'ko--', label ="CS")

ax5.plot( thres, lbaj_hd, 'k*-', label ="HD")

ax5.set_ylim(0,100)

ax5.set_xlim(0.1, 0.9)

ax5.xaxis.set_visible(False)

ax5.set_title("LBAJ")

lbar_tom = [69.89, 76.92, 77.79, 78.6, 77.17, 72.67, 67.29, 58.73, 38.01]

lbar_cloud = [81.12, 85.94, 87.2, 87.8,87.86, 85.77,82.72, 78.02,61.68]

lbar_hd = [63.18, 70.07, 71.18, 71.33, 68.9, 63.56, 58.05, 46.51, 26.31]

ax6 =plt.subplot(5,2,6) # the second subplot in the first figure

ax6.plot( thres, lbar_tom, 'ks--', label = "TC")

ax6.plot( thres, lbar_cloud, 'ko--', label ="CS")

ax6.plot( thres, lbar_hd, 'k*-', label ="HD")

ax6.set_ylim(0,100)

ax6.set_xlim(0.1, 0.9)

ax6.xaxis.set_visible(False)

ax6.yaxis.set_visible(False)

ax6.set_title("LBAR")

lbas_tom = [69.89, 76.92, 77.79, 78.6, 77.17, 72.67, 67.29, 58.73, 38.01]

lbas_cloud = [81.12, 85.94, 87.2, 87.8,87.86, 85.77,82.72, 78.02,61.68]

lbas_hd = [63.18, 70.07, 71.18, 71.33, 68.9, 63.56, 58.05, 46.51, 26.31]

ax7 =plt.subplot(5,2,7) # the second subplot in the first figure

ax7.plot( thres, lbas_tom, 'ks--', label = "TC")

ax7.plot( thres, lbas_cloud, 'ko--', label ="CS")

ax7.plot( thres, lbas_hd, 'k*-', label ="HD")

ax7.set_ylim(0,100)

ax7.set_xlim(0.1, 0.9)

ax7.xaxis.set_visible(False)

ax7.set_title("LBAS")

lboj_tom = [69.89, 76.92, 77.79, 78.6, 77.17, 72.67, 67.29, 58.73, 38.01]

lboj_cloud = [88.66, 88.68, 88.6, 88.55,88.53, 88.43,88.41, 88.34, 88.26]

lboj_hd = [63.18, 70.07, 71.18, 71.33, 68.9, 63.56, 58.05, 46.51, 26.31]

ax8 =plt.subplot(5,2,8) # the second subplot in the first figure

ax8.plot( thres, lboj_tom, 'ks--', label = "TC")

ax8.plot( thres, lboj_cloud, 'ko--', label ="CS")

ax8.plot( thres, lboj_hd, 'k*-', label ="HD")

ax8.set_ylim(0,100)

ax8.set_xlim(0.1, 0.9)

ax8.xaxis.set_visible(False)

ax8.yaxis.set_visible(False)

ax8.set_title("LBOJ")

lbor_tom = [69.89, 76.92, 77.79, 78.6, 77.17, 72.67, 67.29, 58.73, 38.01]

lbor_cloud = [89.42,89.4,89.35,89.41,89.4,89.35,89.33,89.28,89.27]

lbor_hd = [63.18, 70.07, 71.18, 71.33, 68.9, 63.56, 58.05, 46.51, 26.31]

ax9 =plt.subplot(5,2,9) # the second subplot in the first figure

ax9.plot( thres, lbor_tom, 'ks--', label = "TC")

ax9.plot( thres, lbor_cloud, 'ko--', label ="CS")

ax9.plot( thres, lbor_hd, 'k*-', label ="HD")

ax9.set_ylim(0,100)

ax9.set_xlim(0.1, 0.9)

ax9.set_title("LBOR")

lbos_tom = [69.89, 76.92, 77.79, 78.6, 77.17, 72.67, 67.29, 58.73, 38.01]

lbos_cloud = [83.29, 83.1, 83.04, 82.98, 82.84, 82.75, 82.62, 82.52, 82.31]

lbos_hd =[63.18, 70.07, 71.18, 71.33, 68.9, 63.56, 58.05, 46.51, 26.31]

ax10 =plt.subplot(5,2,10) # the second subplot in the first figure

ax10.plot( thres, lbos_tom, 'ks--', label = "TC")

ax10.plot( thres, lbos_cloud, 'ko--', label ="CS")

ax10.plot( thres, lbos_hd, 'k*-', label ="HD")

ax10.set_ylim(0,100)

ax10.set_xlim(0.1, 0.9)

ax10.yaxis.set_visible(False)

ax10.set_title("LBOS")

#plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=2, borderaxespad=0.)

plt.rcParams.update({'font.size': 10})

plt.tight_layout()

plt.show()

Can anyone please suggest some better placement?

相关阅读:
Top