我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用matplotlib.pylab.hist()。
def plot_volcano(logFC,p_val,sample_name,saveName,logFC_thresh): fig=pl.figure() ## To plot and save pl.scatter(logFC[(p_val>0.05)|(abs(logFC)<logFC_thresh)],-np.log10(p_val[(p_val>0.05)|(abs(logFC)<logFC_thresh)]),color='blue',alpha=0.5); pl.scatter(logFC[(p_val<0.05)&(abs(logFC)>logFC_thresh)],-np.log10(p_val[(p_val<0.05)&(abs(logFC)>logFC_thresh)]),color='red'); pl.hlines(-np.log10(0.05),min(logFC),max(logFC)) pl.vlines(-logFC_thresh,min(-np.log10(p_val)),max(-np.log10(p_val))) pl.vlines(logFC_thresh,min(-np.log10(p_val)),max(-np.log10(p_val))) pl.xlim(-3,3) pl.xlabel('Log Fold Change') pl.ylabel('-log10(p-value)') pl.savefig(saveName) pl.close(fig) # def plot_histograms(df_peaks,pntr_list): # # for pntr in pntr_list: # colName =pntr[2]+'_Intragenic_position' # pl.hist(df_peaks[colName]) # pl.xlabel(colName) # pl.ylabel() # pl.show()
def plot_1d(dataset, nbins, data): with sns.axes_style('white'): plt.rc('font', weight='bold') plt.rc('grid', lw=2) plt.rc('lines', lw=3) plt.figure(1) plt.hist(data, bins=np.arange(nbins+1), color='blue') plt.ylabel('Count', weight='bold', fontsize=24) xticks = list(plt.gca().get_xticks()) while (nbins-1) / float(xticks[-1]) < 1.1: xticks = xticks[:-1] while xticks[0] < 0: xticks = xticks[1:] xticks.append(nbins-1) xticks = list(sorted(xticks)) plt.gca().set_xticks(xticks) plt.xlim([int(np.ceil(-0.05*nbins)),int(np.ceil(nbins*1.05))]) plt.legend(loc='upper right') plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight') plt.clf() plt.close()
def plot_1d(dataset, nbins): data = np.loadtxt('experiments/uci/data/splits/{0}_all.csv'.format(dataset), skiprows=1, delimiter=',')[:,-1] with sns.axes_style('white'): plt.rc('font', weight='bold') plt.rc('grid', lw=2) plt.rc('lines', lw=3) plt.figure(1) plt.hist(data, bins=np.arange(nbins+1), color='blue') plt.ylabel('Count', weight='bold', fontsize=24) xticks = list(plt.gca().get_xticks()) while (nbins-1) / float(xticks[-1]) < 1.1: xticks = xticks[:-1] while xticks[0] < 0: xticks = xticks[1:] xticks.append(nbins-1) xticks = list(sorted(xticks)) plt.gca().set_xticks(xticks) plt.xlim([int(np.ceil(-0.05*nbins)),int(np.ceil(nbins*1.05))]) plt.legend(loc='upper right') plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight') plt.clf() plt.close()
def onehist(x,xlabel='',fontsize=12): """ Script that plots the histogram of x with the corresponding xlabel. """ pylab.clf() pylab.rcParams.update({'font.size': fontsize}) pylab.hist(x,histtype='stepfilled') pylab.legend() #### Change the X-axis appropriately #### pylab.xlabel(xlabel) pylab.ylabel('Number') pylab.draw() pylab.show()
def threehistsx(x1,x2,x3,x1leg='$x_1$',x2leg='$x_2$',x3leg='$x_3$',fig=1,fontsize=12,bins1=10,bins2=10,bins3=10): """ Script that pretty-plots three histograms of quantities x1, x2 and x3. Arguments: :param x1,x2,x3: arrays with data to be plotted :param x1leg, x2leg, x3leg: legends for each histogram :param fig: which plot window should I use? Example: x1=Lbol(AD), x2=Lbol(JD), x3=Lbol(EHF10) >>> threehists(x1,x2,x3,38,44,'AD','JD','EHF10','$\log L_{\\rm bol}$ (erg s$^{-1}$)') Inspired by http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label. """ pylab.rcParams.update({'font.size': fontsize}) pylab.figure(fig) pylab.clf() pylab.subplot(3,1,1) pylab.hist(x1,label=x1leg,color='b',bins=bins1) pylab.legend(loc='best',frameon=False) pylab.subplot(3,1,2) pylab.hist(x2,label=x2leg,color='r',bins=bins2) pylab.legend(loc='best',frameon=False) pylab.subplot(3,1,3) pylab.hist(x3,label=x3leg,color='y',bins=bins3) pylab.legend(loc='best',frameon=False) pylab.minorticks_on() pylab.subplots_adjust(hspace=0.15) pylab.draw() pylab.show()
def plot_feat_hist(data_name_list, filename=None): pylab.clf() num_rows = 1 + (len(data_name_list) - 1) / 2 num_cols = 1 if len(data_name_list) == 1 else 2 pylab.figure(figsize=(5 * num_cols, 4 * num_rows)) for i in range(num_rows): for j in range(num_cols): pylab.subplot(num_rows, num_cols, 1 + i * num_cols + j) x, name = data_name_list[i * num_cols + j] pylab.title(name) pylab.xlabel('Value') pylab.ylabel('Density') # the histogram of the data max_val = np.max(x) if max_val <= 1.0: bins = 50 elif max_val > 50: bins = 50 else: bins = max_val n, bins, patches = pylab.hist( x, bins=bins, normed=1, facecolor='green', alpha=0.75) pylab.grid(True) if not filename: filename = "feat_hist_%s.png" % name pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_feat_hist(data_name_list, filename=None): if len(data_name_list) > 1: assert filename is not None pylab.figure(num=None, figsize=(8, 6)) num_rows = int(1 + (len(data_name_list) - 1) / 2) num_cols = int(1 if len(data_name_list) == 1 else 2) pylab.figure(figsize=(5 * num_cols, 4 * num_rows)) for i in range(num_rows): for j in range(num_cols): pylab.subplot(num_rows, num_cols, 1 + i * num_cols + j) x, name = data_name_list[i * num_cols + j] pylab.title(name) pylab.xlabel('Value') pylab.ylabel('Fraction') # the histogram of the data max_val = np.max(x) if max_val <= 1.0: bins = 50 elif max_val > 50: bins = 50 else: bins = max_val n, bins, patches = pylab.hist( x, bins=bins, normed=1, alpha=0.75) pylab.grid(True) if not filename: filename = "feat_hist_%s.png" % name.replace(" ", "_") pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def plot_feat_hist(data_name_list, filename=None): if len(data_name_list)>1: assert filename is not None pylab.figure(num=None, figsize=(8, 6)) num_rows = 1 + (len(data_name_list) - 1) / 2 num_cols = 1 if len(data_name_list) == 1 else 2 pylab.figure(figsize=(5 * num_cols, 4 * num_rows)) for i in range(num_rows): for j in range(num_cols): pylab.subplot(num_rows, num_cols, 1 + i * num_cols + j) x, name = data_name_list[i * num_cols + j] pylab.title(name) pylab.xlabel('Value') pylab.ylabel('Fraction') # the histogram of the data max_val = np.max(x) if max_val <= 1.0: bins = 50 elif max_val > 50: bins = 50 else: bins = max_val n, bins, patches = pylab.hist( x, normed=1, facecolor='blue', alpha=0.75) pylab.grid(True) if not filename: filename = "feat_hist_%s.png" % name.replace(" ", "_") pylab.savefig(os.path.join(CHART_DIR, filename), bbox_inches="tight")
def twohists(x1,x2,xmin,xmax,range=None,x1leg='$x_1$',x2leg='$x_2$',xlabel='',fig=1,sharey=False,fontsize=12,bins1=10,bins2=10): """ Script that plots two histograms of quantities x1 and x2 sharing the same X-axis. :param x1,x2: arrays with data to be plotted :param xmin,xmax: lower and upper range of plotted values, will be used to set a consistent x-range for both histograms. :param x1leg, x2leg: legends for each histogram :param xlabel: self-explanatory. :param bins1,bins2: number of bins in each histogram :param fig: which plot window should I use? :param range: in the form (xmin,xmax), same as range argument for hist and applied to both histograms. Inspired by `Scipy <http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label>`_. """ pylab.rcParams.update({'font.size': fontsize}) fig=pylab.figure(fig) pylab.clf() a=fig.add_subplot(2,1,1) if sharey==True: b=fig.add_subplot(2,1,2, sharex=a, sharey=a) else: b=fig.add_subplot(2,1,2, sharex=a) a.hist(x1,bins1,label=x1leg,color='b',histtype='stepfilled',range=range) a.legend(loc='best',frameon=False) a.set_xlim(xmin,xmax) b.hist(x2,bins2,label=x2leg,color='r',histtype='stepfilled',range=range) b.legend(loc='best',frameon=False) pylab.setp(a.get_xticklabels(), visible=False) b.set_xlabel(xlabel) b.set_ylabel('Number',verticalalignment='bottom') pylab.minorticks_on() pylab.subplots_adjust(hspace=0.15) pylab.draw() pylab.show()
def threehists(x1,x2,x3,xmin,xmax,x1leg='$x_1$',x2leg='$x_2$',x3leg='$x_3$',xlabel='',fig=1,sharey=False,fontsize=12): """ Script that plots three histograms of quantities x1, x2 and x3 sharing the same X-axis. Arguments: - x1,x2,x3: arrays with data to be plotted - xmin,xmax: lower and upper range of plotted values, will be used to set a consistent x-range for both histograms. - x1leg, x2leg, x3leg: legends for each histogram - xlabel: self-explanatory. - sharey: sharing the Y-axis among the histograms? - fig: which plot window should I use? Example: x1=Lbol(AD), x2=Lbol(JD), x3=Lbol(EHF10) >>> threehists(x1,x2,x3,38,44,'AD','JD','EHF10','$\log L_{\\rm bol}$ (erg s$^{-1}$)',sharey=True) Inspired by `Scipy <http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label>`_. """ pylab.rcParams.update({'font.size': fontsize}) fig=pylab.figure(fig) pylab.clf() a=fig.add_subplot(3,1,1) if sharey==True: b=fig.add_subplot(3,1,2, sharex=a, sharey=a) c=fig.add_subplot(3,1,3, sharex=a, sharey=a) else: b=fig.add_subplot(3,1,2, sharex=a) c=fig.add_subplot(3,1,3, sharex=a) a.hist(x1,label=x1leg,color='b',histtype='stepfilled') a.legend(loc='best',frameon=False) a.set_xlim(xmin,xmax) b.hist(x2,label=x2leg,color='r',histtype='stepfilled') b.legend(loc='best',frameon=False) c.hist(x3,label=x3leg,color='y',histtype='stepfilled') c.legend(loc='best',frameon=False) pylab.setp(a.get_xticklabels(), visible=False) pylab.setp(b.get_xticklabels(), visible=False) c.set_xlabel(xlabel) b.set_ylabel('Number') pylab.minorticks_on() pylab.subplots_adjust(hspace=0.15) pylab.draw() pylab.show()
def fourcumplot(x1,x2,x3,x4,xmin,xmax,x1leg='$x_1$',x2leg='$x_2$',x3leg='$x_3$',x4leg='$x_3$',xlabel='',ylabel='$N(x>x\')$',fig=1,sharey=False,fontsize=12,bins1=50,bins2=50,bins3=50,bins4=50): """ Script that plots the cumulative histograms of four variables x1, x2, x3 and x4 sharing the same X-axis. For each bin, Y is the fraction of the sample with values above X. Arguments: - x1,x2,x3,x4: arrays with data to be plotted - xmin,xmax: lower and upper range of plotted values, will be used to set a consistent x-range for both histograms. - x1leg, x2leg, x3leg, x4leg: legends for each histogram - xlabel: self-explanatory. - sharey: sharing the Y-axis among the histograms? - bins1,bins2,...: number of bins in each histogram - fig: which plot window should I use? Inspired by `Scipy <http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label>`_. v1 Jun. 2012: inherited from fourhists. """ pylab.rcParams.update({'font.size': fontsize}) fig=pylab.figure(fig) pylab.clf() a=fig.add_subplot(4,1,1) if sharey==True: b=fig.add_subplot(4,1,2, sharex=a, sharey=a) c=fig.add_subplot(4,1,3, sharex=a, sharey=a) d=fig.add_subplot(4,1,4, sharex=a, sharey=a) else: b=fig.add_subplot(4,1,2, sharex=a) c=fig.add_subplot(4,1,3, sharex=a) d=fig.add_subplot(4,1,4, sharex=a) a.hist(x1,bins1,label=x1leg,color='b',cumulative=-True,normed=True,histtype='stepfilled') a.legend(loc='best',frameon=False) a.set_xlim(xmin,xmax) b.hist(x2,bins2,label=x2leg,color='r',cumulative=-True,normed=True,histtype='stepfilled') b.legend(loc='best',frameon=False) c.hist(x3,bins3,label=x3leg,color='y',cumulative=-True,normed=True,histtype='stepfilled') c.legend(loc='best',frameon=False) d.hist(x4,bins4,label=x4leg,color='g',cumulative=-True,normed=True,histtype='stepfilled') d.legend(loc='best',frameon=False) pylab.setp(a.get_xticklabels(), visible=False) pylab.setp(b.get_xticklabels(), visible=False) pylab.setp(c.get_xticklabels(), visible=False) d.set_xlabel(xlabel) c.set_ylabel(ylabel) pylab.minorticks_on() pylab.subplots_adjust(hspace=0.15) pylab.draw() pylab.show()