def hub_picture(data, dataset, method): fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 10)) ax1.set_xticks([]) ax2.set_xticks([]) ax1.set_ylabel("Proportion", fontsize=16) ax2.set_ylabel("Proportion", fontsize=16) width = 0.8 startposition = 0.8 color_list = ["tomato", 'deepskyblue', "orange"] label_list = ["more than 5 times", "1 to 5 times", "never appear"] legend_list = list() for i in range(len(method)): bottom = 0 for j in range(3): bar1, = ax1.bar(startposition + i * width, height=data[j][i][0], width=0.5, bottom=bottom, color=color_list[j], align="edge", edgecolor="black") bottom += data[j][i][0] ax1.text(startposition + i * width + 0.1, y=-0.03, s=method[i], fontsize="14", rotation=-90) ax1.text(startposition + (len(method) / 2 - 2) * width, y=1.08, s=dataset[0], fontsize="14") for i in range(len(method)): bottom = 0 for j in range(3): bar2, = ax2.bar(startposition + i * width, height=data[j][i + len(method)][0], width=0.5, bottom=bottom, color=color_list[j], align="edge", edgecolor="black") bottom += data[j][i + len(method)][0] if i == 0: legend_list.append(bar2) ax2.text(startposition + i * width + 0.1, y=-0.03, s=method[i], fontsize="14", rotation=-90) ax2.text(startposition + (len(method) / 2 - 2) * width, y=1.08, s=dataset[1], fontsize="14") plt.figlegend(legend_list, labels=label_list, loc="best") # plt.legend() plt.show()
def plot(dataset, grid, trafo): assert os.path.exists('results.csv'), 'need to aggregate results first' df = pandas.read_csv('results.csv') df = df[(df.grid == grid) & (df.data == dataset) & (df.trafo == trafo)] fig, ax = pylab.subplots(1, 1, dpi=200) # fig.subplots_adjust(left=.1, bottom=.225, right=.98, top=.94, hspace=0.35, wspace=0.25) models = [ NA, NB, NF, 'svm-rbf', 'svm-poly', 'svm-sigmoid', 'svm-primal', 'logisticregression', 'lda', 'rforest', 'etrees', 'xgb' ] names = [name_dict[i] for i in models] palette = [color_dict[i] for i in models] palette = seaborn.xkcd_palette(palette) legend = [mpatches.Patch(color=i, label=j) for i, j in zip(palette, names)] seaborn.lineplot(x='sample_size', y='test_score', hue='model', hue_order=models, data=df, ax=ax, palette=palette, legend=False, err_style="bars", ci=CI) ax.set_title(f'Dataset: {dataset}', fontsize=8) ax.set_xscale('log') ax.set_xticks(df.sample_size.unique()) ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) ax.get_xaxis().set_tick_params(which='minor', size=0) ax.get_xaxis().set_tick_params(which='minor', width=0) ax.set_ylabel('Accuracy') ax.set_xlabel('Sample Size') # ax.set_ylim(0.65, 1) pylab.figlegend(handles=legend, ncol=2, fontsize=8, loc='lower center', frameon=False) fig.savefig(f'plots/{dataset}.png')
def main(): sns.set_style("whitegrid") sns.set_context("notebook") n_species=['H', 'He','N','N2', 'O', 'O2'] Nn = sp.array([22340974.0, 1.170203e+08, 269365.12, 8.7995607e+12,5.6790339e+11,1.9389578e+12])*1e6 Tn = 179.29138 n_datablock = sp.zeros((len(Nn),2)) n_datablock[:,0] = Nn n_datablock[:,1] = Tn species = ['NO+','O2+','e-'] Ni = sp.array([1998.2988800000001, 57.700355999999999])*1e6 Ti = 175.1 Ne= 2055.9992320000001 *1e6 Te = 175.1 datablock = sp.zeros((len(Ni)+1,2)) datablock[:-1,0] = Ni datablock[:-1,1] = Ti datablock[-1,0] = Ne datablock[-1,1] = Te (nuparr,nuperp) = get_collisionfreqs(datablock,species,n_datablock, n_species) # ISS1 = ISSnew.ISRSpectrum(centerFrequency = 449e6, bMag = 0.4e-4, nspec=256, sampfreq=50e3,dFlag=True) (omeg,spec_1)=ISS1.getspecsep(datablock,species,vel = 0.0,rcsflag=False) (omeg,spec_2)=ISS1.getspecsep(datablock,species,vel = 0.0,rcsflag=False,col_calc = True) (omeg,spec_3)=ISS1.getspecsep(datablock,species,vel = 0.0,rcsflag=False,col_calc = True,n_datablock=n_datablock,n_species=n_species) (figmplf, axmat) = plt.subplots(1, 1,figsize=(20, 15), facecolor='w') curax = axmat lines = [None]*3 labels = ['Spectrum','Spectrum with Coulomb Collisions','Spectrum with Columb and Neutral Collisions'] lines[0] = curax.plot(omeg*1e-3,spec_1,label='Output',linewidth=5)[0] lines[1] = curax.plot(omeg*1e-3,spec_2,label='Output',linewidth=5)[0] lines[2] = curax.plot(omeg*1e-3,spec_3,label='Output',linewidth=5)[0] figmplf.suptitle('Spectrums with and without collisions') plt.figlegend( lines, labels, loc = 'lower center', ncol=5, labelspacing=0. ) curpath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) imagepath = os.path.join(os.path.split(curpath)[0],'Doc','Figs') plt.savefig(os.path.join(imagepath,'Collisions.png'))
def main(): sns.set_style("whitegrid") sns.set_context("notebook") n_species=['H', 'He','N','N2', 'O', 'O2'] Nn = sp.array([22340974.0, 1.170203e+08, 269365.12, 8.7995607e+12,5.6790339e+11,1.9389578e+12])*1e6 Tn = 179.29138 n_datablock = sp.zeros((len(Nn),2)) n_datablock[:,0] = Nn n_datablock[:,1] = Tn species = ['NO+','O2+','e-'] Ni = sp.array([1998.2988800000001, 57.700355999999999])*1e6 Ti = 175.1 Ne= 2055.9992320000001 *1e6 Te = 175.1 datablock = sp.zeros((len(Ni)+1,2)) datablock[:-1,0] = Ni datablock[:-1,1] = Ti datablock[-1,0] = Ne datablock[-1,1] = Te (nuparr,nuperp) = get_collisionfreqs(datablock,species,n_datablock, n_species) # ISS1 = ISSnew.ISRSpectrum(centerFrequency = 449e6, bMag = 0.4e-4, nspec=256, sampfreq=50e3,dFlag=True) (omeg,spec_1)=ISS1.getspecsep(datablock,species,vel = 0.0,rcsflag=False) (omeg,spec_2)=ISS1.getspecsep(datablock,species,vel = 0.0,rcsflag=False,col_calc = True) (omeg,spec_3)=ISS1.getspecsep(datablock,species,vel = 0.0,rcsflag=False,col_calc = True,n_datablock=n_datablock,n_species=n_species) (figmplf, axmat) = plt.subplots(1, 1,figsize=(20, 15), facecolor='w') curax = axmat lines = [None]*3 labels = ['Spectrum','Spectrum with Coulomb Collisions','Spectrum with Columb and Neutral Collisions'] lines[0] = curax.plot(omeg*1e-3,spec_1,label='Output',linewidth=5)[0] lines[1] = curax.plot(omeg*1e-3,spec_2,label='Output',linewidth=5)[0] lines[2] = curax.plot(omeg*1e-3,spec_3,label='Output',linewidth=5)[0] figmplf.suptitle('Spectrums with and without collisions') plt.figlegend( lines, labels, loc = 'lower center', ncol=5, labelspacing=0. ) plt.savefig('Collisions.png')
def main(): sns.set_style("whitegrid") sns.set_context("notebook") Ne = 1e11 Ti = 3e3 Te = 3e3 databloc = sp.array([[Ne,Ti],[Ne,Te]]) species = ['O+','e-'] Cia = sp.sqrt(v_Boltz*(Te+3.*Ti)/16./v_amu) #make list of dictionaries biglist = [{'name':'AMISR','Fo':449e6,'Fs':50e3,'alpha':70.}, {'name':'Sondrestrom','Fo':1290e6,'Fs':100e3,'alpha':80.}, {'name':'Haystack','Fo':440e6,'Fs':50e3,'alpha':65.}, {'name':'Arecibo','Fo':430e6,'Fs':50e3,'alpha':45.}, {'name':'Jicamarca','Fo':50e6,'Fs':10e3,'alpha':1.}] (figmplf, axmat) = plt.subplots(3, 2,figsize=(20, 15), facecolor='w') axvec = axmat.flatten() lines = [None]*2 labels = ['Spectrum','Ion Acoustic Frequency'] for ima,idict in enumerate(biglist): curax = axvec[ima] k = 2.0*sp.pi*2*idict['Fo']/v_C_0 xloc = sp.array([-k*Cia,k*Cia])/2/sp.pi/2 ISS1 = ISSnew.ISRSpectrum(centerFrequency = idict['Fo'], bMag = 0.4e-4, nspec=256, sampfreq=idict['Fs'],dFlag=True) (omeg,spec)=ISS1.getspecsep(databloc,species,vel = 0.0, alphadeg=idict['alpha'],rcsflag=False) lines[0] = curax.plot(omeg*1e-3,spec,label='Output',linewidth=5)[0] lines[1] = curax.stem(xloc*1e-3, sp.ones(2)*sp.amax(spec), linefmt='g--', markerfmt='go', basefmt=' ')[0] curax.set_xlabel('f in kHz') curax.set_ylabel('Amp') curax.set_title(idict['name']+ ' Spectrtum') figmplf.suptitle(r'Spectrums $N_e$ = {0:.1e}m$^{{-3}}$, $T_e$ = {1:.0f}$^o$K, $T_i$ = {2:.0f}$^o$K'.format(Ne,Te,Ti), fontsize=20) plt.figlegend( lines, labels, loc = 'lower center', ncol=5, labelspacing=0. ) curpath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) imagepath = os.path.join(os.path.split(curpath)[0],'Doc','Figs') plt.savefig(os.path.join(imagepath,'DifferentSystems.png'))
def plotVarsGroupedBySex(varList, varNames, badStringList): fig, axes = plt.subplots(1,2) for i in range(2): group = traindf.groupby([varList[i], 'Sex', 'Survived']) group = group.size().unstack() group = group.div(group.sum(1), axis=0) cols = [[darkpink, darkblue],[lightpink, lightblue]] group.plot(kind='barh', stacked=True, ax=axes[i],legend=False, color=cols) labels = removeBadStringFromLabels(axes[i], badStringList) axes[i].set_yticklabels(labels) axes[i].get_xaxis().set_ticks([]) axes[i].set_ylabel('') axes[i].set_title(varNames[i]) if i ==1: axes[i].yaxis.tick_right() axes[i].yaxis.set_label_position("right") handles, labels = axes[0].get_legend_handles_labels() plt.figlegend(handles[0], ['die', 'die'], loc='lower left') plt.figlegend(handles[1], ['live','live'], loc='lower right') fig.show()
def pretty_template_plot(template, size=(18.5, 10.5), save=False, title=False, background=False, picks=False): r"""Function to make a pretty plot of a single template, designed to work \ better than the default obspy plotting routine for short data lengths. :type template: :class: obspy.Stream :param template: Template stream to plot :type size: tuple :param size: tuple of plot size :type save: bool :param save: if False will plot to screen, if True will save :type title: bool :param title: String if set will be the plot title :type background: :class: obspy.stream :param background: Stream to plot the template within. :type picks: list of obspy.core.event.pick :param picks: List of obspy type picks. """ fig, axes = plt.subplots(len(template), 1, sharex=True, figsize=size) if len(template) > 1: axes = axes.ravel() else: return if not background: mintime = template.sort(['starttime'])[0].stats.starttime else: mintime = background.sort(['starttime'])[0].stats.starttime template.sort(['network', 'station', 'starttime']) lengths = [] lines = [] labels = [] for i, tr in enumerate(template): delay = tr.stats.starttime - mintime y = tr.data x = np.linspace(0, (len(y)-1) * tr.stats.delta, len(y)) x += delay if background: btr = background.select(station=tr.stats.station, channel=tr.stats.channel)[0] bdelay = btr.stats.starttime - mintime by = btr.data bx = np.linspace(0, (len(by)-1) * btr.stats.delta, len(by)) bx += bdelay axes[i].plot(bx, by, 'k', linewidth=1) template_line, = axes[i].plot(x, y, 'r', linewidth=1.1, label='Template') if i == 0: lines.append(template_line) labels.append('Template') lengths.append(max(bx[-1], x[-1])) else: template_line, = axes[i].plot(x, y, 'k', linewidth=1.1, label='Template') if i == 0: lines.append(template_line) labels.append('Template') lengths.append(x[-1]) # print(' '.join([tr.stats.station, str(len(x)), str(len(y))])) axes[i].set_ylabel('.'.join([tr.stats.station, tr.stats.channel]), rotation=0, horizontalalignment='right') axes[i].yaxis.set_ticks([]) # Plot the picks if they are given if picks: tr_picks = [pick for pick in picks if pick.waveform_id.station_code == tr.stats.station and pick.waveform_id.channel_code[0] + pick.waveform_id.channel_code[-1] == tr.stats.channel[0] + tr.stats.channel[-1]] for pick in tr_picks: if 'P' in pick.phase_hint.upper(): pcolor = 'red' label = 'P-pick' elif 'S' in pick.phase_hint.upper(): pcolor = 'blue' label = 'S-pick' else: pcolor = 'k' label = 'Unknown pick' pdelay = pick.time - mintime # print(pdelay) line = axes[i].axvline(x=pdelay, color=pcolor, linewidth=2, linestyle='--', label=label) if label not in labels: lines.append(line) labels.append(label) # axes[i].plot([pdelay, pdelay], []) axes[i].set_xlim([0, max(lengths)]) axes[len(template)-1].set_xlabel('Time (s) from start of template') plt.figlegend(lines, labels, 'upper right') if title: axes[0].set_title(title) else: plt.subplots_adjust(top=0.98) plt.tight_layout() plt.subplots_adjust(hspace=0) if not save: plt.show() plt.close() else: plt.savefig(save)
fig_width_mm=200, fig_height_mm=155, size_axe=[0.13, 0.127, 0.84, 0.835]) ax1.set_xscale('log') ax1.set_yscale('log') ax1.set_xlabel('$r$ (km)') ax1.set_ylabel('$F_T$, $F_L$') l_FT = ax1.plot(r, flatnessT, 'k', linewidth=2) l_FL = ax1.plot(r, flatnessL, 'y', linewidth=2) plt.rc('legend', numpoints=1) leg1 = plt.figlegend([l_FT[0], l_FL[0]], ['$F_T$', '$F_L$'], loc=(0.2, 0.2), labelspacing=0.18) cond = np.logical_and(r > 3, r < 2e2) ax1.plot(r[cond], 6e2 * r[cond]**(-1), 'k', linewidth=1) ax1.text(1e1, 1.8e1, '$r^{-1}$', fontsize=fontsize) cond = np.logical_and(r > 3, r < 1e2) ax1.plot(r[cond], 4e4 * r[cond]**(-3. / 2), 'k--', linewidth=1) ax1.text(1e1, 1.8e3, '$r^{-3/2}$', fontsize=fontsize) r1 = 1 r2 = 3e3 ax1.set_xlim([r1, r2]) ax1.set_ylim([1, 8e3])
ax1.set_yscale('log') linewidth = 2.2 l_FT = ax1.plot(radim, flatnessT, 'k', linewidth=linewidth) l_FL = ax1.plot(radim, flatnessL, 'y', linewidth=linewidth) cond = np.logical_and(radim > 1.4e-2, radim < 2e-1) ax1.plot(radim[cond], 4e0 * rxs[cond]**(-1), 'k', linewidth=1) ax1.text(4e-2, 1e1, '$r^{-1}$', fontsize=fontsize) ax1.set_xlim([r1, r2]) # ax1.set_ylim([1, 10]) plt.rc('legend', numpoints=1) leg1 = plt.figlegend([l_FT[0], l_FL[0]], ['$F_T$', '$F_L$'], loc=(0.2, 0.2), labelspacing=0.18) ax2 = fig.add_axes([0.62, 0.6, 0.32, 0.32]) ax2.set_xscale('log') ax2.plot(radim, flatnessT / flatnessL, 'k', linewidth=linewidth) value_shocks = 1.5 ax2.plot([r1, r2], [value_shocks, value_shocks], 'k:', linewidth=linewidth) ax2.set_xlim([r1, r2]) ax2.set_ylim([0, 1.7]) ax2.set_yticks([0, 0.5, 1, 1.5]) ax2.set_xlabel('$r/L_f$') ax2.set_ylabel('$F_T/F_L$')
def main(): Ne = 1e11 Ti = 3e3 Te = 3e3 databloc = sp.array([[Ne, Ti], [Ne, Te]]) species = ['O+', 'e-'] Cia = sp.sqrt(v_Boltz * (Te + 3. * Ti) / 16. / v_amu) #make list of dictionaries biglist = [{ 'name': 'AMISR', 'Fo': 449e6, 'Fs': 50e3, 'alpha': 70. }, { 'name': 'Sondrestrom', 'Fo': 1290e6, 'Fs': 100e3, 'alpha': 80. }, { 'name': 'Haystack', 'Fo': 440e6, 'Fs': 50e3, 'alpha': 65. }, { 'name': 'Arecibo', 'Fo': 430e6, 'Fs': 50e3, 'alpha': 45. }, { 'name': 'Jicamarca', 'Fo': 50e6, 'Fs': 10e3, 'alpha': 1. }] (figmplf, axmat) = plt.subplots(3, 2, figsize=(20, 15), facecolor='w') axvec = axmat.flatten() lines = [None] * 2 labels = ['Spectrum', 'Ion Acoustic Frequency'] for ima, idict in enumerate(biglist): curax = axvec[ima] k = 2.0 * sp.pi * 2 * idict['Fo'] / v_C_0 xloc = sp.array([-k * Cia, k * Cia]) / 2 / sp.pi / 2 ISS1 = ISSnew.ISRSpectrum(centerFrequency=idict['Fo'], bMag=0.4e-4, nspec=256, sampfreq=idict['Fs'], dFlag=True) (omeg, spec) = ISS1.getspecsep(databloc, species, vel=0.0, alphadeg=idict['alpha'], rcsflag=False) lines[0] = curax.plot(omeg * 1e-3, spec, label='Output', linewidth=5)[0] lines[1] = curax.stem(xloc * 1e-3, sp.ones(2) * sp.amax(spec), linefmt='g--', markerfmt='go', basefmt=' ')[0] curax.set_xlabel('f in kHz') curax.set_ylabel('Amp') curax.set_title(idict['name'] + ' Spectrtum') figmplf.suptitle( r'Spectrums $N_e$ = {0:.1e}m$^{{-3}}$, $T_e$ = {1:.0f}$^o$K, $T_i$ = {2:.0f}$^o$K' .format(Ne, Te, Ti), fontsize=20) plt.figlegend(lines, labels, loc='lower center', ncol=5, labelspacing=0.) plt.savefig('DifferentSystems.png')
def running_time(dir, method_list, dataset_list): user_cmap = config_color_map() color_map = cm.get_cmap("tab20c") print() cnorm = matplotlib.colors.Normalize(vmin=0, vmax=20) sclarmap = cm.ScalarMappable(norm=cnorm, cmap=color_map) data = xlrd.open_workbook(dir) tabel = data.sheet_by_name("run_time") # *****************行******************* left_row_start = 3 left_row_end = 14 right_row_start = 3 right_row_end = 9 # ***********************列坐标************* left_col_start = 3 left_col_end = 31 right_col_start = 34 right_col_end = 65 data_kind = [ "DBP_en_DBP_de", "DBP_en_DBP_fr", "DBP_en_WD_en", "DBP_en_YG_en" ] left_data = [] right_data = [] plt.figure(figsize=(32, 6)) sub_i = 1 for dataset in dataset_list: remove = data_kind.index(dataset) left_col_start += 11 * remove left_col_end += 11 * remove right_col_start += 6 * remove right_col_end += 6 * remove for i in range(left_row_start, left_row_end): line_all_row = tabel.row_slice(i, left_col_start, left_col_end) left_data.append(line_all_row) for i in range(right_row_start, right_row_end): line_all_row = tabel.row_slice(i, right_col_start, right_col_end) right_data.append(line_all_row) print(left_data) print(right_data) all_data = left_data + right_data # ****************************************开始画图相关工作********************* x_label_list = [["15K_V1", "15K_V2"], ["100K_V1", "100K_V2"]] color_list = [ "darkorange", "forestgreen", "lightsteelblue", "rosybrown", "gold", "indigo", "red", "sienna", "skyblue", "deeppink", "slategray", "peru", "grey", "olive", "cyan", "blue", "lightpink" ] color_list = [sclarmap.to_rgba(i) for i in range(17)] # ****************现在是17个方法****************** first_x_position = [ 0.5, 12, ] width = 0.5 fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8)) ax1.set_xticks([]) ax2.set_xticks([]) ax1.set_ylabel("Time(s)", fontsize=16) ax2.set_ylabel("Time(s)", fontsize=16) for method_pos in range(len(first_x_position)): for i in range(len(all_data)): bar1, = ax1.bar(first_x_position[method_pos] + i * (width), height=all_data[i][5 + method_pos * 7].value, width=0.5, bottom=0, color=color_list[i], edgecolor="black", label=method_list[i]) ax1.text(x=3.5, y=-120, s=x_label_list[0][0], fontsize="16") ax1.text(x=15.5, y=-120, s=x_label_list[0][1], fontsize="16") legend_bar = list() for method_pos in range(len(first_x_position)): for i in range(len(all_data)): bar2, = ax2.bar(first_x_position[method_pos] + i * (width), height=all_data[i][14 + 5 + method_pos * 7].value, width=0.5, bottom=0, color=color_list[i], edgecolor="black", label=method_list[i]) if i < len(first_x_position) / 2: legend_bar.append(bar2) ax2.text(x=3.5, y=-2400, s=x_label_list[1][0], fontsize="16") ax2.text(x=15.5, y=-2400, s=x_label_list[1][1], fontsize="16") plt.figlegend(legend_bar, labels=method_list, loc="upper center", ncol=10, bbox_to_anchor=(0.5, 0.95)) # plt.legend() plt.show()
def plot_methods_recall_ratio(datasets, methods, method_labels): n_datasets = len(datasets) fig = plt.figure(figsize=(0.55 + 3.333 * len(datasets), 6.)) plt.rcParams.update({'font.size': 13}) plt.subplots_adjust(bottom=0.15, top=0.85, hspace=0.35) for di, (dataset, dataset_label) in enumerate(zip(datasets, dataset_labels)): ax_recall = plt.subplot(2, n_datasets, di + 1) plt.xlabel('Recall (%)') plt.title(dataset_label) if di == 0: plt.ylabel('Query time (ms)') plt.xlim(0, 100) plt.title(dataset_label) ax_ratio = plt.subplot(2, n_datasets, n_datasets + di + 1) plt.xlabel('Ratio') ax_ratio.set_xlim(1., 1.5) ax_ratio.set_xticks([1., 1.1, 1.2, 1.3, 1.4, 1.5]) # plt.xlim(0, 100) if di == 0: plt.ylabel('Query time (ms)') # plt.ylim(ymin=0) for method, method_label, method_color, method_marker in zip( methods, method_labels, method_colors, method_markers): filename_prefix = get_file_prefix(dataset, method, distance) filename = get_latest_res(filename_prefix) if filename is None: continue print(filename) time_recalls = [] time_ratios = [] settings = [] ress = [] for setting, res in parse_res(filename): # print(setting, res) time_recalls += [[gettime(res), getrecall(res)]] time_ratios += [[gettime(res), getratio(res)]] ress += [res] settings += [setting] time_recalls = np.array(time_recalls) time_ratios = np.array(time_ratios) # print(xys) lower_x, lower_y = lower_bound_curve(time_recalls) ax_recall.semilogy(lower_x, lower_y, '-', color=method_color, marker=method_marker, label=method_label if di == 0 else "", markevery=10, markerfacecolor='none', markersize=10) # lower_x, lower_y = lower_bound_curve2(time_ratios[:, 0], time_ratios[:, 1]) qtime_ratio_lower = lower_bound_curve2(time_ratios[:, 0], time_ratios[:, 1]) ax_ratio.semilogy(qtime_ratio_lower[:, 1], qtime_ratio_lower[:, 0], '-', color=method_color, marker=method_marker, label="", markevery=5, markerfacecolor='none', markersize=10) plt.figlegend(fontsize=16, bbox_to_anchor=(0.07, 0.85, 0.75, 0.2), loc="center", mode="expand", borderaxespad=0, ncol=len(methods)) plt.savefig('saved_figure/time_recall_ratio_%s.png' % distance, bbox_inches='tight', pad_inches=0.27, dpi=fig.dpi) plt.savefig('saved_figure/time_recall_ratio_%s.eps' % distance, bbox_inches='tight', pad_inches=0.27, dpi=fig.dpi) plt.show()
from Simulator import Simulator from States import S_Start, S_DeadAge, S_DeadCancer from numpy import average from math import exp,log import matplotlib.pylab as p import HW2_model as Model for cancer_mortality_rate in [0,.02,.05,.10]: points = [] Model.TR[S_Start][S_DeadCancer]= lambda pt: \ 1 - exp(-cancer_mortality_rate * pt.time_increment) for start_age in range(25, 100): s= Simulator(iters=5000, age=start_age, model=Model, time_increment = 1.0) s.run() le = average([pt.Age()-start_age for pt in s.patients]) points.append((start_age, le)) p.plot([x[0] for x in points], [y[1] for y in points], label="Gompertz plus cancer mortality = %s"%cancer_mortality_rate) lines = p.gca().lines p.figlegend(lines, [l.get_label() for l in lines], 'upper right') p.show()
for Pclass in classes: if gender=='male': colorscheme = [lightblue, darkblue] #blue for boys row=0 else: colorscheme = [lightpink, darkpink] #pink for a girl row=1 group = train[(train.Sex==gender)&(train.Pclass==Pclass)] group = group.groupby(['Embarked', 'Survived']).size().unstack() group = group.div(group.sum(1), axis=0) group.plot(kind='barh', ax=axes2[row, (int(Pclass)-1)], color=colorscheme, stacked=True, legend=False).set_title("{} {}".format("Class", Pclass)).axes.get_xaxis().set_ticks([]) plt.subplots_adjust(wspace=0.4, hspace=1.3) fhandles, flabels = axes2[1,2].get_legend_handles_labels() mhandles, mlabels = axes2[0,2].get_legend_handles_labels() plt.figlegend(fhandles, ('die', 'live'), title='Female', loc='center', bbox_to_anchor=(0.06, 0.2, 1.1, .50)) plt.figlegend(mhandles, ('die', 'live'), 'center', title='Male',bbox_to_anchor=(-0.15, 0.2, 1.1, .50)) fig2.show() bins = [0,5,14, 25, 40, 60, 100] binNames =['Young Child', 'Child', 'Young Adult', 'Adult', 'Middle Aged', 'Older'] binAge = pd.cut(train.Age, bins, labels=binNames) #cut uses the given bins, or if passed an integer, divides the range evenly binFare = pd.qcut(train.Fare, 3, labels=['Cheap', 'Middle', 'Expensive']) #qcut does quantiles fig3, axes3 = plt.subplots(1,2) binVars = [binAge, binFare] varNames = ['Age', 'Fare'] badStringList=['(', ')', 'female', 'male', ','] def removeBadStringFromString(string, badStringList):
def plot_data(data): def proportionSurvived(discreteVar): by_var = data.groupby([discreteVar, 'Survived']) table = by_var.size() table = table.unstack() normedtable = table.div(table.sum(1), axis=0) return normedtable discreteVarList = ['Sex', 'Pclass', 'Embarked'] fig1, axes1 = plt.subplots(3, 1) for i in range(3): var = discreteVarList[i] table = proportionSurvived(var) table.plot(kind='barh', stacked=True, ax=axes1[i]) #fig1.show() # Plot based on categories fig2, axes2 = plt.subplots(2, 3) genders = data.Sex.unique() classes = data.Pclass.unique() def normrgb(rgb): rgb = [float(x)/255 for x in rgb] return rgb darkpink, lightpink = normrgb([255, 20, 147]), normrgb([255, 182, 193]) darkblue, lightblue = normrgb([0, 0, 128]), normrgb([135, 206, 250]) for gender in genders: for pclass in classes: if gender == 'male': colorscheme = [lightblue, darkblue] row = 0 else: colorscheme = [lightpink, darkpink] row = 1 group = data[(data.Sex==gender)&(data.Pclass==pclass)] group = group.groupby(['Embarked', 'Survived']).size().unstack() group = group.div(group.sum(1), axis=0) group.plot(kind='barh', ax=axes2[row, (int(pclass)-1)], color=colorscheme, stacked=True, legend=False).set_title('Class '+str(pclass)).axes.get_xaxis().set_ticks([]) plt.subplots_adjust(wspace=0.4, hspace=1.3) fhandles, flabels = axes2[1,2].get_legend_handles_labels() mhandles, mlabels = axes2[0,2].get_legend_handles_labels() plt.figlegend(fhandles, ('die', 'live'), title='Female', loc='center', bbox_to_anchor=(0.06, 0.45, 1.1, .102)) plt.figlegend(mhandles, ('die', 'live'), 'center', title='Male',bbox_to_anchor=(-0.15, 0.45, 1.1, .102)) #fig2.show() # Plots using bins bins = [0, 5, 14, 25, 40, 60, 100] binNames = ['Young Child', 'Child', 'Young Adult', 'Adult', 'Middle Aged', 'Older'] binAge = pd.cut(data.Age, bins, labels=binNames) binFare = pd.qcut(data.Fare, 3, labels=['Cheap', 'Middle', 'Expensive']) fig3, axes3 = plt.subplots(1, 2) binVars = [binAge, binFare] varNames = ['Age', 'Fare'] badStringList = ['(', ')', 'female', 'male', ','] def removeBadStringFromString(string): for badString in ['(', ')', 'female', 'male']: string = string.replace(badString, '') return string def removeBadStringFromLabels(ax, badStringList): labels = [item.get_text() for item in ax.get_yticklabels()] labels = [removeBadStringFromString(label) for label in labels] return labels for i in range(2): group = data.groupby([binVars[i], 'Sex', 'Survived']) group = group.size().unstack() group = group.div(group.sum(1), axis=0) cols = [[lightpink, lightblue],[darkpink, darkblue]] group.plot(kind='barh', stacked=True, ax=axes3[i], legend=False, color=cols) labels = removeBadStringFromLabels(axes3[i], badStringList) axes3[i].set_yticklabels(labels) axes3[i].get_xaxis().set_ticks([]) axes3[i].set_ylabel('') axes3[i].set_title(varNames[i]) if i==1: axes3[i].yaxis.tick_right() axes3[i].yaxis.set_label_position("right") handles, labels = axes3[0].get_legend_handles_labels() plt.figlegend(handles[0], ['die', 'die'], loc='upper center') plt.figlegend(handles[1], ['live', 'live'], loc='lower center') fig3.show()
if gender=='male': colorscheme = [lightblue, darkblue] row=0 else: colorscheme = [lightpink, darkpink] row=1 group = traindf[(traindf.Sex==gender)&(traindf.Pclass==pclass)] group = group.groupby(['Embarked', 'Survived']).size().unstack() group = group.div(group.sum(1), axis=0) group.plot(kind='barh', ax=axes2[row, (int(pclass)-1)], color=colorscheme, stacked=True, legend=False).set_title('Class '+str(pclass)).axes.get_xaxis().set_ticks([]) plt.subplots_adjust(wspace=0.4, hspace=1.3) fhandles, flabels = axes2[1,2].get_legend_handles_labels() mhandles, mlabels = axes2[0,2].get_legend_handles_labels() plt.figlegend(fhandles, ('die', 'live'), title='Female', loc='center', bbox_to_anchor=(0.06, 0.45, 1.1, .102)) plt.figlegend(mhandles, ('die', 'live'), 'center', title='Male',bbox_to_anchor=(-0.15, 0.45, 1.1, .102)) fig2.show() def removeBadStringFromString(string, badStringList): for badString in badStringList: string = string.replace(badString, '') return string def removeBadStringFromLabels(ax, badStringList): labels = [item.get_text() for item in ax.get_yticklabels()] labels = [removeBadStringFromString(label, badStringList) for label in labels] return labels def plotVarsGroupedBySex(varList, varNames, badStringList):
def plot_gene_coverage(ke, f, x_exon, gene , chrom, sample_ids=None , save_dir=None, **kwargs): """ Plot a gene's DegNorm-estimated and original coverage matrices. Option to save image. :param ke: estimated coverage matrix :param f: original coverage matrix :param x_exon: numpy array with two columns, the first - start positions of exons comprising the gene, the second - end positions of the exons started in the first column. :param gene: str name of gene whose coverage is being plotted :param chrom: str name of chromosome gene is on :param sample_ids: list of str names of samples corresponding to coverage curves :param save_dir: str path to output directory where gene coverage plot should be saved as save_dir/<chromosome>/<gene>_coverage.png (optional, default None returns plt.Figure) :param kwargs: keyword arguments to pass to matplotlib.pylab.figure (e.g. figsize) :return: matplotlib.figure.Figure if save_dir is not specified, otherwise, a filepath to saved .png file """ # quality control. if ke.shape != f.shape: raise ValueError('ke and f arrays do not have the same shape.') if sample_ids: if not len(sample_ids) == ke.shape[0]: raise ValueError('Number of supplied sample IDs does not match number' 'of samples in gene coverage matrix.') else: sample_ids = ['sample_{0}'.format(i + 1) for i in range(ke.shape[0])] # get union of intersecting exons to prevent confusion when plotting exon junctions. x_exon = get_exon_unions(x_exon) # establish exon positioning on chromosome and junction break points. rel_start = x_exon.min() start = rel_start if x_exon.shape[0] > 1: diffs = x_exon[:, 1] - x_exon[:, 0] rel_end = rel_start + np.sum(diffs) end = x_exon.max() else: rel_end = x_exon.max() end = rel_end # before/after plot consists of 4 subplots, establish them. fig = plt.figure(**kwargs) fig.suptitle('Gene {0} coverage -- chromosome {1}'.format(gene, chrom)) gs = gridspec.GridSpec(2, 2, width_ratios=[1, 1], height_ratios=[20, 1]) with sns.axes_style('darkgrid'): # upper-left subplot: original coverage curves ax1 = plt.subplot(gs[0]) for i in range(f.shape[0]): ax1.plot(f[i, :], label=sample_ids[i]) ax1.set_title('Original') # upper-right subplot: DegNorm-estimated coverage curves ax2 = plt.subplot(gs[1]) for i in range(ke.shape[0]): ax2.plot(ke[i, :], label=sample_ids[i]) ax2.set_title('Normalized') handles, labels = ax2.get_legend_handles_labels() for ax in [ax1, ax2]: ax.margins(x=0) # lower-left subplot: exon positioning ax3 = plt.subplot(gs[2]) ax3.set_xlim(start, end) ax3.add_patch(Rectangle((start, 0) , width=start + (rel_end - rel_start) , height=1, fill=True, facecolor='red', lw=1)) # lower-right subplot: exon positioning ax4 = plt.subplot(gs[3]) ax4.set_xlim(start, end) ax4.add_patch(Rectangle((start, 0) , width=start + (rel_end - rel_start) , height=1, fill=True, facecolor='red', lw=1)) # exon splicing y-axis has no meaning, so remove it. ax3.get_yaxis().set_visible(False) ax4.get_yaxis().set_visible(False) # only include start + end position of gene transcript in exon splicing map. for ax in [ax3, ax4]: ax.get_yaxis().set_visible(False) ax.set_xticks([start, end]) ax.set_xticklabels([str(start), str(end)]) for i in range(x_exon.shape[0] - 1): ax.axvline(x=x_exon[i, 1] , ymin=0 , ymax=1 , color='w' , lw=2) # configure legend: determine if labels expand vertically or horizontally. ncol = len(labels) if len(labels) < 6 else 1 loc = 'upper right' if ncol == 1 else 'lower center' bbox_to_anchor = (1.1, 0.85) if ncol == 1 else None plt.figlegend(handles , labels , loc=loc , title='Sample' , ncol=ncol , bbox_to_anchor=bbox_to_anchor) fig.tight_layout(rect=[0, 0.07, 1, 0.95]) # if save directory specified, save coverage plots to file save_dir/chrom/<gene>_coverage.png and close figure. if not save_dir: return fig else: if not os.path.isdir(os.path.join(save_dir, str(chrom))): os.makedirs(os.path.join(save_dir, str(chrom))) fig_path = os.path.abspath(os.path.join(save_dir, str(chrom), '{0}_coverage.png'.format(gene))) fig.savefig(fig_path , dpi=150 , bbox_inches='tight') plt.close(fig) return fig_path
def pretty_template_plot(template, size=(10.5, 7.5), save=False, savefile=None, title=False, background=False, picks=False): r"""Function to make a pretty plot of a single template, designed to work \ better than the default obspy plotting routine for short data lengths. :type template: :class: obspy.Stream :param template: Template stream to plot :type size: tuple :param size: tuple of plot size :type save: bool :param save: if False will plot to screen, if True will save :type savefile: str :param savefile: String to save plot as, required if save=True. :type title: bool :param title: String if set will be the plot title :type background: :class: obspy.stream :param background: Stream to plot the template within. :type picks: list of obspy.core.event.pick :param picks: List of obspy type picks. """ _check_save_args(save, savefile) fig, axes = plt.subplots(len(template), 1, sharex=True, figsize=size) if len(template) > 1: axes = axes.ravel() if not background: mintime = template.sort(['starttime'])[0].stats.starttime else: mintime = background.sort(['starttime'])[0].stats.starttime template.sort(['network', 'station', 'starttime']) lengths = [] lines = [] labels = [] for i, tr in enumerate(template): # Cope with a singe channel template case. if len(template) > 1: axis = axes[i] else: axis = axes delay = tr.stats.starttime - mintime y = tr.data x = np.linspace(0, (len(y) - 1) * tr.stats.delta, len(y)) x += delay if background: btr = background.select(station=tr.stats.station, channel=tr.stats.channel)[0] bdelay = btr.stats.starttime - mintime by = btr.data bx = np.linspace(0, (len(by) - 1) * btr.stats.delta, len(by)) bx += bdelay axis.plot(bx, by, 'k', linewidth=1) template_line, = axis.plot(x, y, 'r', linewidth=1.1, label='Template') if i == 0: lines.append(template_line) labels.append('Template') lengths.append(max(bx[-1], x[-1])) else: template_line, = axis.plot(x, y, 'k', linewidth=1.1, label='Template') if i == 0: lines.append(template_line) labels.append('Template') lengths.append(x[-1]) # print(' '.join([tr.stats.station, str(len(x)), str(len(y))])) axis.set_ylabel('.'.join([tr.stats.station, tr.stats.channel]), rotation=0, horizontalalignment='right') axis.yaxis.set_ticks([]) # Plot the picks if they are given if picks: tr_picks = [ pick for pick in picks if pick.waveform_id.station_code == tr.stats.station and pick.waveform_id.channel_code[0] + pick.waveform_id.channel_code[-1] == tr.stats.channel[0] + tr.stats.channel[-1] ] for pick in tr_picks: if 'P' in pick.phase_hint.upper(): pcolor = 'red' label = 'P-pick' elif 'S' in pick.phase_hint.upper(): pcolor = 'blue' label = 'S-pick' else: pcolor = 'k' label = 'Unknown pick' pdelay = pick.time - mintime # print(pdelay) line = axis.axvline(x=pdelay, color=pcolor, linewidth=2, linestyle='--', label=label) if label not in labels: lines.append(line) labels.append(label) # axes[i].plot([pdelay, pdelay], []) axis.set_xlim([0, max(lengths)]) if len(template) > 1: axis = axes[len(template) - 1] else: axis = axes axis.set_xlabel('Time (s) from start of template') plt.figlegend(lines, labels, 'upper right') if title: if len(template) > 1: axes[0].set_title(title) else: axes.set_title(title) else: plt.subplots_adjust(top=0.98) plt.tight_layout() plt.subplots_adjust(hspace=0) if not save: plt.show() plt.close() else: plt.savefig(savefile)