def contour_2dfit(x, y, data, fit=None, par=None, nfig=None, interp=True): """ Create a figure with the fit shown as contours :param x: input xaxis coordinates - array :param y: input yaxis coordinates - array (should have the same dim as x) :param data: input data points (should have the same dim as x) :param interp: interpolation used or not (Default is True) :param par: parameters of the fit - Default is None. Only used if interpolation (interp=True) is used :param fit: fitted points (should have the same dim as x) :returns: Nothing """ from pygme.binning.voronoibinning import derive_unbinned_field, guess_regular_grid from matplotlib.mlab import griddata from pygme.mgefunctions import convert_xy_to_polar from pygme.fitting.fitn2dgauss_mpfit import n_centred_twodgaussian_I if nfig is None: fig = plt.gcf() else: fig = plt.figure(nfig) fig.clf() ## If interpolation is requested if interp: if fit is None: print "ERROR: you did not provide 'fit' data" return xu, yu = guess_regular_grid(x, y) du = griddata(x, y, data, xu, yu, interp="nn") ## if par is provided, then we compute the real MGE function if par is None: fu = griddata(x, y, fit, xu, yu, interp="nn") else: r, t = convert_xy_to_polar(xu, yu) fu = n_centred_twodgaussian_I(pars=par)(r, t) fu = fu.reshape(xu.shape) ## Otherwise we just use the nearest neighbour else: xu, yu, du = derive_unbinned_field(x, y, data) xu, yu, fu = derive_unbinned_field(x, y, fit) CS = plt.contour(xu, yu, np.log10(du), colors="k", label="Data") CSfit = plt.contour(xu, yu, np.log10(fu), levels=CS.levels, colors="r", label="MGE Fit") plt.axes().set_aspect("equal") plt.legend()
def show_2dfit_residuals(x, y, data, fit, xfield=None, yfield=None, nfig=None, cmap=None): """ Create a figure with a map of the residuals in % :param x: input xaxis coordinates - array :param y: input yaxis coordinates - array (should have the same dim as x) :param data: input data points (should have the same dim as x) :param fit: fitted points (should have the same dim as x) :param xfield: 2d rectangular array for the field to show (xaxis) :param yfield: 2d rectangular array (yaxis) :param nfig: if None, will use the existing figure, otherwise will use that number :returns: Nothing """ if nfig is None: fig = plt.gcf() else: fig = plt.figure(nfig) fig.clf() ## Making the arrays as 1D x_rav = x.ravel() y_rav = y.ravel() d_rav = data.ravel() f_rav = fit.ravel() lx = len(x_rav) ly = len(y_rav) ld = len(d_rav) lf = len(f_rav) ## checking that the dimensions are correct if (lx != ld) or (lx != lf) or (lx != ly): print "ERROR: dimensions for x, y, data, and fit are not the same" print " (respectively: %d %d %d and %d)" % (lx, ly, ld, lf) return unbinned_residuals = derive_unbinned_field(x, y, 100.0 * (data - fit) / fit, xfield, yfield) Sauron_Cmap = get_SauronCmap() plt.imshow(unbinned_residuals, vmin=-20.0, vmax=20.0, cmap=Sauron_Cmap) plt.colorbar()