def _process(self, element, key=None): try: from matplotlib.contour import QuadContourSet from matplotlib.axes import Axes from matplotlib.figure import Figure except ImportError: raise ImportError("contours operation requires matplotlib.") extent = element.range(0) + element.range(1)[::-1] if type(element) is Raster: data = [np.flipud(element.data)] elif isinstance(element, Image): data = [np.flipud(element.dimension_values(2, flat=False))] elif isinstance(element, QuadMesh): data = (element.dimension_values(0, False), element.dimension_values(1, False), element.data[2]) if isinstance(self.p.levels, int): levels = self.p.levels + 2 if self.p.filled else self.p.levels + 3 zmin, zmax = element.range(2) levels = np.linspace(zmin, zmax, levels) else: levels = self.p.levels xdim, ydim = element.dimensions('key', label=True) fig = Figure() ax = Axes(fig, [0, 0, 1, 1]) contour_set = QuadContourSet(ax, *data, filled=self.p.filled, extent=extent, levels=levels) if self.p.filled: contour_type = Polygons else: contour_type = Contours vdims = element.vdims[:1] paths = [] empty = np.full((1, 2), np.NaN) for level, cset in zip(contour_set.get_array(), contour_set.collections): subpaths = [] for path in cset.get_paths(): if path.codes is None: subpaths.append(path.vertices) else: subpaths += np.split(path.vertices, np.where(path.codes == 1)[0][1:]) subpath = np.concatenate( [p for sp in subpaths for p in (sp, empty)][:-1]) paths.append({(xdim, ydim): subpath, element.vdims[0].name: level}) contours = contour_type(paths, label=element.label, kdims=element.kdims, vdims=vdims) if self.p.overlaid: contours = element * contours return contours
def compute_and_plot_1(ax, alpha, theta, t7): #Calculate grid values N = 150 kx = np.linspace(-np.pi, np.pi, N + 1) ky = np.linspace(-np.pi, np.pi, N + 1) KX, KY = np.meshgrid(kx, ky) CS1 = QuadContourSet(ax, KY, KX, evals1(alpha, theta, t7), levels=[-4, -3, -2, -1, -0.5, -0.25, 0], cmap=pyl.get_cmap('Reds'), filled=True) # Plot Fermisurface ax.set_xticks([]) ax.set_yticks([]) ax.set_xlabel("$k_ya$", fontsize=8) ax.set_ylabel("$k_xa$", fontsize=8) ax.set_title('Constant energy lines in 1.BZ', fontsize=6) rect = patches.Rectangle((-2.9, -0.5), 1.0, 1.0, linewidth=1, edgecolor='black', facecolor='none') # Add the patch to the Axes ax.add_patch(rect)
def compute_and_plot(ax, alpha): #Calculate grid values V, W = np.meshgrid(v, w) Z = (V**(beta)) * (W**(1 - beta)) X = x_bar + a + b * Z U = alpha * np.log(V) + (1 - alpha) * np.log(X) - c * (W + V) CS = QuadContourSet(ax, V, W, U, 200)
def compute_and_plot(ax, lamda): #Calculate grid values X, Y = np.meshgrid(x, y) E = np.sin(2 * np.pi * np.sqrt(X**2 + (Y - 1)**2) / lamda) + np.sin( 2 * np.pi * np.sqrt(X**2 + (Y + 1)**2) / lamda) CS = QuadContourSet(ax, X, Y, E, 200) ax.set_xlabel("x (m)") ax.set_ylabel("y (m)")
def get_contours(self): from matplotlib.contour import QuadContourSet from matplotlib.axes import Axes from matplotlib.figure import Figure # Use method matplotlib fig = Figure() ax = Axes(fig, [0, 0, 1, 1]) # Get extent if self.dic: extent=(self.x0_ppm, self.x1_ppm, self.y0_ppm, self.y1_ppm) else: extent=None # calculate contour levels cl = self.get_contour_levels() # Get contours contour_set = QuadContourSet(ax, self.data, filled=False, extent=extent, levels=cl) # To plot in bokeh xs = [] ys = [] intensity = [] xt = [] yt = [] col = [] text = [] isolevelid = 0 color_palettes = bp.viridis(len(cl)) for level, isolevel in zip(cl, contour_set.collections): level_round = round(level, 2) #theiso = str(contour_set.get_array()[isolevelid]) theiso = str(level_round) # Get colour #isocol = isolevel.get_color()[0] #thecol = 3 * [None] #for i in range(3): # thecol[i] = int(255 * isocol[i]) #thecol = '#%02x%02x%02x' % (thecol[0], thecol[1], thecol[2]) thecol = color_palettes[isolevelid] for path in isolevel.get_paths(): v = path.vertices x = v[:, 0] y = v[:, 1] xs.append(x.tolist()) ys.append(y.tolist()) intensity.append(level) xt.append(x[int(len(x) / 2)]) yt.append(y[int(len(y) / 2)]) text.append(theiso) col.append(thecol) # Add to counter isolevelid += 1 cdata={'xs': xs, 'ys': ys, 'line_color': col, 'intensity': intensity} ctext={'xt':xt,'yt':yt,'text':text} return cdata, ctext
def compute_and_plot_2(ax, alpha, theta, t7): #Calculate grid values N = 150 kx = np.linspace(-np.pi, np.pi, N + 1) ky = np.linspace(-np.pi, np.pi, N + 1) KX, KY = np.meshgrid(kx, ky) E = evals2(alpha, theta, t7) CS2 = QuadContourSet(ax, KY, KX, E[:, :, 2],levels=[-1, -0.5, -0.25, -0.1, -0.01, 0], cmap=pyl.get_cmap('Reds'))# Plot Fermisurface pyl.clabel(CS2, inline=1, fontsize=5) ax.set_xlabel("$k_ya$", fontsize=8) ax.set_ylabel("$k_xa$", fontsize=8) ax.set_xticks([]) ax.set_yticks([]) ax.set_title('Constant energy lines around K point', fontsize=6)
def _process(self, element, key=None): try: from matplotlib.contour import QuadContourSet from matplotlib.axes import Axes from matplotlib.figure import Figure from matplotlib.dates import num2date, date2num except ImportError: raise ImportError("contours operation requires matplotlib.") extent = element.range(0) + element.range(1)[::-1] xs = element.dimension_values(0, True, flat=False) ys = element.dimension_values(1, True, flat=False) zs = element.dimension_values(2, flat=False) # Ensure that coordinate arrays specify bin centers if xs.shape[0] != zs.shape[0]: xs = xs[:-1] + np.diff(xs, axis=0)/2. if xs.shape[1] != zs.shape[1]: xs = xs[:, :-1] + (np.diff(xs, axis=1)/2.) if ys.shape[0] != zs.shape[0]: ys = ys[:-1] + np.diff(ys, axis=0)/2. if ys.shape[1] != zs.shape[1]: ys = ys[:, :-1] + (np.diff(ys, axis=1)/2.) data = (xs, ys, zs) # if any data is a datetime, transform to matplotlib's numerical format data_is_datetime = tuple(isdatetime(arr) for k, arr in enumerate(data)) if any(data_is_datetime): data = tuple( date2num(d) if is_datetime else d for d, is_datetime in zip(data, data_is_datetime) ) xdim, ydim = element.dimensions('key', label=True) if self.p.filled: contour_type = Polygons else: contour_type = Contours vdims = element.vdims[:1] kwargs = {} levels = self.p.levels zmin, zmax = element.range(2) if isinstance(self.p.levels, int): if zmin == zmax: contours = contour_type([], [xdim, ydim], vdims) return (element * contours) if self.p.overlaid else contours data += (levels,) else: kwargs = {'levels': levels} fig = Figure() ax = Axes(fig, [0, 0, 1, 1]) contour_set = QuadContourSet(ax, *data, filled=self.p.filled, extent=extent, **kwargs) levels = np.array(contour_set.get_array()) crange = levels.min(), levels.max() if self.p.filled: levels = levels[:-1] + np.diff(levels)/2. vdims = [vdims[0].clone(range=crange)] paths = [] empty = np.array([[np.nan, np.nan]]) for level, cset in zip(levels, contour_set.collections): exteriors = [] interiors = [] for geom in cset.get_paths(): interior = [] polys = geom.to_polygons(closed_only=False) for ncp, cp in enumerate(polys): if any(data_is_datetime[0:2]): # transform x/y coordinates back to datetimes xs, ys = np.split(cp, 2, axis=1) if data_is_datetime[0]: xs = np.array(num2date(xs)) if data_is_datetime[1]: ys = np.array(num2date(ys)) cp = np.concatenate((xs, ys), axis=1) if ncp == 0: exteriors.append(cp) exteriors.append(empty) else: interior.append(cp) if len(polys): interiors.append(interior) if not exteriors: continue geom = { element.vdims[0].name: num2date(level) if data_is_datetime[2] else level, (xdim, ydim): np.concatenate(exteriors[:-1]) } if self.p.filled and interiors: geom['holes'] = interiors paths.append(geom) contours = contour_type(paths, label=element.label, kdims=element.kdims, vdims=vdims) if self.p.overlaid: contours = element * contours return contours
def _process(self, element, key=None): try: from matplotlib.contour import QuadContourSet from matplotlib.axes import Axes from matplotlib.figure import Figure except ImportError: raise ImportError("contours operation requires matplotlib.") extent = element.range(0) + element.range(1)[::-1] xs = element.dimension_values(0, True, flat=False) ys = element.dimension_values(1, True, flat=False) zs = element.dimension_values(2, flat=False) # Ensure that coordinate arrays specify bin centers if xs.shape[0] != zs.shape[0]: xs = xs[:-1] + np.diff(xs, axis=0)/2. if xs.shape[1] != zs.shape[1]: xs = xs[:, :-1] + (np.diff(xs, axis=1)/2.) if ys.shape[0] != zs.shape[0]: ys = ys[:-1] + np.diff(ys, axis=0)/2. if ys.shape[1] != zs.shape[1]: ys = ys[:, :-1] + (np.diff(ys, axis=1)/2.) data = (xs, ys, zs) xdim, ydim = element.dimensions('key', label=True) if self.p.filled: contour_type = Polygons else: contour_type = Contours vdims = element.vdims[:1] kwargs = {} levels = self.p.levels zmin, zmax = element.range(2) if isinstance(self.p.levels, int): if zmin == zmax: contours = contour_type([], [xdim, ydim], vdims) return (element * contours) if self.p.overlaid else contours data += (levels,) else: kwargs = {'levels': levels} fig = Figure() ax = Axes(fig, [0, 0, 1, 1]) contour_set = QuadContourSet(ax, *data, filled=self.p.filled, extent=extent, **kwargs) levels = np.array(contour_set.get_array()) crange = levels.min(), levels.max() if self.p.filled: levels = levels[:-1] + np.diff(levels)/2. vdims = [vdims[0].clone(range=crange)] paths = [] empty = np.array([[np.nan, np.nan]]) for level, cset in zip(levels, contour_set.collections): exteriors = [] interiors = [] for geom in cset.get_paths(): interior = [] polys = geom.to_polygons(closed_only=False) for ncp, cp in enumerate(polys): if ncp == 0: exteriors.append(cp) exteriors.append(empty) else: interior.append(cp) if len(polys): interiors.append(interior) if not exteriors: continue geom = {element.vdims[0].name: level, (xdim, ydim): np.concatenate(exteriors[:-1])} if self.p.filled and interiors: geom['holes'] = interiors paths.append(geom) contours = contour_type(paths, label=element.label, kdims=element.kdims, vdims=vdims) if self.p.overlaid: contours = element * contours return contours
from matplotlib.widgets import Slider #Define display parameters mpl.rcParams['xtick.direction'] = 'out' mpl.rcParams['ytick.direction'] = 'out' delta = 0.025 #Define model parameters alpha = .5 beta = .5 x_bar, a, b, c = 2, 0, 1, .1 v = np.arange(0, 10, delta) w = np.arange(0, 10, delta) #Calculate grid values V, W = np.meshgrid(v, w) Z = (V**(beta)) * (W**(1 - beta)) X = x_bar + a + b * Z U = alpha * np.log(V) + (1 - alpha) * np.log(X) - c * (W + V) # Plot fig = pyl.figure() ax = fig.add_subplot(221) CS = QuadContourSet(pyl.gca(), V, W, U, 200) pyl.clabel(CS, inline=1, fontsize=10) pyl.title('Simplest default with labels') pyl.plot() pyl.show(block=True)