def create_cits_plots(data, title_=""): fig, ax1 = plt.subplots(1, 1, figsize=(7, 7)) fit = powerlaw.Fit(data, discrete=True) plt.suptitle(title_, fontsize=30) a = fit.power_law.alpha xmin = fit.power_law.xmin pdf = powerlaw.pdf(data) bins = pdf[0] widths = bins[1:] - bins[0:-1] centers = bins[0:-1] + 0.5 * widths xc, yc = find_pdf_at_x(xmin, pdf[0], pdf[1]) x_, y_ = fitted_pl_xy(a, xc, yc, xmin, np.max(pdf[0])) ax1.set_xlabel("Number of word occurances", fontsize=20) ax1.set_ylabel("Probability density function", fontsize=20) ax1.plot(centers, pdf[1], 'o') powerlaw.plot_pdf(data, ax=ax1, color='b', label='APS data') ax1.plot(x_, y_, 'r--', label=r'Power law fit: $x^{-%4.3f}$' % (a)) #ax1.plot([xmin,xmin],[10**(-16),1],'--',color="grey", label=r'$\rm x_{min}=%d$' % (xmin)) ax1.legend(loc=0, fontsize=15) fig.text(0.95, 0.05, '(c) 2018, P.G.', fontsize=10, color='gray', ha='right', va='bottom', alpha=0.5) plt.show()
def plot_basics(data, data_inst, fig, units): ''' This function is the main plotting function. Adapted from Newman's powerlaw package. ''' import pylab pylab.rcParams['xtick.major.pad'] = '8' pylab.rcParams['ytick.major.pad'] = '8' pylab.rcParams['font.sans-serif'] = 'Arial' from matplotlib import rc rc('font', family='sans-serif') rc('font', size=10.0) rc('text', usetex=False) from matplotlib.font_manager import FontProperties panel_label_font = FontProperties().copy() panel_label_font.set_weight("bold") panel_label_font.set_size(12.0) panel_label_font.set_family("sans-serif") n_data = 1 n_graphs = 4 from powerlaw import plot_pdf, Fit, pdf ax1 = fig.add_subplot(n_graphs, n_data, data_inst) x, y = pdf(data, linear_bins=True) ind = y > 0 y = y[ind] x = x[:-1] x = x[ind] ax1.scatter(x, y, color='r', s=.5, label='data') plot_pdf(data[data > 0], ax=ax1, color='b', linewidth=2, label='PDF') from pylab import setp setp(ax1.get_xticklabels(), visible=False) plt.legend(loc='bestloc') ax2 = fig.add_subplot(n_graphs, n_data, n_data + data_inst, sharex=ax1) plot_pdf(data[data > 0], ax=ax2, color='b', linewidth=2, label='PDF') fit = Fit(data, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle=':', color='g', label='w/o xmin') p = fit.power_law.pdf() ax2.set_xlim(ax1.get_xlim()) fit = Fit(data, discrete=True, xmin=3) fit.power_law.plot_pdf(ax=ax2, linestyle='--', color='g', label='w xmin') from pylab import setp setp(ax2.get_xticklabels(), visible=False) plt.legend(loc='bestloc') ax3 = fig.add_subplot(n_graphs, n_data, n_data * 2 + data_inst) #, sharex=ax1)#, sharey=ax2) fit.power_law.plot_pdf(ax=ax3, linestyle='--', color='g', label='powerlaw') fit.exponential.plot_pdf(ax=ax3, linestyle='--', color='r', label='exp') fit.plot_pdf(ax=ax3, color='b', linewidth=2) ax3.set_ylim(ax2.get_ylim()) ax3.set_xlim(ax1.get_xlim()) plt.legend(loc='bestloc') ax3.set_xlabel(units)
def plot_basics(data, data_inst, fig, units): ''' This function is the main plotting function. Adapted from Newman's powerlaw package. ''' import pylab pylab.rcParams['xtick.major.pad']='8' pylab.rcParams['ytick.major.pad']='8' pylab.rcParams['font.sans-serif']='Arial' from matplotlib import rc rc('font', family='sans-serif') rc('font', size=10.0) rc('text', usetex=False) from matplotlib.font_manager import FontProperties panel_label_font = FontProperties().copy() panel_label_font.set_weight("bold") panel_label_font.set_size(12.0) panel_label_font.set_family("sans-serif") n_data = 1 n_graphs = 4 from powerlaw import plot_pdf, Fit, pdf ax1 = fig.add_subplot(n_graphs,n_data,data_inst) x, y = pdf(data, linear_bins=True) ind = y>0 y = y[ind] x = x[:-1] x = x[ind] ax1.scatter(x, y, color='r', s=.5, label='data') plot_pdf(data[data>0], ax=ax1, color='b', linewidth=2, label='PDF') from pylab import setp setp( ax1.get_xticklabels(), visible=False) plt.legend(loc = 'bestloc') ax2 = fig.add_subplot(n_graphs,n_data,n_data+data_inst, sharex=ax1) plot_pdf(data[data>0], ax=ax2, color='b', linewidth=2, label='PDF') fit = Fit(data, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle=':', color='g',label='w/o xmin') p = fit.power_law.pdf() ax2.set_xlim(ax1.get_xlim()) fit = Fit(data, discrete=True,xmin=3) fit.power_law.plot_pdf(ax=ax2, linestyle='--', color='g', label='w xmin') from pylab import setp setp(ax2.get_xticklabels(), visible=False) plt.legend(loc = 'bestloc') ax3 = fig.add_subplot(n_graphs,n_data,n_data*2+data_inst)#, sharex=ax1)#, sharey=ax2) fit.power_law.plot_pdf(ax=ax3, linestyle='--', color='g',label='powerlaw') fit.exponential.plot_pdf(ax=ax3, linestyle='--', color='r',label='exp') fit.plot_pdf(ax=ax3, color='b', linewidth=2) ax3.set_ylim(ax2.get_ylim()) ax3.set_xlim(ax1.get_xlim()) plt.legend(loc = 'bestloc') ax3.set_xlabel(units)
def plot_basics(data, data_inst, fig, units): from powerlaw import plot_pdf, Fit, pdf annotate_coord = (-.4, .95) ax1 = fig.add_subplot(n_graphs,n_data,data_inst) x, y = pdf(data, linear_bins=True) ind = y>0 y = y[ind] x = x[:-1] x = x[ind] ax1.scatter(x, y, color='r', s=.5) plot_pdf(data[data>0], ax=ax1, color='b', linewidth=2) from pylab import setp setp( ax1.get_xticklabels(), visible=False) if data_inst==1: ax1.annotate("A", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) from mpl_toolkits.axes_grid.inset_locator import inset_axes ax1in = inset_axes(ax1, width = "30%", height = "30%", loc=3) ax1in.hist(data, normed=True, color='b') ax1in.set_xticks([]) ax1in.set_yticks([]) ax2 = fig.add_subplot(n_graphs,n_data,n_data+data_inst, sharex=ax1) plot_pdf(data, ax=ax2, color='b', linewidth=2) fit = Fit(data, xmin=1, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle=':', color='g') p = fit.power_law.pdf() ax2.set_xlim(ax1.get_xlim()) fit = Fit(data, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle='--', color='g') from pylab import setp setp( ax2.get_xticklabels(), visible=False) if data_inst==1: ax2.annotate("B", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) ax2.set_ylabel(u"p(X)")# (10^n)") ax3 = fig.add_subplot(n_graphs,n_data,n_data*2+data_inst)#, sharex=ax1)#, sharey=ax2) fit.power_law.plot_pdf(ax=ax3, linestyle='--', color='g') fit.exponential.plot_pdf(ax=ax3, linestyle='--', color='r') fit.plot_pdf(ax=ax3, color='b', linewidth=2) ax3.set_ylim(ax2.get_ylim()) ax3.set_xlim(ax1.get_xlim()) if data_inst==1: ax3.annotate("C", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) ax3.set_xlabel(units)
def plot_gamma(graphe): #fitness = powerlaw.Fit(graphe.degree().values(),xmin =1) #print fitness.alpha,fitness.xmin alpha = 1 + (graphe.number_of_nodes() / sum(np.log(graphe.degree().values()))) #powerlaw.plot_pdf(graphe.degree().values()) xdata, ydata = powerlaw.pdf(graphe.degree().values()) vala = {} xvala = [] yvala = [] somme = 0 exceptions = [] last_exception = -1 for value in graphe.degree().values(): somme += 1 try: vala[value] += 1 except: vala[value] = 1 for degree in range(1, max(vala.keys()) + 1): try: yvala.append(float(vala[degree]) / somme) xvala.append(degree) except: if len(exceptions) == 0: last_exception = degree #print degree exceptions.append(degree) #slope, intercept, r_value, p_value, std_err = stats.linregress( #np.log(xvala[:last_exception]),np.log(yvala[:last_exception])) #print slope x = xvala plt.plot(x, np.power(x, -alpha), color="orange") #plt.plot(x,np.power(x,-2.34296138661),color ="blue") #plfit.plfit(graphe.degree().values(),quiet = False,verbose = True,silent = False) plt.plot(xvala, yvala, color="black") plt.yscale('log') plt.xscale('log') #plt.ylim([0,0.4]) #plt.xlim([0,20]) plt.show()
def plot_gamma(graphe) : #fitness = powerlaw.Fit(graphe.degree().values(),xmin =1) #print fitness.alpha,fitness.xmin alpha = 1+(graphe.number_of_nodes()/sum(np.log(graphe.degree().values()))) #powerlaw.plot_pdf(graphe.degree().values()) xdata,ydata = powerlaw.pdf(graphe.degree().values()) vala ={} xvala=[] yvala=[] somme = 0 exceptions =[] last_exception = -1 for value in graphe.degree().values() : somme+=1 try : vala[value]+=1 except : vala[value] =1 for degree in range(1,max(vala.keys())+1) : try : yvala.append(float(vala[degree])/somme) xvala.append(degree) except : if len(exceptions) ==0 : last_exception =degree #print degree exceptions.append(degree) #slope, intercept, r_value, p_value, std_err = stats.linregress( #np.log(xvala[:last_exception]),np.log(yvala[:last_exception])) #print slope x = xvala plt.plot(x,np.power(x,-alpha),color = "orange") #plt.plot(x,np.power(x,-2.34296138661),color ="blue") #plfit.plfit(graphe.degree().values(),quiet = False,verbose = True,silent = False) plt.plot(xvala,yvala,color = "black") plt.yscale('log') plt.xscale('log') #plt.ylim([0,0.4]) #plt.xlim([0,20]) plt.show() #xdata,ydata=powerlaw.pdf(graphe.degree().values())
def power_law_plot(graph, log=True, linear_binning=False, bins=90, draw=True, x_min=None): degree = list(dict(graph.degree()).values()) #powerlaw does not work if a bin is empty #sum([1 if x == 0 else 0 for x in list(degree)]) corrected_degree = [x for x in degree if x != 0] if x_min is not None: corrected_degree = [x for x in corrected_degree if x > x_min] # fit powerlaw exponent and return distribution pwl_distri = pwl.pdf(corrected_degree, bins=bins) if draw: degree_distribution = Counter(degree) # Degree distribution x = [] y = [] for i in sorted(degree_distribution): x.append(i) y.append(degree_distribution[i] / len(graph)) #plot our distributon compared to powerlaw #plt.figure(figsize=(10,7)) plt.yscale('log') plt.xscale('log') plt.plot(x, y, 'ro') plt.xticks(fontsize=15) plt.yticks(fontsize=15) plt.xlabel('$k$', fontsize=16) plt.ylabel('$P(k)$', fontsize=16) if linear_binning: pwl.plot_pdf(corrected_degree, linear_bins=True, color='black', linewidth=2) else: pwl.plot_pdf(corrected_degree, color='black', linewidth=2) return pwl_distri
def analyze_pk(g, figfile): ks = [x[1] for x in g.degree()] results = powerlaw.Fit(ks, discrete=True) figPDF = results.power_law.plot_pdf(label=r"fit($\alpha$: %.2f, $x_{min}$: %d)" % (results.alpha, results.xmin)) figPDF.set_ylabel("P(k)") figPDF.set_xlabel("k") cdf = {z[0]:z[1] for z in zip(*powerlaw.cdf(ks))} cdf_xmin = cdf[results.xmin] bin_edges, prob = powerlaw.pdf(ks) x = [ (bin_edges[i]+bin_edges[i+1])/2.0 for i in range(0, len(bin_edges)-1) ] plt.plot(x, prob/(1.0-cdf_xmin), label="data") plt.legend() plt.savefig(figfile) return (results.power_law.alpha, results.power_law.xmin)
def plot_basics(filepath, ax, c): tweetcounts = np.genfromtxt(filepath, usecols=(1,), comments=None, dtype=np.int32, unpack=True) ### x, y = powerlaw.pdf(tweetcounts, linear_bins=True) ind = y > 0 y = y[ind] x = x[:-1] x = x[ind] ax.scatter(x, y, color=c, s=.5) # ax.set_xscale('log') ax.set_yscale('log') # powerlaw.plot_pdf(tweetcounts, ax=ax, color='b', linewidth=2) # ### # # from mpl_toolkits.axes_grid.inset_locator import inset_axes # ax1in = inset_axes(ax, width="30%", height="30%", loc=3) # ax1in.hist(tweetcounts, density=True, color='b') # ax1in.set_xticks([]) # ax1in.set_yticks([]) # # ### # fit = powerlaw.Fit(tweetcounts, xmin=1, discrete=True) # fit.power_law.plot_pdf(ax=ax, linestyle=':', color='g') # # #### # fit = powerlaw.Fit(tweetcounts, discrete=True) # fit.power_law.plot_pdf(ax=ax, linestyle='--', color='g') # # ax.set_xlabel(r"Tweetcount") # ax.set_ylabel(u"p(X)") # # alpha = fit.power_law.alpha # xmin = fit.power_law.xmin # textstr = 'alpha: {0:.4f}\nxmin: {1}\nperiod: {2}'.format(alpha, xmin, text) # props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) # ax.text(0.5, 0.95, textstr, transform=ax.transAxes, fontsize=14, verticalalignment='top', bbox=props) # #### return
def create_cits_plots(data, title_=""): # fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14,7) ) fig, ax1 = plt.subplots(1, 1, figsize=(7,7) ) fit = powerlaw.Fit(data, discrete=True) plt.suptitle(title_,fontsize=30) a = fit.power_law.alpha xmin = fit.power_law.xmin pdf = powerlaw.pdf(data) bins = pdf[0] widths = bins[1:] - bins[0:-1] centers = bins[0:-1] + 0.5*widths xc,yc = find_pdf_at_x(xmin, pdf[0], pdf[1]) x_, y_ = fitted_pl_xy(a, xc, yc, xmin, np.max(pdf[0]) ) #### AX1 ax1.set_xlabel("Number of citations", fontsize=20) ax1.set_ylabel("Probability density function", fontsize=20) ax1.plot(centers,pdf[1],'o') powerlaw.plot_pdf(data, ax=ax1, color='b',label='APS data') ax1.plot(x_, y_, 'r--', label=r'Power law fit: $x^{-%4.3f}$' % ( a ) ) # ax1.plot(xc,yc,'o',color='grey') ax1.plot([xmin,xmin],[10**(-16),1],'--',color="grey", label=r'$\rm x_{min}=%d$' % (xmin)) ax1.legend(loc=0, fontsize=15) #### AX2 # ax2.set_xlabel(r'$\rm x_{min}$', fontsize=20) # ax2.set_ylabel(r'$\rm D, \sigma, \alpha$', fontsize=20) # ax2.plot(fit.xmins, fit.Ds, label=r'$D$') # ax2.plot(fit.xmins, fit.sigmas, label=r'$\sigma$', linestyle='--') # ax2.plot(fit.xmins, fit.sigmas/fit.alphas, label=r'$\sigma /\alpha$', linestyle='--') # ax2.legend(loc=0, fontsize=15) # ax2.set_xlim( [0, 200] ) # ax2.set_ylim( [0, .25] ) fig.text(0.95, 0.05, '(c) 2018, P.G.',fontsize=10, color='gray', ha='right', va='bottom', alpha=0.5) plt.show()
def plot_basics(data, data_inst, fig, units): from powerlaw import plot_pdf, Fit, pdf annotate_coord = (-.1, .95) # annotate_coord = (1.1, .95) ax1 = fig.add_subplot(n_graphs, n_data, data_inst, visible=False) x, y = pdf(data, linear_bins=True) ind = y > 0 y = y[ind] x = x[:-1] x = x[ind] ax1.scatter(x, y, color='r', s=.5) plot_pdf(data[data > 0], ax=ax1, color='b', linewidth=2) from pylab import setp setp(ax1.get_xticklabels(), visible=False) # ABC # if data_inst == 1: # ax1.annotate("A", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) # ax1.set_ylabel(u"p(X)") # from mpl_toolkits.axes_grid.inset_locator import inset_axes # ax1in = inset_axes(ax1, width="30%", height="30%", loc=3) # ax1in.hist(data, density=True, color='b') # ax1in.set_xticks([]) # ax1in.set_yticks([]) # ax1.set_xlabel(units) ax2 = fig.add_subplot(n_graphs, n_data, n_data + data_inst, sharex=ax1, visible=False) plot_pdf(data, ax=ax2, color='b', linewidth=2, label="pdf of data") fit = Fit(data, xmin=1, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle=':', color='g', label="power law fit") p = fit.power_law.pdf() ax2.set_xlim(ax1.get_xlim()) fit = Fit(data, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle='--', color='g', label="power law fit--opt xmin") from pylab import setp setp(ax2.get_xticklabels(), visible=True) # if data_inst == 1: ax2.annotate("B", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) ax2.set_ylabel(u"p(X)") # (10^n)") handles, labels = ax2.get_legend_handles_labels() ax2.legend(handles, labels, loc=3) ax2.set_xlabel(units) ax3 = fig.add_subplot(n_graphs, n_data, n_data * 2 + data_inst) # , sharex=ax1)#, sharey=ax2) fit.power_law.plot_pdf(ax=ax3, linestyle='--', color='g', label="power law fit\n(opt-min)") fit.exponential.plot_pdf(ax=ax3, linestyle='--', color='r', label="exponential fit\n(opt-min)") fit.plot_pdf(ax=ax3, color='b', linewidth=2, label="PDF\n(opt-min)") ax3.set_ylim(ax2.get_ylim()) ax3.set_xlim(ax1.get_xlim()) handles, labels = ax3.get_legend_handles_labels() ax3.legend(handles, labels, loc=3, fontsize=12) ax3.set_xlabel(units, fontsize=15) # if data_inst == 1: ax3.annotate("C", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) ax3.set_ylabel(u"p(X)", fontsize=15)
def main(): G = nx.Graph() fileIn = open('brazil.txt','r') for line in fileIn: m,f,t = line.split(' ') G.add_node(m,gender='m') G.add_node(f,gender='f') G.add_edge(m,f) degree_sequence_female =sorted([d for n,d in G.degree().iteritems() if G.node[n]['gender']== 'f' ], reverse=True) # degree sequence degree_sequence_male =sorted([d for n,d in G.degree().iteritems() if G.node[n]['gender']== 'm' ], reverse=True) # degree sequence male_fraction = float(len(degree_sequence_male))/(len(degree_sequence_male) + len(degree_sequence_female)) print male_fraction e_male,e_female = analytic_exponent(G,male_fraction) pdf_female = powerlaw.pdf(degree_sequence_female) x_fe = list(pdf_female[0]) ; y_fe = list(pdf_female[1]) del x_fe[-1] pdf_male = powerlaw.pdf(degree_sequence_male) x_ma = list(pdf_male[0]) ; y_ma = list(pdf_male[1]) del x_ma[-1] data_fe = degree_sequence_female plt.scatter(x_fe,y_fe,color=maj_color ) fit_fe = powerlaw.Fit(data_fe, discrete=True ,xmax = 200 , xmin = 20 ) alpha_fe = fit_fe.power_law.alpha figFIT = fit_fe.power_law.plot_pdf(color=maj_color, linestyle='--' , label=r"(workers) Fit: %.2f, Model: %.2f"%(alpha_fe,e_female) ) data_ma = degree_sequence_male plt.scatter(x_ma,y_ma,color=min_color ) fit_ma = powerlaw.Fit(data_ma, discrete=False ,xmax = 200 , xmin = 20) alpha_ma = fit_ma.power_law.alpha fit_ma.power_law.plot_pdf(color=min_color, linestyle='--' , label=r"(buyers) Fit: %.2f, Model: %.2f"%(alpha_ma,e_male) ) plt.xlim([1,200]) #plt.tight_layout() handles, labels = figFIT.get_legend_handles_labels() leg = figFIT.legend(handles, labels, loc=3 ) leg.draw_frame(False) figFIT.text(9,0.18,'A) Sexual contacts',fontsize = 24) figFIT.text(2,0.001,r'$f_{min} = 0.40$',fontsize = 26) figFIT.set_ylabel("$p(k)$" , size = 26) figFIT.set_xlabel("degree $(k)$ " , size = 26) figname = 'deg_dist_brazil_scatter' plt.savefig(figname+'.eps', bbox_inches='tight') plt.savefig(figname+'.svg', bbox_inches='tight')
def main(): G = nx.Graph() fileIn = 'sampled_APS_pacs052030.gexf' G = nx.read_gexf(fileIn) degree_sequence_maj = sorted([ d for n, d in G.degree().iteritems() if G.node[n]['pacs'] == '05.30.-d' ], reverse=True) # degree sequence degree_sequence_min = sorted([ d for n, d in G.degree().iteritems() if G.node[n]['pacs'] == '05.20.-y' ], reverse=True) # degree sequence maj_fraction = float(len(degree_sequence_maj)) / ( len(degree_sequence_maj) + len(degree_sequence_min)) maj_maj = maj_min = min_min = 0 for e in G.edges(): n1, n2 = e g1 = G.node[n1]['pacs'] g2 = G.node[n2]['pacs'] if g1 == g2: if g1 == '05.20.-y': min_min += 1 else: maj_maj += 1 else: maj_min += 1 h_aa = 0.88 h_bb = 1 e_min = 3.4 e_maj = 2.8 data_min = degree_sequence_min data_pdf = powerlaw.pdf(data_min) x = list(data_pdf[0]) y = data_pdf[1] del x[-1] #scatter(x,y,color='r' ) #plot(range(10),range(10)) data_maj = degree_sequence_maj maj_pdf = powerlaw.pdf(data_maj) x_maj = list(maj_pdf[0]) del x_maj[-1] y_maj = maj_pdf[1] data_min = degree_sequence_min data_pdf = powerlaw.pdf(data_min) plt.scatter(x, y, color=min_color) #figPDF = powerlaw.plot_pdf(data_min,color='r', linewidth=2 ) fit_min = powerlaw.Fit(data_min, xmax=30, xmin=4) alpha_min = fit_min.power_law.alpha figFIT = fit_min.power_law.plot_pdf(color=min_color, linestyle='--', label=r"(CSM) Fit: %.2f, Model: %.2f" % (alpha_min, e_min)) data_maj = degree_sequence_maj plt.scatter(x_maj, y_maj, color=maj_color) #powerlaw.plot_pdf(data_maj,color='b', linewidth=2 ) fit_maj = powerlaw.Fit(data_maj, xmax=80, xmin=5) alpha_maj = fit_maj.power_law.alpha fit_maj.power_law.plot_pdf(color=maj_color, linestyle='--', label=r"(QSM) Fit: %.2f , Model: %.2f" % (alpha_maj, e_maj)) handles, labels = figFIT.get_legend_handles_labels() leg = figFIT.legend(handles, labels, loc=3) leg.draw_frame(False) plt.xlim([1, 40]) figFIT.text(7, 0.18, 'C) Scientific citation', fontsize=24) figFIT.text(2, 0.01, r'$f_{min} = 0.38$', fontsize=26) figFIT.set_ylabel(r"$p(k)$", size=26) figFIT.set_xlabel(r"degree $(k)$", size=26) figname = 'deg_dist_APS_05' plt.savefig(figname + '.eps', bbox_inches='tight') plt.savefig(figname + '.svg', bbox_inches='tight')
def plot_basics(data, data_inst, fig, units): ### Setup ### from powerlaw import plot_pdf, Fit, pdf import pylab pylab.rcParams['xtick.major.pad'] = '8' pylab.rcParams['ytick.major.pad'] = '8' #pylab.rcParams['font.sans-serif']='Arial' from matplotlib.font_manager import FontProperties panel_label_font = FontProperties().copy() panel_label_font.set_weight("bold") panel_label_font.set_size(30.0) panel_label_font.set_family("sans-serif") n_data = 2 n_graphs = 4 annotate_coord = (-.4, .95) ############# ax1 = fig.add_subplot(n_graphs, n_data, data_inst) x, y = pdf(data, linear_bins=True) ind = y > 0 y = y[ind] x = x[:-1] x = x[ind] ax1.scatter(x, y, color='r', s=.5) plot_pdf(data[data > 0], ax=ax1, color='b', linewidth=2) from pylab import setp setp(ax1.get_xticklabels(), visible=False) if data_inst == 1: ax1.annotate("A", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) ax2 = fig.add_subplot(n_graphs, n_data, n_data + data_inst, sharex=ax1) plot_pdf(data, ax=ax2, color='b', linewidth=2) fit = Fit(data, xmin=1, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle='--', color='g') _ = fit.power_law.pdf() ax2.set_xlim((1, max(x))) setp(ax2.get_xticklabels(), visible=False) if data_inst == 1: ax2.annotate("B", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) ax2.set_ylabel(u"p(X)") # (10^n)") ax3 = fig.add_subplot(n_graphs, n_data, n_data * 2 + data_inst) #, sharex=ax1)#, sharey=ax2) fit.power_law.plot_pdf(ax=ax3, linestyle='--', color='g') fit.exponential.plot_pdf(ax=ax3, linestyle='--', color='r') fit.lognormal.plot_pdf(ax=ax3, linestyle=':', color='r') fit.plot_pdf(ax=ax3, color='b', linewidth=2) ax3.set_ylim(ax2.get_ylim()) ax3.set_xlim(ax1.get_xlim()) if data_inst == 1: ax3.annotate("C", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) ax3.set_xlabel(units)
def plplot(data, title, save=False, save_path=None): data = np.array(data) fig = plt.figure(figsize=(18,6)) fig.suptitle(title) # === A === ax1 = fig.add_subplot(1,3,1) # 线性x轴 x, y = pdf(data, linear_bins=True) ind = y>0 y = y[ind] x = x[:-1] x = x[ind] ax1.scatter(x, y, color='r', s=.5) # 双log-绘制概率密度曲线 plot_pdf(data[data>0], ax=ax1, color='b', linewidth=2) ax1.set_xlabel('A') # 绘制histogram小图 from mpl_toolkits.axes_grid.inset_locator import inset_axes ax1in = inset_axes(ax1, width = "30%", height = "30%", loc=3) ax1in.hist(data, normed=True, color='b') ax1in.set_xticks([]) ax1in.set_yticks([]) # === A === # === B === annotation = '' ax2 = fig.add_subplot(1,3,2, sharey=ax1) # 双log-绘制概率密度曲线 print(title) print(pdf(data)) print() plot_pdf(data, ax=ax2, color='b', linewidth=2) # 拟合power-law函数并绘图 fit = Fit(data, xmin=1, discrete=True, parameter_range={'alpha':[None,None]}) fit.power_law.plot_pdf(ax=ax2, linestyle=':', color='g') params1 = (fit.power_law.alpha, fit.power_law.xmin, fit.power_law.sigma) # alpha为拟合系数 # xmin表示最小的x值(使不为0),此处指定为1 # sigma为标准差 annotation += '\':\' - alpha={:.2f}, xmin= {}, sigma={:.2f}'.format(*params1) # p = fit.power_law.pdf() fit = Fit(data, discrete=True, parameter_range={'alpha':[-5,10]}) # 区别于ax2中的第一条拟合线 - 此处的xmin并非指定,而是自动计算的optimal fit.power_law.plot_pdf(ax=ax2, linestyle='--', color='g') params2 = (fit.power_law.alpha, fit.power_law.xmin, fit.power_law.sigma) annotation += '\n\'--\' - alpha={:.2f}, xmin= {}, sigma={:.2f}'.format(*params2) ax2.set_xlabel('B') ax2.set_ylabel(u"p(X)")# (10^n)") ax2.set_xlim(ax1.get_xlim()) annotate_coord = (0.05, 0.88) ax2.annotate(annotation, annotate_coord, xycoords="axes fraction") # === B === # === C === ax3 = fig.add_subplot(1,3,3, sharey=ax1)#, sharex=ax1)#, sharey=ax2) plot_pdf(data[data>0], ax=ax3, color='b', linewidth=2) fit.power_law.plot_pdf(ax=ax3, linestyle='--', color='g') fit.exponential.plot_pdf(ax=ax3, linestyle='--', color='r') ax3.set_ylim(ax2.get_ylim()) ax3.set_xlim(ax1.get_xlim()) ax3.set_xlabel('C') # === C === if save: plt.savefig(save_path) else: plt.show() return params1, params2
def draw(sample, xmin, xmax, model, ax=None, lim1=4, color='green', normalized=False): """ Fits the data contained in "sample" (i.e. sizes or durations of the avalanches) with the model chosen. xmin : the maximum xmin that can be considered in the power law fit. lim1 : upper limit of the range in which to search the power law parameter ax = object of the type AxesSubplot (returned by fig.add_subplot()) """ ypred = pwl.Fit(sample, xmin=(1, xmin + 1), xmax=xmax, parameter_range={"alpha": [1, lim1]}, discrete=True) if normalized == True: new = [] for i in range(len(sample)): if sample[i] >= ypred.xmin: new.append(sample[i]) sample = new if ax == None: fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ns = len(pwl.pdf(sample)[0]) - 1 # == nbins + 1 print(ns) ax.set_xscale('log') nbins = np.logspace(np.log10(min(sample)), np.log10(max(sample)), ns) ax.hist(sample, density=True, histtype='stepfilled', log=True, bins=nbins, color=color, alpha=0.25) pwl.plot_pdf(sample, color='r', linewidth=2, label='pdf', linear_bins=False) if model == 'power_law': ypred.power_law.plot_pdf(color='blue', linestyle='-', linewidth=2, label='Power Law fit') print( ypred.distribution_compare('power_law', 'exponential', normalized_ratio=True)) print('Parameters are (alpha)', ypred.power_law.alpha, 'xmin', ypred.power_law.xmin) elif model == 'lognormal': ypred.lognormal.plot_pdf(color='blue', linestyle='-', linewidth=2, label='Lognormal fit') print('Parameters are (mu,sigma)', ypred.lognormal.mu, 'and', ypred.lognormal.sigma) elif model == 'exponential': ypred.exponential.plot_pdf(color='blue', linestyle='-', linewidth=2, label='Exponential fit') print('Parameters are (lambda)', ypred.exponential.Lambda) elif model == 'stretched_exponential': ypred.stretched_exponential.plot_pdf(color='blue', linestyle='-', linewidth=2, label='Stretched_exponential fit') print('Parameters are (lambda,beta)', ypred.stretched_exponential.Lambda, 'and', ypred.stretched_exponential.beta) elif model == 'truncated_power_law': ypred.truncated_power_law.plot_pdf(color='blue', linestyle='-', linewidth=2, label='Truncated_power_law fit') print('Parameters are (alpha,lambda)', ypred.truncated_power_law.alpha, 'and', ypred.truncated_power_law.Lambda) plt.legend()
def plot_basics(data, data_inst, fig, units): from powerlaw import plot_pdf, Fit, pdf annotate_coord = (-.4, .95) ax1 = fig.add_subplot(n_graphs,n_data,data_inst) plot_pdf(data[data>0], ax=ax1, linear_bins=True, color='r', linewidth=.5) x, y = pdf(data, linear_bins=True) ind = y>0 y = y[ind] x = x[:-1] x = x[ind] ax1.scatter(x, y, color='r', s=.5) plot_pdf(data[data>0], ax=ax1, color='b', linewidth=2) from pylab import setp setp( ax1.get_xticklabels(), visible=False) #ax1.set_xticks(ax1.get_xticks()[::2]) ax1.set_yticks(ax1.get_yticks()[::2]) locs,labels = yticks() #yticks(locs, map(lambda x: "%.0f" % x, log10(locs))) if data_inst==1: ax1.annotate("A", annotate_coord, xycoords="axes fraction", fontsize=14) from mpl_toolkits.axes_grid.inset_locator import inset_axes ax1in = inset_axes(ax1, width = "30%", height = "30%", loc=3) ax1in.hist(data, normed=True, color='b') ax1in.set_xticks([]) ax1in.set_yticks([]) ax2 = fig.add_subplot(n_graphs,n_data,n_data+data_inst, sharex=ax1) plot_pdf(data, ax=ax2, color='b', linewidth=2) fit = Fit(data, xmin=1, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle=':', color='g') p = fit.power_law.pdf() #ax2.set_ylim(min(p), max(p)) ax2.set_xlim(ax1.get_xlim()) fit = Fit(data, discrete=True) fit.power_law.plot_pdf(ax=ax2, linestyle='--', color='g') from pylab import setp setp( ax2.get_xticklabels(), visible=False) #ax2.set_xticks(ax2.get_xticks()[::2]) if ax2.get_ylim()[1] >1: ax2.set_ylim(ax2.get_ylim()[0], 1) ax2.set_yticks(ax2.get_yticks()[::2]) #locs,labels = yticks() #yticks(locs, map(lambda x: "%.0f" % x, log10(locs))) if data_inst==1: ax2.annotate("B", annotate_coord, xycoords="axes fraction", fontsize=14) ax2.set_ylabel(r"$p(X)$")# (10^n)") ax3 = fig.add_subplot(n_graphs,n_data,n_data*2+data_inst)#, sharex=ax1)#, sharey=ax2) fit.power_law.plot_pdf(ax=ax3, linestyle='--', color='g') fit.exponential.plot_pdf(ax=ax3, linestyle='--', color='r') fit.plot_pdf(ax=ax3, color='b', linewidth=2) #p = fit.power_law.pdf() ax3.set_ylim(ax2.get_ylim()) ax3.set_yticks(ax3.get_yticks()[::2]) ax3.set_xlim(ax1.get_xlim()) #locs,labels = yticks() #yticks(locs, map(lambda x: "%.0f" % x, log10(locs))) if data_inst==1: ax3.annotate("C", annotate_coord, xycoords="axes fraction", fontsize=14) #if ax2.get_xlim()!=ax3.get_xlim(): # zoom_effect01(ax2, ax3, ax3.get_xlim()[0], ax3.get_xlim()[1]) ax3.set_xlabel(units)
def build(path="../data/influence_data.csv"): G = create_graph() # print(nx.degree_histogram(G)) deg_dist = nx.degree_histogram(G) # _, deg_dist = get_inout_degrees(G) degree = list(dict(nx.degree(G)).values()) # degree = deg_dist degree = [d for d in degree if d > 0] plt.plot(range(0, len(degree)), sorted(degree, reverse=True), 'ko', color='#40916c', alpha=0.7) plt.legend(['degree']) plt.xlabel('id') plt.ylabel('degree') plt.tight_layout() # 去除pdf周围白边 plt.savefig("../img/degree_distrubition.pdf") # plt.show() plt.figure() import powerlaw fit = powerlaw.Fit(degree, xmin=3, discrete=True) print(f'alpha: {fit.power_law.alpha}') print(f'x-min: {fit.power_law.xmin}') print(f'D: {fit.power_law.D}') fit.plot_pdf(color='#2d6a4f', lw=3, linestyle=':', label='curve') x, y = powerlaw.pdf(degree, linear_bins=True) ind = y > 0 y = y[ind] x = x[:-1] x = x[ind] plt.scatter(x, y, color='#74c69d', marker='s', alpha=0.6, s=50, label='original degree') fit.power_law.plot_pdf(color='#1b4332', linestyle='-', label='$\gamma=1.8$', lw=2) # fit.power_law.plot_pdf(color='#1b4332', linestyle='-', # label='$gamma=%.3f$' % fit.power_law.alpha, lw=2) plt.legend() plt.xlabel('ln(id)') plt.ylabel('ln(degree)') # plt.title( # 'Power Law Curve in Double Logarithmic Coordinates') plt.savefig("../img/powerlaw.pdf", dpi=600) plt.show() # 计算皮尔逊 指数 # from scipy.stats import pearsonr # r, p = pearsonr(x, y) # print(f'pearsonr r: {r}, p: {p}') # 线性回归 # slope, intercept, r_value, p_value, std_err = stats.linregress(x, y) # print(slope, intercept, r_value, p_value, std_err) # 分布 # DataFitAndVisualization(X, Y) # plt.loglog(x, y, color="steelblue", linewidth=2) # 在双对数坐标轴上绘制度分布曲线 r1, p1 = fit.distribution_compare('power_law', 'exponential', nested=True) r2, p2 = fit.distribution_compare('power_law', 'lognormal', nested=True) r3, p3 = fit.distribution_compare('power_law', 'truncated_power_law', nested=True) r4, p4 = fit.distribution_compare('power_law', 'stretched_exponential', nested=True) print(r1, p1) print(r2, p2) print(r3, p3) print(r4, p4)
def powerFit(Gx, mode='power_law', xmin=None, xmax=None, linear=False, indeg=True, undir=False, units=None): # set-up pylab.rcParams['xtick.major.pad'] = '24' pylab.rcParams['ytick.major.pad'] = '24' #pylab.rcParams['font.sans-serif']='Arial' from matplotlib import rc rc('font', family='sans-serif') rc('font', size=14.0) rc('text', usetex=False) panel_label_font = FontProperties().copy() panel_label_font.set_weight("bold") panel_label_font.set_size(12.0) panel_label_font.set_family("sans-serif") # fit power law distribution using the powerlaw package try: if indeg == True: data = np.array( sorted([d for n, d in Gx.in_degree()], reverse=True)) elif indeg == False and undir == False: data = np.array( sorted([d for n, d in Gx.out_degree()], reverse=True)) elif indeg == False and undir == True: data = np.array(sorted([d for n, d in Gx.degree()], reverse=True)) except: data = Gx #### annotate_coord = (-.4, .95) fig = plt.figure(figsize=(16, 8)) linf = fig.add_subplot(1, 2, 1) x, y = powerlaw.pdf(data[data > 0], linear_bins=True) ind = y > 0 y = y[ind] x = x[:-1] x = x[ind] linf.scatter(x, y, color='r', s=5.5) powerlaw.plot_pdf(data[data > 0], color='b', linewidth=2, linear_bins=linear, ax=linf) linf.annotate(" ", annotate_coord, xycoords="axes fraction", fontproperties=panel_label_font) linf.set_ylabel(u"p(X)") # (10^n)") linf.set_xlabel(units) if xmin != None and xmax == None: fit = powerlaw.Fit(data, discrete=True, xmin=xmin) elif xmax != None and xmin == None: fit = powerlaw.Fit(data, discrete=True, xmax=xmax) elif xmax != None and xmin != None: fit = powerlaw.Fit(data, discrete=True, xmin=xmin, xmax=xmax) else: fit = powerlaw.Fit(data, discrete=True) fitf = fig.add_subplot(1, 2, 2, sharey=linf) fitf.set_xlabel(units) powerlaw.plot_pdf(data, color='b', linewidth=2, ax=fitf) if mode == 'truncated_power_law': fit.truncated_power_law.plot_pdf(color='r', linestyle='--', ax=fitf) print('alpha = ', fit.truncated_power_law.alpha) print('Lambda = ', fit.truncated_power_law.Lambda) print('xmin,xmax = ', fit.xmin, fit.xmax) print('Kolmogorov-Smirnov distance = ', fit.truncated_power_law.D) elif mode == 'power_law': fit.power_law.plot_pdf(color='r', linestyle='--', ax=fitf) print('alpha = ', fit.power_law.alpha) print('sigma = ', fit.power_law.sigma) print('xmin,xmax = ', fit.xmin, fit.xmax) print('Kolmogorov-Smirnov distance = ', fit.power_law.D) elif mode == 'lognormal': fit.lognormal.plot_pdf(color='r', linestyle='--', ax=fitf) print('mu = ', fit.lognormal.mu) print('sigma = ', fit.lognormal.sigma) print('xmin,xmax = ', fit.xmin, fit.xmax) print('Kolmogorov-Smirnov distance = ', fit.lognormal.D) elif mode == 'lognormal_positive': fit.lognormal_positive.plot_pdf(color='r', linestyle='--', ax=fitf) print('mu = ', fit.lognormal_positive.mu) print('sigma = ', fit.lognormal_positive.sigma) print('xmin,xmax = ', fit.xmin, fit.xmax) print('Kolmogorov-Smirnov distance = ', fit.lognormal_positive.D) elif mode == 'exponential': fit.exponential.plot_pdf(color='r', linestyle='--', ax=fitf) print('Lambda = ', fit.exponential.Lambda) print('xmin,xmax = ', fit.xmin, fit.xmax) print('Kolmogorov-Smirnov distance = ', fit.exponential.D) elif mode == 'stretched_exponential': fit.stretched_exponential.plot_pdf(color='r', linestyle='--', ax=fitf) print('Lambda = ', fit.stretched_exponential.Lambda) print('beta = ', fit.stretched_exponential.beta) print('xmin,xmax = ', fit.xmin, fit.xmax) print('Kolmogorov-Smirnov distance = ', fit.stretched_exponential.D) else: fit = None print('mode not allowed') return (data, fit)
def main(): G = nx.read_gexf('DBLP_graph.gexf') degree_sequence_min = sorted( [d for n, d in G.degree().iteritems() if G.node[n]['gender'] == 'f'], reverse=True) # degree sequence degree_sequence_maj = sorted( [d for n, d in G.degree().iteritems() if G.node[n]['gender'] == 'm'], reverse=True) # degree sequence maj_fraction = float(len(degree_sequence_maj)) / ( len(degree_sequence_min) + len(degree_sequence_maj)) #e_maj,e_min = analytic_exponent(G,maj_fraction) e_maj = 2.94 #measured analytically e_min = 3.24 pdf_female = powerlaw.pdf(degree_sequence_min) x_fe = list(pdf_female[0]) y_fe = list(pdf_female[1]) del x_fe[-1] pdf_male = powerlaw.pdf(degree_sequence_maj) x_ma = list(pdf_male[0]) y_ma = list(pdf_male[1]) del x_ma[-1] data_min = degree_sequence_min plt.scatter(x_fe, y_fe, color=min_color) fit_min = powerlaw.Fit(data_min, xmax=60, xmin=6) alpha_min = fit_min.power_law.alpha figFIT = fit_min.power_law.plot_pdf( color=min_color, linestyle='--', label=r"(women) Fit: %.2f, Model: %.2f" % (alpha_min, e_min)) data_maj = degree_sequence_maj plt.scatter(x_ma, y_ma, color=maj_color) fit_maj = powerlaw.Fit(data_maj, xmax=80, xmin=6) alpha_maj = fit_maj.power_law.alpha fit_maj.power_law.plot_pdf(color=maj_color, linestyle='--', label=r"(men) Fit: %.2f , Model: %.2f" % (alpha_maj, e_maj)) plt.ylim([10**(-5), 1]) plt.xlim([1, 120]) handles, labels = figFIT.get_legend_handles_labels() leg = figFIT.legend(handles, labels, loc=3) leg.draw_frame(False) figFIT.text(7, 0.2, 'B) Scientific collaboration', fontsize=24) figFIT.text(2, 0.01, r'$f_{min} = 0.23$', fontsize=26) figFIT.set_ylabel(r"$p(k)$", size=26) figFIT.set_xlabel(r"degree $(k)$", size=26) figname = 'deg_dist_DBLP_2010_scatter' plt.savefig(figname + '.eps', bbox_inches='tight') plt.savefig(figname + '.svg', bbox_inches='tight')