Aligning markers for individual points in matplotlib and removing "double" legend for markers in matplotlib in Python -


i trying figure out alignment of markers in "ax.plot" . apart plotting 2 bar graphs, need plot 2 points, 1 per bar graph. here's looking -:

  1. centering/alignment of markers ('o' , '' here, in center of each bar, rather @ edge of bar graph. "o" should come @ center of 1st bar graph , "" should come @ center of 2nd bar graph, individual heights differ though, on scale "performance" -the "o" , "" "performance" objects (right hand side scale, in figure) - centering, means, overlay of markers("o" , "" against respective stacked graph.

  2. removing duplicate marker symbols, 'o' , '*' in legend on upper right corner. and, understanding why happens par2.plot , not ax.bar object. have done without using ax.twinx(), generates 2 scales (one "#candidates" , other "performance" - , if double entry of legend related using 2 scales ? (i hope not)

    for (2), used plt.legend(numpoints=1) before last line, plt,show() according answer here, multiple markers in legend , didn't seem remove "duplicate markers" in context.

also attached graph, (1) , (2) highlighted enter image description here

tip -: ignore looping constructs, part of larger piece, , did not want change while pasting, focus on snippet of entire code (imo, should narrow problem?)

rects1 = ax.bar(ind, current_period, width, color=colors) rects2 = ax.bar(ind+width, next_period, width, color='c') lines_1=par1.plot(perform_1,linestyle='', marker='h', markerfacecolor ='k') lines_2=par1.plot(perform_2,linestyle='', marker='*',markerfacecolor ='m') ax.legend((rects1[0], rects2[0],lines_1[0],lines_2[0]), ('current time period', 'next time period','current period performance', 'next period performance'),prop=dict(size=10) ) 

here complete code used -:

#final plotting file  import numpy np import matplotlib.pyplot plt matplotlib import rc #placing anchored text within figure mpl_toolkits.axes_grid.anchored_artists import anchoredtext rc('mathtext', default='regular')  history_p=[[1.4155322812819471, 4.9723842851306213, 3.6831354714462456, 3.0345047089322521, 5.3355879766963819], [2.3240101637275856, 4.7804345245879354, 7.0829471987293973, 6.1050663075245852, 3.6087166298399973], [3.5770722538162265, 3.4516290562530587, 4.4851829512197678, 5.1158026103364733, 3.7873662329909235], [4.7137003352158136, 5.0792119756378593, 4.4624078437179504, 3.1790266221827754, 4.8711126648436895], [4.8043291762010414, 5.6979872315568576, 3.4869780377350339, 3.892755123606721, 3.8142509389863095], [4.8072846135271492, 4.2055137431209033, 5.0441056822018417, 4.1014759291893306, 5.327936039526822]] history_c=[[14000, 14000, 14000, 14000, 14000], [5373, 18874, 13981, 11519, 20253], [6806, 14001, 20744, 17880, 10569], [12264, 11834, 15377, 17540, 12985], [14793, 15940, 14004, 9977, 15286], [15500, 18384, 11250, 12559, 12307]]  n = 5 ind = np.arange(n)  # x locations groups width = 0.35  def make_patch_spines_invisible(ax):     ax.set_frame_on(true)     ax.patch.set_visible(false)     sp in ax.spines.itervalues():         sp.set_visible(false)  def autolabel(rects):     # attach text labels     rect in rects:         height = rect.get_height()         ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),ha='center', va='bottom')  alphab = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6']  k in range(0,5):     colors=[]     current_period=history_c[k]     next_period = history_c[k+1]     perform_1=history_p[k]     perform_2=history_p[k+1]      in range(0,5):         if perform_1[i]==max(perform_1) :             colors.append('g')             best=i         elif perform_1[i]==min(perform_1):             colors.append('r')             worst=i         elif (perform_1[i] != min(perform_1) or perform_1[i] != max(perform_1)):             colors.append('b')      fig, ax = plt.subplots()     fig.subplots_adjust(right=0.75)      par1 = ax.twinx()     make_patch_spines_invisible(par1)      rects1 = ax.bar(ind, current_period, width, color=colors)     rects2 = ax.bar(ind+width, next_period, width, color='c')     lines_1=par1.plot(perform_1,linestyle='', marker='h', markerfacecolor ='k')     lines_2=par1.plot(perform_2,linestyle='', marker='*',markerfacecolor ='m')     ax.set_xlabel("model #",style='italic',size='large')     ax.set_ylabel("candidate #",style='italic',size='large')     par1.set_ylabel("performance",style='italic',size='large')       ax.set_title('aggregated performace rolled out candidates, per period',style='italic')     #fontdict=dict('fontsize':rcparams['axes.titlesize'],'verticalalignment': 'baseline', 'horizontalalignment': loc)     ax.set_xticks(ind+width)     ax.set_xticklabels( ('m1', 'm2', 'm3', 'm4', 'm5') )     ax.annotate('worst performer', xy=(worst,0),  xycoords='data',xytext=(-30, 30), textcoords='offset points',size=12, va="center", ha="center",arrowprops=dict(arrowstyle="simple", connectionstyle="arc3,rad=-0.2"))     ax.annotate('best performer', xy=(best,0),  xycoords='data',xytext=(-30, 30), textcoords='offset points',size=12, va="center", ha="center",arrowprops=dict(arrowstyle="simple", connectionstyle="arc3,rad=-0.2"))     ax.legend((rects1[0], rects2[0],lines_1[0],lines_2[0]), ('current time period', 'next time period','current period performance', 'next period performance'),prop=dict(size=10) )     #placing anchored text within figure, per period     @ = anchoredtext("time period :"+str(k+1),prop=dict(size=10), frameon=true,loc=2,)     at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")     ax.add_artist(at)     par1.set_ylim(0, 10)      autolabel(rects1)     autolabel(rects2)     plt.show() 

  1. you have provide plot method x-coordinate arguments. if given 1 list-like object, matplotlib use list y-coordinates , use x = np.arange(len(y)) (where y given y-coordinates).

  2. you should not call legend method several times each axes; include numpoints kwarg in original legend call.

in other words, replace lines

lines_1=par1.plot(perform_1,linestyle='', marker='h', markerfacecolor ='k') lines_2=par1.plot(perform_2,linestyle='', marker='*',markerfacecolor ='m') ax.legend((rects1[0], rects2[0],lines_1[0],lines_2[0]), ('current time period', 'next time period','current period performance', 'next period performance'),prop=dict(size=10) ) 

with

lines_1=par1.plot(ind + 0.5*width, perform_1,linestyle='', marker='h', markerfacecolor ='k') lines_2=par1.plot(ind + 1.5*width, perform_2,linestyle='', marker='*',markerfacecolor ='m') ax.legend((rects1[0], rects2[0],lines_1[0],lines_2[0]), ('current time period', 'next time period','current period performance', 'next period performance'),prop=dict(size=10), numpoints=1 ) 

this gives desire output:

enter image description here


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -