def func(kei,y_l,y_up): fns = os.listdir(PATH) print fns data = [] for fn in fns: p = os.path.join(PATH,fn) #print p stat_d = load_obj(p) target_l = stat_d[kei] #target_l = [float(x) for x in target_l] data.append(target_l) data = [data[3],data[4],data[0],data[1],data[2]] r = data[3] r.sort() print r fig, ax1 = plt.subplots(figsize=(10,5)) fig.canvas.set_window_title(tag+' '+kei) plt.subplots_adjust(left=0.15, right=1, top=0.8, bottom=0.2) bp = plt.boxplot(data, notch=0, sym='o', vert=1, whis=1.5) plt.setp(bp['boxes'], color='black') plt.setp(bp['whiskers'], color='black') plt.setp(bp['fliers'], color='lightgrey', marker='x') ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) # Hide these grid behind plot objects ax1.set_axisbelow(True) #ax1.set_title(tag+' '+kei) ax1.set_xlabel('Time since the first post (min)',fontsize=LABEL_FONT) ax1.set_ylabel(y_l,fontsize=LABEL_FONT) plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0),labelsize=LABEL_FONT) plt.rc('font',size=18) # Now fill the boxes with desired colors boxColors = ['white','white'] numBoxes = len(fns) medians = range(numBoxes) for i in range(numBoxes): box = bp['boxes'][i] boxX = [] boxY = [] for j in range(5): boxX.append(box.get_xdata()[j]) boxY.append(box.get_ydata()[j]) boxCoords = zip(boxX,boxY) # Alternate between Dark Khaki and Royal Blue k = i % 2 boxPolygon = Polygon(boxCoords, facecolor=boxColors[k]) ax1.add_patch(boxPolygon) # Now draw the median lines back over what we just filled in med = bp['medians'][i] medianX = [] medianY = [] for j in range(2): medianX.append(med.get_xdata()[j]) medianY.append(med.get_ydata()[j]) plt.plot(medianX, medianY, 'k') medians[i] = medianY[0] # Finally, overplot the sample averages, with horizontal alignment # in the center of each box plt.plot([np.average(med.get_xdata())], [np.average(data[i])], color='black', marker='^', markeredgecolor='k') ax1.set_xlim(0.5, numBoxes+0.5) #top = np.average(data[i])*3 #top = 1.5 top = y_up bottom = 0 ax1.set_ylim(bottom, top) #xLabels = ['40','60','100','150','300'] #for time xLabels = ['40','60','100','150','300'] #for size xtickNames = plt.setp(ax1, xticklabels=np.repeat(xLabels, 1)) plt.setp(xtickNames, fontsize=TICK_FONT) plt.yticks(fontsize = TICK_FONT) pos = np.arange(numBoxes)+1 avgs = [ np.average(data[i]) for i in xrange(0,len(data))] upperLabels = [] for i in xrange(0,len(avgs)): upperLabels.append('Med' + str(round(medians[i],1))+'\n'+'Avg' + str(round(avgs[i],1))) weights = ['normal', 'normal'] for tick,label in zip(range(numBoxes),ax1.get_xticklabels()): k = tick % 2 ax1.text(pos[tick], top+(top*0.1), upperLabels[tick], horizontalalignment='center', weight=weights[k], color='black',fontsize = UPPERLABEL_FONT) #path = os.path.join('C:\Users\rguo12\Documents\Asonam\\asonam\SNI_Paper\\',y_l.replace('#','no').replace('\n','').replace(' ','').replace('(','').replace(')','')+'.pdf') path = 'C:\weibodataset\\for_box_plot_Mar20\\'+kei+'_'+tag+'.pdf' #plt.savefig(path,format='pdf') plt.show()
def func(kei,y_u,y_l,y_label ): fns = os.listdir(PATH) print fns data = [] for fn in fns: p = os.path.join(PATH,fn) #print p stat_d = load_obj(p) target_l = stat_d[kei] #target_l = [float(x) for x in target_l] data.append(target_l) data = [data[1],data[3],data[4],data[0],data[2]] #data = [data[3],data[4],data[0],data[1],data[2]] #for ele in data: # print len(ele) fig, ax1 = plt.subplots(figsize=(8.7,7)) fig.canvas.set_window_title(tag+' '+kei) #plt.ticklabel_format(style='sci',axis='y',scilimits=(0,0)) plt.rc('font',size=18) plt.subplots_adjust(left=0.15, right=0.98, top=0.8, bottom=0.15) bp = plt.boxplot(data, notch=0, sym='o', vert=1, whis=1.5,positions=np.log10([10,25,50,100,200])) if y_u > 1000: formatter = FuncFormatter(thousands) ax1.yaxis.set_major_formatter(formatter) ax1.set_ylabel(y_label+r'($10^3$)',fontsize=LABEL_FONT,fontweight='bold') elif y_u > 100: formatter = FuncFormatter(hundreds) ax1.yaxis.set_major_formatter(formatter) ax1.set_ylabel(y_label+r'($10^2$)',fontsize=LABEL_FONT,fontweight='bold') else: ax1.set_ylabel(y_label,fontsize=LABEL_FONT,fontweight='bold') plt.setp(bp['boxes'], color='black') plt.setp(bp['whiskers'], color='black') plt.setp(bp['fliers'], color='white', marker='x') #ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', #alpha=0.5) # Hide these grid behind plot objects ax1.set_axisbelow(True) #ax1.set_title(title + '(' + tag + ')') #ax1.set_xlabel('Time since the first post (min)') ax1.set_xlabel('Number of Adopters',fontsize=LABEL_FONT,fontweight='bold') #ax1.set_xlabel('Time Since the Original Post',fontsize=LABEL_FONT,fontweight='bold') #ax1.set_yticks(fontsize=12) # Now fill the boxes with desired colors boxColors = ['white','white'] numBoxes = len(fns) medians = range(numBoxes) for i in range(numBoxes): box = bp['boxes'][i] boxX = [] boxY = [] for j in range(5): boxX.append(box.get_xdata()[j]) boxY.append(box.get_ydata()[j]) boxCoords = zip(boxX,boxY) # Alternate between Dark Khaki and Royal Blue k = i % 2 boxPolygon = Polygon(boxCoords, facecolor=boxColors[k]) ax1.add_patch(boxPolygon) # Now draw the median lines back over what we just filled in med = bp['medians'][i] medianX = [] medianY = [] for j in range(2): medianX.append(med.get_xdata()[j]) medianY.append(med.get_ydata()[j]) plt.plot(medianX, medianY, 'blue') medians[i] = medianY[0] # Finally, overplot the sample averages, with horizontal alignment # in the center of each box plt.plot([np.average(med.get_xdata())], [np.average(data[i])], color='r', marker='^', markeredgecolor='k') ax1.set_xlim(0.8, np.log10(200)+0.2) #top = np.average(data[i]) * 2 #top = 1.5 bottom = 0 #ax1.set_ylim(bottom, top) ax1.set_ylim(y_l,y_u) #xLabels = ['40','60','100','150','300'] #for time xLabels = ['10','30','50','100','200'] #for size xtickNames = plt.setp(ax1, xticklabels=np.repeat(xLabels, 1)) plt.setp(xtickNames, rotation=0, fontsize=TICK_FONT) for tick in ax1.yaxis.get_major_ticks(): tick.label.set_fontsize(TICK_FONT) #pos = np.arange(numBoxes)+1 pos = np.log10([10,25,50,100,200]) avgs = [ np.average(data[i]) for i in xrange(0,len(data))] upperLabels = [] for i in xrange(0,len(avgs)): if i==0: upperLabels.append('M: '+str(smart_round(medians[i]))+'\n'+'A: '+str(smart_round(avgs[i]))) else: upperLabels.append(str(smart_round(medians[i]))+'\n'+str(smart_round(avgs[i]))) #upperLabels.append(('M' + str(round(medians[i],1)),'A' + str(round(avgs[i],1)))) weights = ['bold', 'semibold'] for tick,label in zip(range(numBoxes),ax1.get_xticklabels()): k = tick % 2 ax1.text(pos[tick],y_u+(y_u*0.1) , upperLabels[tick],fontsize=TICK_FONT-2, horizontalalignment='center', weight=weights[k], color='black') #plt.set_size_inches(18.5,10.5) #plt.figure(figsize=(8.0, 6.0)) if tag == 'viral': vnv = 'v_' else: vnv = 'nv_' plt.savefig('E:\\SNAM_2015\\for_box_plot_size_comparison_infomap\\svg\\'+vnv+kei+'.pdf',format='pdf')
def func(kei, y_u, y_l, y_label): fns = os.listdir(PATH) print fns data = [] for fn in fns: p = os.path.join(PATH, fn) # print p stat_d = load_obj(p) target_l = stat_d[kei] # target_l = [float(x) for x in target_l] data.append(target_l) # data = [data[1],data[3],data[4],data[0],data[2]] data = [data[3], data[4], data[0], data[1], data[2]] # for ele in data: # print len(ele) fig, ax1 = plt.subplots(figsize=(8.7, 7)) fig.canvas.set_window_title(tag + " " + kei) # plt.ticklabel_format(style='sci',axis='y',scilimits=(0,0)) plt.rc("font", size=18) plt.subplots_adjust(left=0.15, right=0.98, top=0.8, bottom=0.15) bp = plt.boxplot(data, notch=0, sym="o", vert=1, whis=1.5, positions=np.log10([35, 60, 95, 150, 300])) if y_u > 1000: formatter = FuncFormatter(thousands) ax1.yaxis.set_major_formatter(formatter) ax1.set_ylabel(y_label + r"($10^3$)", fontsize=LABEL_FONT, fontweight="bold") elif y_u > 100: formatter = FuncFormatter(hundreds) ax1.yaxis.set_major_formatter(formatter) ax1.set_ylabel(y_label + r"($10^2$)", fontsize=LABEL_FONT, fontweight="bold") else: ax1.set_ylabel(y_label, fontsize=LABEL_FONT, fontweight="bold") plt.setp(bp["boxes"], color="black") plt.setp(bp["whiskers"], color="black") plt.setp(bp["fliers"], color="white", marker="x") # ax1.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', # alpha=0.5) # Hide these grid behind plot objects ax1.set_axisbelow(True) # ax1.set_title(title + '(' + tag + ')') # ax1.set_xlabel('Time since the first post (min)') # ax1.set_xlabel('Number of Adopters',fontsize=LABEL_FONT,fontweight='bold') ax1.set_xlabel("Time Since the Original Post", fontsize=LABEL_FONT, fontweight="bold") # ax1.set_yticks(fontsize=12) # Now fill the boxes with desired colors boxColors = ["white", "white"] numBoxes = len(fns) medians = range(numBoxes) for i in range(numBoxes): box = bp["boxes"][i] boxX = [] boxY = [] for j in range(5): boxX.append(box.get_xdata()[j]) boxY.append(box.get_ydata()[j]) boxCoords = zip(boxX, boxY) # Alternate between Dark Khaki and Royal Blue k = i % 2 boxPolygon = Polygon(boxCoords, facecolor=boxColors[k]) ax1.add_patch(boxPolygon) # Now draw the median lines back over what we just filled in med = bp["medians"][i] medianX = [] medianY = [] for j in range(2): medianX.append(med.get_xdata()[j]) medianY.append(med.get_ydata()[j]) plt.plot(medianX, medianY, "k") medians[i] = medianY[0] # Finally, overplot the sample averages, with horizontal alignment # in the center of each box plt.plot([np.average(med.get_xdata())], [np.average(data[i])], color="black", marker="^", markeredgecolor="k") ax1.set_xlim(np.log10(40) - 0.2, np.log10(300) + 0.2) # top = np.average(data[i]) * 2 # top = 1.5 bottom = 0 # ax1.set_ylim(bottom, top) ax1.set_ylim(y_l, y_u) # xLabels = ['40','60','100','150','300'] #for time xLabels = ["40", "60", "100", "150", "300"] # for size xtickNames = plt.setp(ax1, xticklabels=np.repeat(xLabels, 1)) plt.setp(xtickNames, rotation=0, fontsize=TICK_FONT) for tick in ax1.yaxis.get_major_ticks(): tick.label.set_fontsize(TICK_FONT) # pos = np.arange(numBoxes)+1 pos = np.log10([35, 60, 95, 150, 300]) avgs = [np.average(data[i]) for i in xrange(0, len(data))] upperLabels = [] for i in xrange(0, len(avgs)): if i == 0: upperLabels.append("M: " + str(smart_round(medians[i])) + "\n" + "A: " + str(smart_round(avgs[i]))) else: upperLabels.append(str(smart_round(medians[i])) + "\n" + str(smart_round(avgs[i]))) # upperLabels.append(('M' + str(round(medians[i],1)),'A' + str(round(avgs[i],1)))) weights = ["bold", "semibold"] for tick, label in zip(range(numBoxes), ax1.get_xticklabels()): k = tick % 2 ax1.text( pos[tick], y_u + (y_u * 0.1), upperLabels[tick], fontsize=TICK_FONT - 2, horizontalalignment="center", weight=weights[k], color="black", ) # plt.set_size_inches(18.5,10.5) # plt.figure(figsize=(8.0, 5.0)) if tag == "viral": vnv = "v_" else: vnv = "nv_" plt.savefig("C:\\Users\\rguo12\\Desktop\\SNAM_figures\\boxplt_time\\" + vnv + kei + ".pdf", format="pdf")