def test_c4_zlist(self): """ contours = 4, zlimit = [-4,2] res == [-4,-2,0,2] """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = [-4,2], flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(res == [-4,-2,0,2]))
def test_c4_zm1(self): """ contours = 4, zlimit = -1 res == [-3,-1,1,3] """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = -1, flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(res == [-3,-1,1,3]))
def test_c3_zlist(self): """ contours = 3, zlimit = 2 res == [-2,0,2] """ res = FU.make_contours_2d(self.data, contours = 3, zlimit = [-3,1], flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(res == [-3,-1,1]))
def test_c3_z0(self): """ contours = 3, zlimit = 0 res == [-3,-0.5,2] """ res = FU.make_contours_2d(self.data, contours = 3, zlimit = 0, flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(res == [-3,-0.5,2]))
def test_c4_zlist_reverse(self): """ list from high to low contours = 4, zlimit = [2,-4] res == [2,0,-2,-4] """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = [2,-4], flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(res == [2,0,-2,-4]))
def test_c4_z2(self): """ contours = 4, zlimit = 2.4 res == [-2.4,-0.8,0.8,2.4] using numpy.allclose() for rounding error in third element """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = 2.4, flag_verbose = self.flag_verbose) self.assertTrue(numpy.allclose(res, [-2.4, -0.8 , 0.8 , 2.4]))
def test_c4_z0(self): """ contours = 4, zlimit = 0 res == [-3,-1.33333,0.33333,2] using numpy.allclose() for rounding error """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = 0, flag_verbose = self.flag_verbose) self.assertTrue(numpy.allclose(res, [-3,-1.33333,0.33333,2]))
def test_c4_zm2(self): """ zlimit is negative, zlimit = -2 should give same result as zlimit = 2 contours = 4, zlimit = -2 res == [-3,-1,1,3] """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = -2, flag_verbose = self.flag_verbose) self.assertTrue(numpy.allclose(res, [-2, -0.66666667, 0.66666667,2]))
def contourplot(data, x_axis, y_axis, # ax = False, # x_range = [0,0], # y_range = [0,-1], # zlimit = -1, # contours = 12, # filled = True, # black_contour = True, # x_label = "", # y_label = "", # title = "", # diagonal_line = True, # invert_colors = False, # linewidth = 1, # flag_verbose = False, **kwargs): """ Make a contourplot. The defaults are for a plot with w3 as the x-axis. - data, x_axis, y_axis: data and axes - ax (bool (False) or matplotlib axes instance): if False, it will make a new figure, otherwise it will use the axes instance, allowing subplots etc. - x_label, y_label, title (string, default=''): the labels for the axes. If no label is set, it will use the default. Use 'no_label' or 'no_title' to show no label. - x_range, y_range (array with 2 elements, [0,0], [0,-1]): the range to be plotted. Possible cases: - [min, max]: plot range min to max - [0, 0]: plot the whole range - [0, -1]: use the range from the other axis. If both have this, it will plot both axes complete. (ie. it is identical to both having [0,0]) - zlimit (number or list, -1): the z-range that will be used Possible cases: zlimit = 0, show all, not don't care about centering around zero zlimit = -1, show all, centered around zero zlimit = all else, use that, centered around zero zlimit = [a,b], plot from a to b - contours (int, 16): number of contours to be used - invert_colors (BOOL, False): data = -data CHANGELOG: 201108xx/RB: started function 20130213/RB: moved some things out as separate functions 20160418/RB: replaced arguments with kwargs """ if "flag_verbose" in kwargs: flag_verbose = kwargs["flag_verbose"] else: flag_verbose = False DEBUG.verbose("contour plot", flag_verbose) y, x = numpy.shape(data) if len(x_axis) != x and len(y_axis) != y: DEBUG.printError("The data should have the same shape as the axes, wrong for both axes", inspect.stack()) return False elif len(x_axis) != x: DEBUG.printError("The data should have the same shape as the axes, wrong for the x-axis", inspect.stack()) return False elif len(y_axis) != y: DEBUG.printError("The data should have the same shape as the axes, wrong for the y-axis", inspect.stack()) return False if "invert_colors" in kwargs and kwargs["invert_colors"]: data = -data if "x_range" in kwargs: x_range = kwargs["x_range"] else: x_range = [0,0] if "y_range" in kwargs: y_range = kwargs["y_range"] else: y_range = [0,-1] # determine the range to be plotted x_min, x_max, y_min, y_max = FU.find_axes(x_axis, y_axis, x_range, y_range, flag_verbose) # find the area to be plotted x_min_i, x_max_i = FU.find_axes_indices(x_axis, x_min, x_max) y_min_i, y_max_i = FU.find_axes_indices(y_axis, y_min, y_max) # truncate the data, this speeds up the plotting data, x_axis, y_axis = FU.truncate_data(data, x_axis, y_axis, x_min_i, x_max_i, y_min_i, y_max_i) if "zlimit" in kwargs: zlimit = kwargs["zlimit"] else: zlimit = -1 if "contours" in kwargs: contours = kwargs["contours"] else: contours = 16 # now make the actual contours V = FU.make_contours_2d(data, zlimit, contours, flag_verbose) # make sure there is an axis-object if "ax" in kwargs: ax = kwargs["ax"] flag_show = False else: fig = plt.figure() ax = fig.add_subplot(111) flag_show = True # else: # flag_show = False if "linewidth" in kwargs: linewidth = kwargs["linewidth"] else: linewidth = 1 # actually plot the thing if "filled" in kwargs and kwargs["filled"] == False: pass else: ax.contourf(x_axis, y_axis, data, V, cmap = FU.rwb_cmap) if "black_contour" in kwargs and kwargs["black_contour"] == False: pass else: if "filled" in kwargs and kwargs["filled"] == False: ax.contour(x_axis, y_axis, data, V, linewidths = linewidth, colors = "k") else: ax.contour(x_axis, y_axis, data, V, linewidths = linewidth, linestyles = "solid", colors = "k") # the diagonal line if "diagonal_line" in kwargs and kwargs["diagonal_line"] == False: pass else: ax.plot([x_axis[0]-100,x_axis[-1]+100], [x_axis[0]-100,x_axis[-1]+100], "k", linewidth = linewidth) # we only want to see a certain part of the spectrum ax.set_xlim(x_min, x_max) ax.set_ylim(y_min, y_max) # add some text if "x_label" in kwargs and kwargs["x_label"] != "" and kwargs["x_label"] != "no_label": ax.set_xlabel(kwargs["x_label"] ) if "y_label" in kwargs and kwargs["y_label"] != "" and kwargs["y_label"] != "no_label": ax.set_ylabel(kwargs["y_label"]) if "title" in kwargs and kwargs["title"] != "": ax.set_title(kwargs["title"]) if flag_show: plt.show() return True
def plot_overlap( ma, ax, la = [], x_range = [0,0], y_range = [0,-1], zlimit = -1, contours = 12, colors = ["b", "r"], x_label = "", y_label = "", title = "", diagonal_line = True, invert_colors = False, ma_linewidth = 1, la_linewidth = 2, flag_verbose = False): DEBUG.verbose("contour plot", flag_verbose) for i in range(2): # check for correct lengths y, x = numpy.shape(ma[i].s) if len(ma[i].s_axis[2]) != x and len(ma[i].s_axis[0]) != y: DEBUG.printError("The data should have the same shape as the axes, wrong for both axes", inspect.stack()) return False elif len(ma[i].s_axis[2]) != x: DEBUG.printError("The data should have the same shape as the axes, wrong for the x-axis", inspect.stack()) return False elif len(ma[i].s_axis[0]) != y: DEBUG.printError("The data should have the same shape as the axes, wrong for the y-axis", inspect.stack()) return False # invert colors if invert_colors: ma[i].s = ma[i].s x_axis = ma[0].s_axis[2] y_axis = ma[0].s_axis[0] # determine the range to be plotted x_min, x_max, y_min, y_max = FU.find_axes(x_axis, y_axis, x_range, y_range, flag_verbose) # find the area to be plotted x_min_i, x_max_i= FU.find_axes_indices(x_axis, x_min, x_max) y_min_i, y_max_i= FU.find_axes_indices(y_axis, y_min, y_max) x_axis = x_axis[x_min_i:x_max_i] y_axis = y_axis[y_min_i:y_max_i] for i in range(2): # truncate the data, this speeds up the plotting data = ma[i].s[y_min_i:y_max_i,x_min_i:x_max_i] # now make the actual contours V = FU.make_contours_2d(data, zlimit, contours, flag_verbose) # actually plot the thing ax.contour(x_axis, y_axis, data, V, linewidths = ma_linewidth, colors = colors[i]) if len(la) == 4: ax.plot(la[0],la[1], c = colors[0], lw = la_linewidth) ax.plot(la[2],la[3], c = colors[1], lw = la_linewidth) # the diagonal line if diagonal_line: ax.plot([x_axis[0]-100,x_axis[-1]+100], [x_axis[0]-100,x_axis[-1]+100], "k", linewidth = ma_linewidth) # we only want to see a certain part of the spectrum ax.set_xlim(x_min, x_max) ax.set_ylim(y_min, y_max) # add some text if x_label != "" and x_label != "no_label": ax.set_xlabel(x_label) if y_label != "" and y_label != "no_label": ax.set_ylabel(y_label) if title != "": ax.set_title(title) return True