def plot_lf_file(opt_lf_file, ir_lf_file, axs=None, plt_kw=None, opt_limit=None, ir_limit=None, agb_mod=None, opt_norms=None, ir_norms=None, norm_lf_kw=None): '''needs work, but: plot the lf files. norm_lf_kw: opt_trgb, ir_trgb, trgb_excludes, opt_limit, ir_limit, opt_mag2, ir_mag2 ''' # set up the plot norm_lf_kw = norm_lf_kw or {} plt_kw = plt_kw or {} plt_kw = dict({'linestyle': 'steps-mid', 'color': 'black', 'alpha': 0.2}.items() + plt_kw.items()) if agb_mod is not None: label = '%s' % agb_mod.split('_')[-1] plt_kw_lab = dict(plt_kw.items() + {'label': label, 'lw': 2}.items()) if 'label' in plt_kw.keys(): plt_kw_lab = dict(plt_kw.items() + {'lw': 2, 'alpha': 1}.items()) del plt_kw['label'] if axs is None: _, (axs) = plt.subplots(ncols=2, figsize=(12, 6)) plt.subplots_adjust(right=0.95, left=0.05, wspace=0.1) opt_hists, opt_binss = load_lf_file(opt_lf_file) ir_hists, ir_binss = load_lf_file(ir_lf_file) if None in [opt_norms, ir_norms]: opt_norms, ir_norms = norm_lf_file(opt_hists, opt_binss, ir_hists, ir_binss, **norm_lf_kw) for i, (hists, binss, limit, norms) in \ enumerate(zip([opt_hists, ir_hists], [opt_binss, ir_binss], [opt_limit, ir_limit], [opt_norms, ir_norms])): for j, (hist, bins, norm) in enumerate(zip(hists, binss, norms)): if j == 0: axs[i].plot(np.zeros(20)-99, np.zeros(20)-99, **plt_kw_lab) if limit is not None: inds, = np.nonzero(bins <= limit) axs[i].plot(bins[inds], hist[inds] * norm, **plt_kw) else: axs[i].plot(bins, hist * norm, **plt_kw) return axs, opt_binss[0], ir_binss[0]
def compare_to_gal(matchphot=None, lf_file=None, limit=None, draw_lines=True, narratio=True, xlim=None, ylim=None, extra_str='', ax=None, plt_kw=None, trgb=None, trgb_exclude=None, filter2=None, narratio_file=None, **kwargs): ''' Plot the LFs and galaxy LF. ARGS: narratio: overlay NRGB, NAGB, and NAGB/NRGB +/- err no_agb: plot the LF without AGB stars RETURNS: ax1, ax2: axes instances created for the plot. ''' target = kwargs.get('target') agb_mod = kwargs.get('agb_mod') nrgb = kwargs.get('nrgbs') nagb = kwargs.get('nagbs') ast_corr = kwargs.get('ast_corr', False) mag_faint = kwargs.get('mag_faint') mag_bright = kwargs.get('mag_bright') mag_limit_val = kwargs.get('mag_limit_val') offset = kwargs.get('offset') fake_file = kwargs.get('fake_file') if ast_corr: extra_str += '_ast' norms, mag2s, _, _, _, _, _, inorm = load_lf_file(lf_file) mag1, mag2 = np.loadtxt(matchphot, unpack=True) bins = np.arange(10, 27, 0.1) # plot lfs from simulations (and initialize figure) plt_kw = plt_kw or {} inorm = None ax = plot_model(mag2s, bins, norms, inorm=inorm, limit=limit, ax=ax, plt_kw=plt_kw, agb_mod=agb_mod) # plot galaxy data ax = plot_gal(mag2, bins, ax=ax, target=target, fake_file=fake_file) # initialize add numbers to the plot if narratio: # count stars from the saved file ratio_data = rsp.fileio.readfile(narratio_file, string_column=0) # get the number ratios for the annotations mean_ratio = {} for key in ratio_data.dtype.names: if key == 'target': continue try: mean_ratio[key] = np.nanmean(ratio_data[:][key]) except ValueError: mean_ratio[key] = np.nanmean(ratio_data[key]) ax.set_yscale('log') if ylim is not None: ax.set_ylim(ylim) else: ax.set_ylim(1, ax.get_ylim()[-1]) if xlim is not None: ax.set_xlim(xlim) else: xmax = ax.get_xlim()[-1] if mag_limit_val is not None: xmax = mag_limit_val ax.set_xlim(np.min([np.min(mag2), np.min(np.concatenate(mag2s))]), xmax) if draw_lines: if mag_bright is not None: low = mag_faint mid = mag_bright else: assert offset is not None, \ 'need either offset or mag limits' low = trgb + offset mid = trgb + trgb_exclude yarr = np.linspace(*ax.get_ylim()) # vertical lines around the trgb exclude region ax.fill_betweenx(yarr, trgb - trgb_exclude, trgb + trgb_exclude, color='black', alpha=0.1) ax.vlines(trgb, *ax.get_ylim(), color='black', linestyle='--') ax.vlines(low, *ax.get_ylim(), color='black', linestyle='--') #ax.fill_betweenx(yarr, low, mid, color='black', alpha=0.1) #if mag_limit_val is not None: # ax.fill_betweenx(yarr, mag_limit_val, ax.get_xlim()[-1], # color='black', alpha=0.5) loc = 4 if not narratio: loc = 0 ax.legend(loc=loc) ax.set_xlabel('${}$'.format(filter2), fontsize=20) if narratio: # need to load the data nrgb and nagb, calculate the ratio # and error. mid_txt = kwargs.get('mid_txt', 'RGB') ax = add_narratio_to_plot(ax, mean_ratio, nrgb, nagb, mid_txt=mid_txt) plt.tick_params(labelsize=16) outfile = '%s%s_lfs.png' % (lf_file.split('_lf')[0], extra_str) plt.savefig(outfile, dpi=150) print 'wrote %s' % outfile return ax