plot - Laying 2 different legends in one axis in matplotlib's gridspec module -


i'm working on plot using matplotlib's gridspec module. want add 2 different legends on specific axis, 1 mentioned in matplotlib: 2 different legends on same graph.

however, gridspec, 1 legend laid on other axis , 1 lost.

here code:

import matplotlib.pyplot plt import matplotlib.gridspec gridspec  fig = plt.figure() gs1 = gridspec.gridspec(8, 4) gs1.update(left=0.1, right=0.65, bottom=0.06, top=1, hspace=0)  axf = plt.subplot(gs1[0, :]) axe = plt.subplot(gs1[1, :],sharex=axf) axpa = plt.subplot(gs1[2, :],sharex=axf) axmiu = plt.subplot(gs1[3:7, :],sharex=axf) axres = plt.subplot(gs1[7, :],sharex=axf)  hd1=axmiu.errorbar(range(5),map(lambda x:x+1,range(5)), fmt='o',     color='black', mfc='none',markersize=5, label='hehe') hd2=axmiu.errorbar(range(5),map(lambda x:x+2,range(5)), fmt='-',         color='fuchsia',markersize=5, linewidth=2, label='heihei')  first_legend=plt.legend(prop={'size':12},labelspacing=0,handles=[hd2],loc='upper right') axmiu.add_artist(first_legend) plt.legend(prop={'size':12},labelspacing=0,handles=[hd1],loc='right') plt.show() 

you need use axis.legend, not plt.legend.

for example, make final 4 lines:

first_legend=axmiu.legend(prop={'size':12},labelspacing=0,handles=[hd2],loc='upper right') axmiu.add_artist(first_legend) axmiu.legend(prop={'size':12},labelspacing=0,handles=[hd1],loc='right') plt.show() 

enter image description here


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -