def visualize_temporal_activities(class_predictions, max_value=200, fps=1, title=None, legend=False): normalize = Normalize(vmin=1, vmax=max_value) normalize.clip=False cmap = plt.cm.Reds cmap.set_under('w') nb_instances = len(class_predictions) plt.figure(num=None, figsize=(18, 1), dpi=100) to_plot = class_predictions.astype(np.float32) to_plot[class_predictions==0.] = np.ma.masked plt.imshow(np.broadcast_to(to_plot, (20, nb_instances)), norm=normalize, interpolation='nearest', aspect='auto', cmap=cmap) if title: plt.title(title) ax = plt.gca() ax.get_yaxis().set_visible(False) if legend: index = np.arange(0,200) colors_index = np.unique(to_plot).astype(np.int64) if 0 in colors_index: colors_index = np.delete(colors_index, 0) patches = [] for c in colors_index: patches.append(mpatches.Patch(color=cmap(normalize(c)), label=dataset.labels[c][1])) if patches: plt.legend(handles=patches, loc='upper center', bbox_to_anchor=(0.5, -.2), ncol=len(patches), fancybox=True, shadow=True) plt.show()
def residual_map_special_deltapsi_add_on( reflections,experiments,matches,hkllist, predicted,plot,eta_deg,deff ): detector = experiments[0].detector crystal = experiments[0].crystal unit_cell = crystal.get_unit_cell() pxlsz = detector[0].get_pixel_size() model_millers = reflections["miller_index"] dpsi = flex.double() for match in matches: obs_miller = hkllist[match["pred"]] model_index= model_millers.first_index(obs_miller) raw_delta_psi = reflections["delpsical.rad"][model_index] deltapsi_envelope = (unit_cell.d(obs_miller)/deff) + math.pi*eta_deg/180. normalized_delta_psi = raw_delta_psi/deltapsi_envelope dpsi.append( normalized_delta_psi ) from matplotlib.colors import Normalize dnorm = Normalize() dnorm.autoscale(dpsi.as_numpy_array()) CMAP = plot.get_cmap("bwr") for match,dcolor in zip(matches,dpsi): #print dcolor, dnorm(dcolor), CMAP(dnorm(dcolor)) #blue represents negative delta psi: outside Ewald sphere; red, positive, inside Ewald sphere plot.plot([predicted[match["pred"]][1]/pxlsz[1]],[-predicted[match["pred"]][0]/pxlsz[0]],color=CMAP(dnorm(dcolor)), marker=".", markersize=5)
def model_to_pc2(model, x_start, y_start, resolution, width, height): """ Creates a PointCloud2 by sampling a regular grid of points from the given model. """ pc = PointCloud2() pc.header.stamp = rospy.get_rostime() pc.header.frame_id = 'map' xy_points = [] for x in map_range(x_start, x_start + width, resolution): for y in map_range(y_start, y_start + height, resolution): xy_points.append([x, y]) probs = model.score_samples(xy_points) # and normalise to range to make the visualisation prettier normaliser = Normalize() normaliser.autoscale(probs) probs = normaliser(probs) colour_map = plt.get_cmap('jet') colours = colour_map(probs, bytes=True) cloud = [] for i in range(len(probs)): cloud.append([xy_points[i][0], xy_points[i][1], 2*probs[i], pack_rgb(colours[i][0], colours[i][1], colours[i][2])]) return create_cloud_xyzrgb(pc.header, cloud)
def __init__(self): Normalize.__init__(self) self.stretch = "linear" self.bias = 0.5 self.contrast = 0.5 self.clip_lo = 5.0 self.clip_hi = 95.0
def __init__(self, stretch='linear', exponent=5, vmid=None, vmin=None, vmax=None, clip=False): ''' Initalize an APLpyNormalize instance. Optional Keyword Arguments: *vmin*: [ None | float ] Minimum pixel value to use for the scaling. *vmax*: [ None | float ] Maximum pixel value to use for the scaling. *stretch*: [ 'linear' | 'log' | 'sqrt' | 'arcsinh' | 'power' ] The stretch function to use (default is 'linear'). *vmid*: [ None | float ] Mid-pixel value used for the log and arcsinh stretches. If set to None, a default value is picked. *exponent*: [ float ] if self.stretch is set to 'power', this is the exponent to use. *clip*: [ True | False ] If clip is True and the given value falls outside the range, the returned value will be 0 or 1, whichever is closer. ''' if vmax < vmin: raise Exception("vmax should be larger than vmin") # Call original initalization routine Normalize.__init__(self, vmin=vmin, vmax=vmax, clip=clip) # Save parameters self.stretch = stretch self.exponent = exponent if stretch == 'power' and np.equal(self.exponent, None): raise Exception("For stretch=='power', an exponent should be specified") if np.equal(vmid, None): if stretch == 'log': if vmin > 0: self.midpoint = vmax / vmin else: raise Exception("When using a log stretch, if vmin < 0, then vmid has to be specified") elif stretch == 'arcsinh': self.midpoint = -1./30. else: self.midpoint = None else: if stretch == 'log': if vmin < vmid: raise Exception("When using a log stretch, vmin should be larger than vmid") self.midpoint = (vmax - vmid) / (vmin - vmid) elif stretch == 'arcsinh': self.midpoint = (vmid - vmin) / (vmax - vmin) else: self.midpoint = None
def __call__(self, value): if self.vmax <= self.vmin: self.vmax, self.vmin = self.vmin, self.vmax result = 1 - Normalize.__call__(self, value) self.vmax, self.vmin = self.vmin, self.vmax else: result = Normalize.__call__(self, value) return result
def __init__(self, vmin=None, vmax=None, midpoint=0, clip=False): """ Copied from SO answer: http://stackoverflow.com/questions/20144529/shifted-colorbar-matplotlib :param vmin: :param vmax: :param midpoint: :param clip: """ self.midpoint = midpoint Normalize.__init__(self, vmin, vmax, clip)
def ImagePlot(image): if str(image.colorscale)=='n': remap = Normalize() remap.autoscale(image.data) ax.imshow(image.data, cmap='gray', norm=remap, origin='lower') elif str(image.colorscale) == 'yg': remap = LogNorm() remap.autoscale(image.data) ax.imshow(image.data, cmap='gray', norm=remap, origin='lower') elif str(image.colorscale) == 'ys': remap = LogNorm() remap.autoscale(image.data) ax.imshow(image.data, cmap='seismic', norm=remap, origin='lower')
def compare_temporal_activities(ground_truth, class_predictions, max_value=200, fps=1, title=None, legend=False, save_file='./img/activity_detection_sample_{}.png'): global count normalize = Normalize(vmin=1, vmax=max_value) normalize.clip=False cmap = plt.cm.Reds cmap.set_under('w') nb_instances = len(class_predictions) to_plot = np.zeros((20, nb_instances)) to_plot[:10,:] = np.broadcast_to(ground_truth, (10, nb_instances)) to_plot[10:,:] = np.broadcast_to(class_predictions, (10, nb_instances)) to_plot = to_plot.astype(np.float32) to_plot[to_plot==0.] = np.ma.masked # Normalize the values and give them the largest distance possible between them unique_values = np.unique(to_plot).astype(np.int64) if 0 in unique_values: unique_values = np.delete(unique_values, 0) nb_different_values = len(unique_values) color_values = np.linspace(40, 190, nb_different_values) for i in range(nb_different_values): to_plot[to_plot == unique_values[i]] = color_values[i] plt.figure(num=None, figsize=(18, 1), dpi=100) plt.imshow(to_plot, norm=normalize, interpolation='nearest', aspect='auto', cmap=cmap) #plt.grid(True) plt.axhline(9, linestyle='-', color='k') plt.xlim([0,nb_instances]) if title: plt.title(title) ax = plt.gca() #ax.get_yaxis().set_visible(False) ax.xaxis.grid(True, which='major') labels=['Ground\nTruth', 'Prediction'] plt.yticks([5,15], labels, rotation="horizontal", size=13) plt.xlabel('Time (s)', horizontalalignment='left', fontsize=13) ax.xaxis.set_label_coords(0, -0.3) if legend: patches = [] for c, l in zip(color_values, unique_values): patches.append(mpatches.Patch(color=cmap(normalize(c)), label=dataset.labels[l][1])) if patches: plt.legend(handles=patches, loc='upper center', bbox_to_anchor=(.5, -.2), ncol=len(patches), fancybox=True, shadow=True) #plt.show() plt.savefig(save_file.format(count), bbox_inches='tight') count += 1
def plot(d, sphere=False): """ Plot directivity `d`. :param d: Directivity :type d: :class:`Directivity` :returns: Figure """ #phi = np.linspace(-np.pi, +np.pi, 50) #theta = np.linspace(0.0, np.pi, 50) phi = np.linspace(0.0, +2.0*np.pi, 50) theta = np.linspace(0.0, np.pi, 50) THETA, PHI = np.meshgrid(theta, phi) # Directivity strength. Real-valued. Can be positive and negative. dr = d.using_spherical(THETA, PHI) if sphere: x, y, z = spherical_to_cartesian(1.0, THETA, PHI) else: x, y, z = spherical_to_cartesian( np.abs(dr), THETA, PHI ) #R, THETA, PHI = cartesian_to_spherical(x, y, z) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') #p = ax.plot_surface(x, y, z, cmap=plt.cm.jet, rstride=1, cstride=1, linewidth=0) norm = Normalize() norm.autoscale(dr) colors = cm.jet(norm(dr)) m = cm.ScalarMappable(cmap=cm.jet, norm=norm) m.set_array(dr) p = ax.plot_surface(x, y, z, facecolors=colors, rstride=1, cstride=1, linewidth=0) plt.colorbar(m, ax=ax) ax.set_xlabel('$x$') ax.set_ylabel('$y$') ax.set_zlabel('$z$') return fig
def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False, nlevs=9): """.. warning:: MidpointNormalize is deprecated and will be removed in future versions. Please use :class:`timutils.get_discrete_midpt_cmap_norm` instead returns a colormap and a matplotlib.colors.Normalize instance that implement a *continuous* colormap with an arbitrary midpoint. ARGS: vmin (real): the minimum value in the colormap vmax (real): the maximum value in the colormap midpoint (real): the midpoint to center on clip (boolean): nlevs (integer): number of levels to divide the colormap into. Not currently functional. EXAMPLE: >>> import matplotlib.pyplot as plt >>> import numpy as np >>> from timutils.midpt_norm import MidpointNormalize >>> plt.close('all') >>> data = np.random.randint(-120, 20, [124, 124]) >>> fix, ax = plt.subplots(1, 2) >>> mycmap = plt.get_cmap('Blues') >>> mynorm = MidpointNormalize(vmin=-120, vmax=20, midpoint=0.0) >>> cm = ax[0].pcolormesh(data, norm=mynorm, cmap=mycmap) >>> plt.colorbar(cm, cax=ax[1], norm=mynorm, cmap=mycmap) >>> plt.show() adapted by Timothy W. Hilton from `code posted by Joe Kington <http://stackoverflow.com/questions/20144529/shifted-colorbar-matplotlib>`_ accessed 19 January 2015 """ warnings.warn(('MidpointNormalize is (1) deprecated and (2)' 'buggy and will be' 'removed in future versions. ' 'Please use get_discrete_midpt_cmap_norm instead')) self.midpoint = midpoint self.nlevs = nlevs Normalize.__init__(self, vmin, vmax, clip)
def auto_scale_cross_plot(self, event): norm = Normalize() for hl in self.h_cross_slice_plot.get_lines(): d = hl.get_ydata() norm.autoscale(d) hl.set_ydata(norm(d)) for vl in self.v_cross_slice_plot.get_lines(): d = vl.get_ydata() norm.autoscale(d) vl.set_ydata(norm(d)) self.v_cross_slice_plot.relim() self.h_cross_slice_plot.relim() self.v_cross_slice_plot.autoscale_view(True,True,True) self.h_cross_slice_plot.autoscale_view(True,True,True) self.cross_slice_canvas.draw()
def plot(self,dano_summation): from matplotlib import pyplot as plt if self.params.use_weights: wt = 1./(self.diffs.sigmas()*self.diffs.sigmas()) order = flex.sort_permutation(wt) wt = wt.select(order) df = self.diffs.data().select(order) dano = dano_summation.select(self.sel0).select(order) from matplotlib.colors import Normalize dnorm = Normalize() dnorm.autoscale(wt.as_numpy_array()) CMAP = plt.get_cmap("rainbow") for ij in xrange(len(self.diffs.data())): #blue represents zero weight: red, large weight plt.plot([df[ij]],[dano[ij]],color=CMAP(dnorm(wt[ij])),marker=".", markersize=4) else: plt.plot(self.diffs.data(),dano_summation.select(self.sel0),"r,") plt.axes().set_aspect("equal") plt.axes().set_xlabel("Observed Dano") plt.axes().set_ylabel("Model Dano") plt.show()
def plot2D(X, filename=None, last_column_color=False): x1 = X[:, 0] x2 = X[:, 1] m = X.shape[0] if last_column_color: c = X[:, -1] c_map = get_cmap('jet') c_norm = Normalize() c_norm.autoscale(c) scalar_map = ScalarMappable(norm=c_norm, cmap=c_map) color_val = scalar_map.to_rgba(c) else: color_val = 'b' * m fig = figure() ax = fig.add_subplot(111) for i in range(m): ax.plot(x1[i], x2[i], 'o', color=color_val[i]) if filename is None: fig.show() else: fig.savefig(filename + ".png") fig.clf() close()
# %% Load Modules modules = pd.read_table("../Embryo3/hotspot/modules_lineage_tree.txt", index_col=0).Cluster Z = pd.read_table("../Embryo3/hotspot/linkage_lineage_tree.txt", header=None).values # %% Plot Modules colors = list(plt.get_cmap("tab10").colors) module_colors = {i: colors[(i - 1) % len(colors)] for i in modules.unique()} module_colors[-1] = '#ffffff' cm = ScalarMappable(norm=Normalize(0, 0.05, clip=True), cmap="viridis") row_colors1 = pd.Series( [module_colors[i] for i in modules], index=z_scores.index, ) row_colors = pd.DataFrame({ "Modules": row_colors1, }) zvals = z_scores.values.ravel() vmax = 8 vmin = -8 cm = sns.clustermap( z_scores,
def __init__(self, vmin=None, vmax=None, clip=False, vin=None, cin=0.01): self.vin = vin self.cin = cin Normalize.__init__(self, vmin, vmax, clip)
def __init__(self,vmin=None,vmax=None,clip=False): Normalize.__init__(self,vmin,vmax,clip)
def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False): self.midpoint = midpoint Normalize.__init__(self, vmin, vmax, clip)
def plot_griddeddata_on_map(data, lons=None, lats=None, var_name=None, unit=None, xlim=(-180, 180), ylim=(-90, 90), vmin=None, vmax=None, add_zero=False, c_under=None, c_over=None, log_scale=True, discrete_norm=True, cbar_levels=None, cbar_ticks=None, add_cbar=True, cmap=None, cbar_ticks_sci=False, color_theme=COLOR_THEME, ax=None, ax_cbar=None, **kwargs): """Make a plot of gridded data onto a map Parameters ---------- data : ndarray 2D data array lons : ndarray longitudes of data lats : ndarray latitudes of data var_name : :obj:`str`, optional name of variable that is plotted xlim : tuple 2-element tuple specifying plotted longitude range ylim : tuple 2-element tuple specifying plotted latitude range vmin : :obj:`float`, optional lower value of colorbar range vmax : :obj:`float`, optional upper value of colorbar range add_zero : bool if True and vmin is not 0, then, the colorbar is extended down to 0. This may be used, e.g. for logarithmic scales that should include 0. c_under : :obj:`float`, optional colour of data values smaller than ``vmin`` c_over : :obj:`float`, optional colour of data values exceeding ``vmax`` log_scale : bool if True, the value to color mapping is done in a pseudo log scale (see :func:`get_cmap_levels_auto` for implementation) discrete_norm : bool if True, color mapping will be subdivided into discrete intervals cbar_levels : iterable, optional discrete colorbar levels. Will be computed automatically, if None (and applicable) cbar_ticks : iterable, optional ticks of colorbar levels. Will be computed automatically, if None (and applicable) Returns ------- fig matplotlib figure instance containing plot result. Use ``fig.axes[0]`` to access the map axes instance (e.g. to modify the title or lon / lat range, etc.) """ if add_cbar: kwargs['contains_cbar'] = True if ax is None: ax = init_map(xlim, ylim, color_theme=color_theme, **kwargs) if not isinstance(ax, GeoAxes): raise AttributeError('Invalid input for ax, need GeoAxes') fig = ax.figure from pyaerocom.griddeddata import GriddedData if isinstance(data, GriddedData): if not data.has_latlon_dims: from pyaerocom.exceptions import DataDimensionError raise DataDimensionError('Input data needs to have latitude and ' 'longitude dimension') if not data.ndim == 2: if not data.ndim == 3 or not 'time' in data.dimcoord_names: raise DataDimensionError('Input data needs to be 2 dimensional ' 'or 3D with time being the 3rd ' 'dimension') data.reorder_dimensions_tseries() data = data[0] lons = data.longitude.points lats = data.latitude.points data = data.grid.data elif not isinstance(data, np.ndarray) or not data.ndim == 2: raise IOError("Need 2D numpy array") elif not isinstance(lats, np.ndarray) or not isinstance(lons, np.ndarray): raise ValueError('Missing lats or lons input') if isinstance(data, np.ma.MaskedArray): sh = data.shape if data.mask.sum() == sh[0] * sh[1]: raise ValueError('All datapoints in input data (masked array) are ' 'invalid') if add_cbar and ax_cbar is None: ax_cbar = _add_cbar_axes(ax)#, where='right') X, Y = meshgrid(lons, lats) bounds = None norm = None if cbar_levels is not None: #user provided levels of colorbar explicitely if vmin is not None or vmax is not None: raise ValueError('Please provide either vmin/vmax OR cbar_levels') bounds = list(cbar_levels) low, high = bounds[0], bounds[-1] if add_zero and low > 0: bounds.insert(0, 0) # insert zero bound if cmap is None: cmap = get_cmap_maps_aerocom(color_theme, low, high) elif isinstance(cmap, str): cmap = plt.get_cmap(cmap) norm = BoundaryNorm(boundaries=bounds, ncolors=cmap.N, clip=False) else: dmin = np.nanmin(data) dmax = np.nanmax(data) if any([np.isnan(x) for x in [dmin, dmax]]): raise ValueError('Cannot plot map of data: all values are NaN') elif dmin == dmax: raise ValueError('Minimum value in data equals maximum value: ' '{}'.format(dmin)) if vmin is None: vmin = dmin else: if vmin < 0 and log_scale: log_scale=False if vmax is None: vmax = dmax if exponent(vmin) == exponent(vmax): log_scale=False if log_scale: # no negative values allowed if vmin < 0: vmin = data[data>0].min() if c_under is None: #special case, set c_under to indicate that there is values below 0 c_under = 'r' if cmap is None: cmap = get_cmap_maps_aerocom(color_theme, vmin, vmax) elif isinstance(cmap, str): cmap = plt.get_cmap(cmap) if discrete_norm: #to compute upper range of colour range, round up vmax exp = float(exponent(vmax) - 1) vmax_colors = ceil(vmax / 10**exp)*10**exp bounds = calc_pseudolog_cmaplevels(vmin=vmin, vmax=vmax_colors, add_zero=add_zero) norm = BoundaryNorm(boundaries=bounds, ncolors=cmap.N, clip=False) else: if not vmin > 0: raise ValueError('Logscale can only be applied for vmin>0') norm = LogNorm(vmin=vmin, vmax=vmax, clip=True) else: if add_zero and vmin > 0: vmin = 0 if cmap is None: cmap = get_cmap_maps_aerocom(color_theme, vmin, vmax) elif isinstance(cmap, str): cmap = plt.get_cmap(cmap) if discrete_norm: bounds = np.linspace(vmin, vmax, 10) norm = BoundaryNorm(boundaries=bounds, ncolors=cmap.N, clip=False) else: norm = Normalize(vmin=vmin, vmax=vmax) cbar_extend = "neither" if c_under is not None: cmap.set_under(c_under) cbar_extend = "min" if bounds is not None: bounds.insert(0, bounds[0] - bounds[1]) if c_over is not None: cmap.set_over(c_over) if bounds is not None: bounds.append(bounds[-1] + bounds[-2]) if cbar_extend == "min": cbar_extend = "both" else: cbar_extend = "max" fig.norm = norm disp = ax.pcolormesh(X, Y, data, cmap=cmap, norm=norm) # ============================================================================= # fmt = None # if bounds is not None: # print(bounds) # min_mag = -exponent(bounds[1]) # min_mag = 0 if min_mag < 0 else min_mag # print(min_mag) # #fmt = "%." + str(min_mag) + "f" # ============================================================================= if add_cbar: cbar = fig.colorbar(disp, cmap=cmap, norm=norm, #boundaries=bounds, extend=cbar_extend, cax=ax_cbar, shrink=0.8) fig.cbar = cbar if var_name is not None: var_str = var_name# + VARS.unit_str if unit is not None: if not str(unit) in ['1', 'no_unit']: var_str += ' [{}]'.format(unit) cbar.set_label(var_str) if cbar_ticks: cbar.set_ticks(cbar_ticks) if cbar_ticks_sci: lbls = [] for lbl in cbar.ax.get_yticklabels(): tstr = lbl.get_text() if bool(tstr): lbls.append('{:.1e}'.format(float(tstr))) else: lbls.append('') cbar.ax.set_yticklabels(lbls) return fig
def __init__(self, epoch0, epoch1): Normalize.__init__(self, self.mktime(epoch0), self.mktime(epoch1))
def show_nourishment_area(visit_freq, grid=None, walk_data=None, cmap='Reds', sigma=0.7, min_alpha=0, show_seed=True, seed_color='dodgerblue'): """Plot a smoothed, normalized heatmap of particle visit frequency. Function will plot the history of particle travel locations in walk_data as a heatmap overtop the specified grid, using the output of :obj:`dorado.particle_track.nourishment_area()`. Colors indicate number of instances in which that cell was occupied by a particle. **Inputs** : visit_freq : `numpy.ndarray` A 2-D grid of normalized particle visit frequencies, i.e. the output of the :obj:`dorado.particle_track.nourishment_area()` function grid : `numpy.ndarray`, optional An optional 2-D grid upon which the particles will be plotted. Examples of grids that might be nice to use are `dorado.particle_track.modelParams.depth`, `dorado.particle_track.modelParams.stage`, `dorado.particle_track.modelParams.topography`. walk_data : `dict`, optional The dictionary with the particle information, which is used to show the seed location. This is the output from one of the other routines or the :obj:`dorado.particle_track.Particles.run_iteration()` function. cmap : `str`, optional Name of Matplotlib colormap used for the foreground (heatmap). Default is 'Reds' sigma : `float`, optional Degree of spatial smoothing of the heatmap used in the dorado.particle_track.nourishment_area() function, only used to adjust the plot asthetics for low sigma's min_alpha : `float`, optional Minimum alpha value of the heatmap, representing the least frequented cell locations, default is full transparency show_seed : `bool`, optional Determines whether resulting plot shows a marker indicating the (first) seed location. Uses indices of walk_data[:][0][0]. Default is True, but only if 'walk_data' is also provided. seed_color : `str`, optional Name of matplotlib color used for the marker seed, if shown. Default is a light blue to contrast with the 'Reds' heatmap. **Outputs** : ax : `matplotlib.axes` A `matplotlib.axes` upon which the nourishment area is drawn """ from matplotlib.colors import Normalize # Plot heatmap with alpha based on visit_freq if sigma >= 0.125: # This is just a visual trial-and-error thing amax = np.nanpercentile(visit_freq, 60) else: amax = np.nanpercentile(visit_freq, 30) alphas = Normalize(0, amax, clip=True)(visit_freq) # Normalize alphas alphas = np.clip(alphas, min_alpha, 1) colors = Normalize(np.nanmin(visit_freq), 1)(visit_freq) # Normalize color cmap = plt.cm.get_cmap(cmap) colors = cmap(colors) colors[..., -1] = alphas # Plot figure if len(plt.get_fignums()) < 1: # Create new figure axes if none are open fig, ax = plt.subplots(1, 1, figsize=(5, 5), dpi=300) else: ax = plt.gca() # Otherwise grab existing ax.set_facecolor('k') # Set facecolor black if grid is not None: # Grid background intentionally dark: im = ax.imshow(grid, cmap='gist_gray', vmax=np.max(grid) * 3) # Show nourishment area nr = ax.imshow(colors) plt.title('Nourishment Area') if (show_seed) & (walk_data is not None): ax.scatter(walk_data['yinds'][0][0], walk_data['xinds'][0][0], c=seed_color, edgecolors='black', s=10, linewidths=0.5) return ax
def add_cont(self, var, target=None, n=24, maxz=False, lines=True, cmap=False, add_cbar=False, label=None, loc=111, xticksize=12, yticksize=12, **kwargs): ''' Create a polar contour of variable *var*. Plot will be either drawn on a new matplotlib figure and axes, or you can specify a plot target using the *target* kwarg. Parameters ========== var : str The name of the variable to plot. Returns ======= fig : matplotlib figure object ax : matplotlib axes object cnt : matplotlib contour object cb : matplotlib colorbar object Other Parameters ================ target : Figure or Axes If None (default), a new figure is generated from scratch. If a matplotlib Figure object, a new axis is created to fill that figure. If a matplotlib Axes object, the plot is placed into that axis. loc : int Use to specify the subplot placement of the axis (e.g. loc=212, etc.) Used if target is a Figure or None. Default 111 (single plot). n : int Set number of levels. Should be a multiple of 3 for best match between filled and traced contours. Default is 21. lines : bool Add unfilled black solid/dashed contours to plot for additional contrast. Default is **True**. maxz : real Set the max/min value for the color bar. Default is set by data. cmap : str Set the colormap. Default is to autoselect using classic IE maps. Can be 'bwr', 'wr', or any name of a matplotlib colar map. add_cbar : bool Add colorbar to plot. Default is **False** which will not add one to the plot. Extra keywords are passed to the contourf routine. ''' # Get only what we need to decrease runtime. from math import pi from numpy import linspace from matplotlib.colors import Normalize from matplotlib.ticker import MaxNLocator, MultipleLocator from matplotlib.pyplot import clabel, colorbar fig, ax = set_target(target, polar=True, loc=loc) hemi = var[:2] # Set levels and ticks: if label == None: label = tex_label(var) lt_labels = ['06', label, '18', '00'] xticks = [0, pi / 2, pi, 3 * pi / 2] lct = MultipleLocator(10) minz = self[var].min() if minz < 0.0: if not maxz: maxz = max([abs(minz), self[var].max()]) crange = Normalize(vmin=-1. * maxz, vmax=maxz) levs = linspace(-1. * maxz, maxz, n) else: if not maxz: maxz = self[var].max() crange = Normalize(vmin=0., vmax=maxz) levs = linspace(0., maxz, n) # Get color map if not given: if not cmap: if self[var].min() >= 0.0: cmap = get_iono_cb('wr') else: cmap = get_iono_cb('bwr') cnt1 = ax.contourf(self[hemi + 'psi'] * pi / 180.0 + pi / 2., self[hemi + 'theta'], np.array(self[var]), levs, norm=crange, cmap=cmap) # Set xtick label size, increase font of top label. labels = ax.get_xticklabels() for l in labels: l.set_size(xticksize) labels[1].set_size(xticksize * 1.25) if lines: nk = int(round(n / 3.0)) cnt2 = ax.contour(self[hemi + 'psi'] * pi / 180.0 + pi / 2., self[hemi + 'theta'], np.array(self[var]), nk, colors='k') #clabel(cnt2,fmt='%3i',fontsize=10) if add_cbar: cbarticks = MaxNLocator(7) cbar = colorbar(cnt1, ticks=cbarticks, shrink=0.75, pad=0.08) cbar.set_label(tex_label(self[var].attrs['units'])) else: cbar = False ax.set_xticks(xticks) ax.set_xticklabels(lt_labels) ax.yaxis.set_major_locator(lct) ax.set_ylim([0, 40]) # Use text function to manually add pretty ticks. ax.set_yticklabels('') # old ticks off. opts = { 'size': yticksize, 'rotation': -45, 'ha': 'center', 'va': 'center' } for theta in [80., 70., 60.]: txt = '{:02.0f}'.format(theta) + r'$^{\circ}$' ax.text(pi / 4., 90. - theta, txt, color='w', weight='heavy', **opts) ax.text(pi / 4., 90. - theta, txt, color='k', weight='light', **opts) return fig, ax, cnt1, cbar
def _plot_storm(self, storm_table, ax=None, kernel=None, bw_method=0.05, upscale=2, alpha_cutoff=None, **kwargs): raise DeprecationWarning("") x, y = storm_table['x'], storm_table['y'] if self.cell_obj.data.shape: xmax = self.cell_obj.data.shape[1] ymax = self.cell_obj.data.shape[0] else: xmax = int(storm_table['x'].max()) ymax = int(storm_table['y'].max()) x_bins = np.linspace(0, xmax, num=xmax * upscale, endpoint=True) y_bins = np.linspace(0, ymax, num=ymax * upscale, endpoint=True) h, xedges, yedges = np.histogram2d(x, y, bins=[x_bins, y_bins]) ax = plt.gca() if ax is None else ax if not kernel: cm = plt.cm.get_cmap('Blues') cmap = cm if not 'cmap' in kwargs else kwargs.pop('cmap') img = h.T ax.imshow(img, interpolation='nearest', cmap=cmap, extent=[0, xmax, ymax, 0], **kwargs) else: # https://jakevdp.github.io/PythonDataScienceHandbook/05.13-kernel-density-estimation.html # todo check the mgrid describes the coords correctly X, Y = np.mgrid[0:xmax:xmax * upscale * 1j, ymax:0:ymax * upscale * 1j] positions = np.vstack([X.ravel(), Y.ravel()]) values = np.vstack([x, y]) k = stats.gaussian_kde(values, bw_method=bw_method) Z = np.reshape(k(positions).T, X.shape) img = np.rot90(Z) img_norm = img / img.max() alphas = np.ones(img.shape) if alpha_cutoff: alphas[img_norm < 0.3] = img_norm[img_norm < 0.3] / 0.3 cmap = sns.light_palette( "green", as_cmap=True) if not 'cmap' in kwargs else plt.cm.get_cmap( kwargs.pop('cmap')) normed = Normalize()(img) colors = cmap(normed) colors[..., -1] = alphas ax.imshow(colors, cmap=cmap, extent=[0, xmax, ymax, 0], interpolation='nearest', **kwargs)
############################################################################### # Blending in transparency # ======================== # # The simplest way to include transparency when plotting data with # :func:`matplotlib.pyplot.imshow` is to convert the 2-D data array to a # 3-D image array of rgba values. This can be done with # :class:`matplotlib.colors.Normalize`. For example, we'll create a gradient # moving from left to right below. # Create an alpha channel of linearly increasing values moving to the right. alphas = np.ones(weights.shape) alphas[:, 30:] = np.linspace(1, 0, 70) # Normalize the colors b/w 0 and 1, we'll then pass an MxNx4 array to imshow colors = Normalize(vmin, vmax, clip=True)(weights) colors = cmap(colors) # Now set the alpha channel to the one we created above colors[..., -1] = alphas # Create the figure and image # Note that the absolute values may be slightly different fig, ax = plt.subplots() ax.imshow(greys) ax.imshow(colors, extent=(xmin, xmax, ymin, ymax)) ax.set_axis_off() ############################################################################### # Using transparency to highlight values with high amplitude # ==========================================================
def draw_networkx_multi_edges(G, pos, edgelist=None, width=1.0, edge_color='k', style='solid', alpha=1.0, arrowstyle='-|>', arrowsize=10, edge_cmap=None, edge_vmin=None, edge_vmax=None, ax=None, arrows=True, label=None, node_size=300, nodelist=None, node_shape="o", rad=None, **kwds): """Draw the edges of the graph G. This draws only the edges of the graph G. Parameters ---------- G : graph A networkx graph pos : dictionary A dictionary with nodes as keys and positions as values. Positions should be sequences of length 2. edgelist : collection of edge tuples Draw only specified edges(default=G.edges()) width : float, or array of floats Line width of edges (default=1.0) edge_color : color string, or array of floats Edge color. Can be a single color format string (default='r'), or a sequence of colors with the same length as edgelist. If numeric values are specified they will be mapped to colors using the edge_cmap and edge_vmin,edge_vmax parameters. style : string Edge line style (default='solid') (solid|dashed|dotted,dashdot) alpha : float The edge transparency (default=1.0) edge_ cmap : Matplotlib colormap Colormap for mapping intensities of edges (default=None) edge_vmin,edge_vmax : floats Minimum and maximum for edge colormap scaling (default=None) ax : Matplotlib Axes object, optional Draw the graph in the specified Matplotlib axes. arrows : bool, optional (default=True) For directed graphs, if True draw arrowheads. Note: Arrows will be the same color as edges. arrowstyle : str, optional (default='-|>') For directed graphs, choose the style of the arrow heads. See :py:class: `matplotlib.patches.ArrowStyle` for more options. arrowsize : int, optional (default=10) For directed graphs, choose the size of the arrow head head's length and width. See :py:class: `matplotlib.patches.FancyArrowPatch` for attribute `mutation_scale` for more info. label : [None| string] Label for legend rad: courbure de la liaison Returns ------- matplotlib.collection.LineCollection `LineCollection` of the edges list of matplotlib.patches.FancyArrowPatch `FancyArrowPatch` instances of the directed edges Depending whether the drawing includes arrows or not. Notes ----- For directed graphs, arrows are drawn at the head end. Arrows can be turned off with keyword arrows=False. Be sure to include `node_size' as a keyword argument; arrows are drawn considering the size of nodes. Examples -------- >>> G = nx.dodecahedral_graph() >>> edges = nx.draw_networkx_edges(G, pos=nx.spring_layout(G)) >>> G = nx.DiGraph() >>> G.add_edges_from([(1, 2), (1, 3), (2, 3)]) >>> arcs = nx.draw_networkx_edges(G, pos=nx.spring_layout(G)) >>> alphas = [0.3, 0.4, 0.5] >>> for i, arc in enumerate(arcs): # change alpha values of arcs ... arc.set_alpha(alphas[i]) Also see the NetworkX drawing examples at https://networkx.github.io/documentation/latest/auto_examples/index.html See Also -------- draw() draw_networkx() draw_networkx_nodes() draw_networkx_labels() draw_networkx_edge_labels() """ try: import matplotlib import matplotlib.pyplot as plt import matplotlib.cbook as cb from matplotlib.colors import colorConverter, Colormap, Normalize from matplotlib.collections import LineCollection from matplotlib.patches2 import FancyArrowPatch import numpy as np except ImportError: raise ImportError("Matplotlib required for draw()") except RuntimeError: print("Matplotlib unable to open display") raise if ax is None: ax = plt.gca() if edgelist is None: edgelist = list(G.edges()) if not edgelist or len(edgelist) == 0: # no edges! return None if nodelist is None: nodelist = list(G.nodes()) # set edge positions edge_pos = np.asarray([(pos[e[0]], pos[e[1]]) for e in edgelist]) if not cb.iterable(width): lw = (width, ) else: lw = width if not is_string_like(edge_color) \ and cb.iterable(edge_color) \ and len(edge_color) == len(edge_pos): if np.alltrue([is_string_like(c) for c in edge_color]): # (should check ALL elements) # list of color letters such as ['k','r','k',...] edge_colors = tuple( [colorConverter.to_rgba(c, alpha) for c in edge_color]) elif np.alltrue([not is_string_like(c) for c in edge_color]): # If color specs are given as (rgb) or (rgba) tuples, we're OK if np.alltrue( [cb.iterable(c) and len(c) in (3, 4) for c in edge_color]): edge_colors = tuple(edge_color) else: # numbers (which are going to be mapped with a colormap) edge_colors = None else: raise ValueError('edge_color must contain color names or numbers') else: if is_string_like(edge_color) or len(edge_color) == 1: edge_colors = (colorConverter.to_rgba(edge_color, alpha), ) else: msg = 'edge_color must be a color or list of one color per edge' raise ValueError(msg) if (not G.is_directed() or not arrows): edge_collection = LineCollection( edge_pos, #ici colors=edge_colors, rotation=100, linewidths=lw, antialiaseds=(1, ), linestyle=style, transOffset=ax.transData, ) edge_collection.set_zorder(1) # edges go behind nodes edge_collection.set_label(label) ax.add_collection(edge_collection) # Note: there was a bug in mpl regarding the handling of alpha values # for each line in a LineCollection. It was fixed in matplotlib by # r7184 and r7189 (June 6 2009). We should then not set the alpha # value globally, since the user can instead provide per-edge alphas # now. Only set it globally if provided as a scalar. if cb.is_numlike(alpha): edge_collection.set_alpha(alpha) if edge_colors is None: if edge_cmap is not None: assert (isinstance(edge_cmap, Colormap)) edge_collection.set_array(np.asarray(edge_color)) edge_collection.set_cmap(edge_cmap) if edge_vmin is not None or edge_vmax is not None: edge_collection.set_clim(edge_vmin, edge_vmax) else: edge_collection.autoscale() return edge_collection arrow_collection = None if G.is_directed() and arrows: # Note: Waiting for someone to implement arrow to intersection with # marker. Meanwhile, this works well for polygons with more than 4 # sides and circle. def to_marker_edge(marker_size, marker): if marker in "s^>v<d": # `large` markers need extra space return np.sqrt(2 * marker_size) / 2 else: return np.sqrt(marker_size) / 2 # Draw arrows with `matplotlib.patches.FancyarrowPatch` arrow_collection = [] mutation_scale = arrowsize # scale factor of arrow head arrow_colors = edge_colors if arrow_colors is None: if edge_cmap is not None: assert (isinstance(edge_cmap, Colormap)) else: edge_cmap = plt.get_cmap() # default matplotlib colormap if edge_vmin is None: edge_vmin = min(edge_color) if edge_vmax is None: edge_vmax = max(edge_color) color_normal = Normalize(vmin=edge_vmin, vmax=edge_vmax) for i, (src, dst) in enumerate(edge_pos): x1, y1 = src x2, y2 = dst arrow_color = None line_width = None shrink_source = 0 # space from source to tail shrink_target = 0 # space from head to target if cb.iterable(node_size): # many node sizes src_node, dst_node = edgelist[i] index_node = nodelist.index(dst_node) marker_size = node_size[index_node] shrink_target = to_marker_edge(marker_size, node_shape) else: shrink_target = to_marker_edge(node_size, node_shape) if arrow_colors is None: arrow_color = edge_cmap(color_normal(edge_color[i])) elif len(arrow_colors) > 1: arrow_color = arrow_colors[i] else: arrow_color = arrow_colors[0] if len(lw) > 1: line_width = lw[i] else: line_width = lw[0] arrow = FancyArrowPatch((x1, y1), (x2, y2), arrowstyle=arrowstyle, shrinkA=shrink_source, shrinkB=shrink_target, mutation_scale=mutation_scale, color=arrow_color, linewidth=line_width, zorder=1, rad=rad) # arrows go behind nodes # There seems to be a bug in matplotlib to make collections of # FancyArrowPatch instances. Until fixed, the patches are added # individually to the axes instance. arrow_collection.append(arrow) ax.add_patch(arrow) # update view minx = np.amin(np.ravel(edge_pos[:, :, 0])) maxx = np.amax(np.ravel(edge_pos[:, :, 0])) miny = np.amin(np.ravel(edge_pos[:, :, 1])) maxy = np.amax(np.ravel(edge_pos[:, :, 1])) w = maxx - minx h = maxy - miny padx, pady = 0.05 * w, 0.05 * h corners = (minx - padx, miny - pady), (maxx + padx, maxy + pady) ax.update_datalim(corners) ax.autoscale_view() return arrow_collection
def plot_agency_magnitude_density(data, overlay=False, number_samples=0, xlim=[], ylim=[], figure_size=(7, 8), lognorm=True, filetype="png", resolution=300, filename=None): """ """ keys = data.keys() if not data: print "No pairs found - abandoning plot!" return if len(xlim) == 2: lowx = xlim[0] highx = xlim[1] else: lowx = np.floor(np.min(data[keys[0]])) highx = np.ceil(np.max(data[keys[0]])) if len(ylim) == 2: lowy = ylim[0] highy = ylim[1] else: lowy = np.floor(np.min(data[keys[2]])) highy = np.ceil(np.max(data[keys[2]])) if lowy < lowx: lowx = lowy if highy > highx: highx = highy xbins = np.linspace(lowx - 0.05, highx + 0.05, ((highx + 0.05 - lowx - 0.05) / 0.1) + 2.0) ybins = np.linspace(lowx - 0.05, highx + 0.05, ((highx + 0.05 - lowx - 0.05) / 0.1) + 2.0) density = sample_agency_magnitude_pairs(data, xbins, ybins, number_samples) fig = plt.figure(figsize=figure_size) if lognorm: cmap = deepcopy(matplotlib.cm.get_cmap("jet")) data_norm = LogNorm(vmin=0.1, vmax=np.max(density)) else: cmap = deepcopy(matplotlib.cm.get_cmap("jet")) cmap.set_under("w") data_norm = Normalize(vmin=0.1, vmax=np.max(density)) #density[density < 1E-15] == np.nan plt.pcolormesh(xbins[:-1] + 0.05, ybins[:-1] + 0.05, density.T, norm=data_norm, cmap=cmap) cbar = plt.colorbar() cbar.set_label("Number Events", fontsize=16) plt.xlabel(utils._to_latex(keys[0]), fontsize=16) plt.ylabel(utils._to_latex(keys[2]), fontsize=16) plt.grid(True) plt.ylim(lowx, highx) plt.xlim(lowx, highx) # Overlay 1:1 line plt.plot(np.array([lowx, highx]), np.array([lowx, highx]), ls="--", color=[0.5, 0.5, 0.5], zorder=1) plt.tight_layout() if filename: utils._save_image(filename, filetype, resolution) if not overlay: plt.show() return data
# In[25]: # TEST Training, validation and test RMSE (2c) Test.assertTrue( np.allclose([rmseTrainBase, rmseValBase, rmseTestBase], [21.305869, 21.586452, 22.136957]), 'incorrect RMSE value') # #### ** Visualization 3: Predicted vs. actual ** # #### We will visualize predictions on the validation dataset. The scatter plots below visualize tuples storing i) the predicted value and ii) true label. The first scatter plot represents the ideal situation where the predicted value exactly equals the true label, while the second plot uses the baseline predictor (i.e., `averageTrainYear`) for all predicted values. Further note that the points in the scatter plots are color-coded, ranging from light yellow when the true and predicted values are equal to bright red when they drastically differ. # In[26]: from matplotlib.colors import ListedColormap, Normalize from matplotlib.cm import get_cmap cmap = get_cmap('YlOrRd') norm = Normalize() actual = np.asarray(parsedValData.map(lambda lp: lp.label).collect()) error = np.asarray( parsedValData.map(lambda lp: (lp.label, lp.label)).map( lambda (l, p): squaredError(l, p)).collect()) clrs = cmap(np.asarray(norm(error)))[:, 0:3] fig, ax = preparePlot(np.arange(0, 100, 20), np.arange(0, 100, 20)) plt.scatter(actual, actual, s=14**2, c=clrs, edgecolors='#888888', alpha=0.75, linewidths=0.5)
def show_nourishment_time(mean_times, grid=None, walk_data=None, cmap='magma', show_colorbar=True, min_alpha=0.3, show_seed=True, seed_color='dodgerblue'): """Plot a smoothed heatmap of mean particle visit time. Function will plot the history of mean particle travel times in walk_data as a heatmap overtop the specified grid, using the output of :obj:`dorado.particle_track.nourishment_time()`. Colors indicate the mean length of time particles spent in each cell (potentially after smoothing) **Inputs** : mean_times : `numpy.ndarray` Array of mean occupation times in each cell, i.e. the output of the :obj:`dorado.particle_track.nourishment_time()` function grid : `numpy.ndarray`, optional An optional 2-D grid upon which the particles will be plotted. Recommended to use `dorado.particle_track.modelParams.topography` walk_data : `dict`, optional The dictionary with the particle information, which is used to show the seed location. This is the output from one of the other routines or the :obj:`dorado.particle_track.Particles.run_iteration()` function. cmap : `str`, optional Name of Matplotlib colormap used for the foreground (heatmap). Default is 'magma' show_colorbar : `bool`, optional Controls whether to plot a colorbar for mean_times, default is False min_alpha : `float`, optional Minimum alpha value of the heatmap, representing the cells which spent the least amount of time occupied, default is 0.3 show_seed : `bool`, optional Determines whether resulting plot shows a marker indicating the (first) seed location. Uses indices of walk_data[:][0][0]. Default is True, but only if 'walk_data' is also provided. seed_color : `str`, optional Name of matplotlib color used for the marker seed, if shown. Default is a light blue. **Outputs** : ax : `matplotlib.axes` A `matplotlib.axes` upon which the nourishment times are drawn """ from matplotlib.colors import Normalize from mpl_toolkits.axes_grid1 import make_axes_locatable # Plot heatmap with alpha based on times amax = np.nanpercentile(mean_times, 20) alphas = Normalize(0, amax, clip=True)(mean_times) # Normalize alphas alphas = np.clip(alphas, min_alpha, 1) colors = Normalize(np.nanmin(mean_times), np.nanmax(mean_times))(mean_times) # Normalize colors cmap = plt.cm.get_cmap(cmap) colors = cmap(colors) colors[..., -1] = alphas # Plot figure if len(plt.get_fignums()) < 1: # Create new figure axes if none are open fig, ax = plt.subplots(1, 1, figsize=(5, 5), dpi=300) else: ax = plt.gca() # Otherwise grab existing ax.set_facecolor('k') # Set facecolor black if grid is not None: # Grid background intentionally dark: im = ax.imshow(grid, cmap='gist_gray', vmax=np.max(grid) * 3) # Show nourishment times nt = ax.imshow(colors) plt.title('Nourishment Times') # Optionally plot colorbar if show_colorbar: # Requires a few extra steps due to how we made the heatmap norm = matplotlib.colors.Normalize(vmin=np.nanmin(mean_times), vmax=np.nanmax(mean_times)) sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) sm.set_array([]) divider = make_axes_locatable(ax) # Make the right size cax = divider.append_axes("right", size="5%", pad=0.05) cbar = plt.colorbar(sm, cax=cax) cbar.set_label('Mean Nourishment Time [s]') # Optionally show seed location if (show_seed) & (walk_data is not None): ax.scatter(walk_data['yinds'][0][0], walk_data['xinds'][0][0], c=seed_color, edgecolors='black', s=10, linewidths=0.5) return ax
def __call__(self, *args): v = Normalize.__call__(self, *args) return v*v
def plot_storm(self, ax=None, data_name='', method='plot', upscale=5, alpha_cutoff=None, storm_weight=False, sigma=0.25, **kwargs): #todo make functions with table and shape and other kwargs? """ Graphically represent STORM data. Parameters --------- ax : :class:`~matplotlib.axes.Axes` Optional matplotlib axes to use for plotting. data_name : :obj:`str` Name of the data element to plot. Must be of data class 'storm'. method : :obj:`str` Method of visualization. Options are 'plot', 'hist', or 'gauss' just plotting points, histogram plot or gaussian kernel plot. upscale : :obj:`int` Upscale factor for the output image. Number of pixels is increased w.r.t. data.shape with a factor upscale**2 alpha_cutoff : :obj:`float` Values (normalized) below `alpha_cutoff` are transparent, where the alpha is linearly scaled between 0 and `alpha_cutoff` storm_weight : :obj:`bool` If `True` the STORM data points are weighted by their intensity. sigma : :obj:`float` or :obj:`string` or :class:`~numpy.ndarray` Only applies for method 'gauss'. The value is the sigma which describes the gaussian kernel. If `sigma` is a scalar, the same sigma value is used for all data points. If `sigma` is a string it is interpreted as the name of the field in the STORM array to use. Otherwise, sigma can be an array with equal length to the number of datapoints. **kwargs Additional kwargs passed to ax.plot() or ax.imshow() Returns ------- artist :class:`~matplotlib.image.AxesImage` or :class:`~matplotlib.lines.Line2D` Matplotlib artist object. """ #todo alpha cutoff docstirng and adjustment / testing if not data_name: #todo update via CellListData data_name = list(self.cell_list[0].data.storm_dict.keys())[0] storm_table = self.cell_list[0].data.data_dict[data_name] assert storm_table.dclass == 'storm' x, y = storm_table['x'], storm_table['y'] if self.cell_list.data.shape is not None: xmax = self.cell_list.data.shape[1] ymax = self.cell_list.data.shape[0] else: #todo change to global x, y max and not local xmax = int(storm_table['x'].max()) ymax = int(storm_table['y'].max()) extent = kwargs.pop('extent', [0, xmax, ymax, 0]) interpolation = kwargs.pop('interpolation', 'nearest') ax = plt.gca() if ax is None else ax if method == 'plot': color = kwargs.pop('color', 'r') marker = kwargs.pop('marker', '.') linestyle = kwargs.pop('linestyle', 'None') x, y = zip(*[(cell.data.data_dict[data_name]['x'], cell.data.data_dict[data_name]['y']) for cell in self.cell_list]) artist, = ax.iter_plot(x, y, color=color, marker=marker, linestyle=linestyle, **kwargs) elif method == 'hist': x_bins = np.linspace(0, xmax, num=xmax * upscale, endpoint=True) y_bins = np.linspace(0, ymax, num=ymax * upscale, endpoint=True) img = np.empty( (len(self.cell_list), ymax * upscale - 1, xmax * upscale - 1)) print(img.shape) for i, cell in enumerate(self.cell_list): storm_table = cell.data.data_dict[data_name] x, y = storm_table['x'], storm_table['y'] h, xedges, yedges = np.histogram2d(x, y, bins=[x_bins, y_bins]) img[i] = h.T cm = plt.cm.get_cmap('Blues') cmap = cm if not 'cmap' in kwargs else kwargs.pop('cmap') artist = ax.iter_imshow(img, interpolation=interpolation, cmap=cmap, extent=extent, **kwargs) elif method == 'gauss': step = 1 / upscale xi = np.arange(step / 2, xmax, step) yi = np.arange(step / 2, ymax, step) x_coords = np.repeat(xi, len(yi)).reshape(len(xi), len(yi)).T y_coords = np.repeat(yi, len(xi)).reshape(len(yi), len(xi)) cmap = kwargs.pop('cmap', 'viridis') cmap = plt.cm.get_cmap(cmap) if type(cmap) == str else cmap colors_stack = np.empty((len(self.cell_list), *x_coords.shape, 4)) for i, cell in enumerate(self.cell_list): storm_table = cell.data.data_dict[data_name] x, y = storm_table['x'], storm_table['y'] if type(sigma) == str: sigma_local = storm_table[sigma] elif isinstance(sigma, np.ndarray): assert sigma.shape == x.shape sigma_local = sigma elif np.isscalar(sigma): sigma_local = sigma * np.ones_like(x) else: raise ValueError('Invalid sigma') try: intensities = storm_table[ 'intensity'] if storm_weight else np.ones_like(x) except ValueError: intensities = np.ones_like(x) # Make empty image and iteratively add gaussians for each localization #img = np.zeros_like(x_coords) img = render_storm(x_coords, y_coords, sigma_local, intensities, x, y) # @jit(nopython=True) # for _sigma, _int, _x, _y in zip(sigma_local, intensities, x, y): # img += _int * np.exp(-(((_x - x_coords) / _sigma) ** 2 + ((_y - y_coords) / _sigma) ** 2) / 2) img_norm = img / img.max() alphas = np.ones(img.shape) if alpha_cutoff: alphas[img_norm < alpha_cutoff] = img_norm[ img_norm < alpha_cutoff] / alpha_cutoff normed = Normalize()(img) colors = cmap(normed) colors[..., -1] = alphas colors_stack[i] = colors artist = ax.iter_imshow(colors_stack, cmap=cmap, extent=extent, interpolation=interpolation, **kwargs) else: raise ValueError('Invalid plotting method') return artist
def plot_one_model(self,nrow,out): fig = plt.subplot(self.gs[nrow*self.ncols]) two_thetas = self.reduction.get_two_theta_deg() degrees = self.reduction.get_delta_psi_deg() if self.color_encoding=="conventional": positive = (self.reduction.i_sigi>=0.) fig.plot(two_thetas.select(positive), degrees.select(positive), "bo") fig.plot(two_thetas.select(~positive), degrees.select(~positive), "r+") elif self.color_encoding=="I/sigma": positive = (self.reduction.i_sigi>=0.) tt_selected = two_thetas.select(positive) dp_selected = degrees.select(positive) i_sigi_select = self.reduction.i_sigi.select(positive) order = flex.sort_permutation(i_sigi_select) tt_selected = tt_selected.select(order) dp_selected = dp_selected.select(order) i_sigi_selected = i_sigi_select.select(order) from matplotlib.colors import Normalize dnorm = Normalize() dcolors = i_sigi_selected.as_numpy_array() dnorm.autoscale(dcolors) N = len(dcolors) CMAP = plt.get_cmap("rainbow") if self.refined.get("partiality_array",None) is None: for n in xrange(N): fig.plot([tt_selected[n]],[dp_selected[n]], color=CMAP(dnorm(dcolors[n])),marker=".", markersize=10) else: partials = self.refined.get("partiality_array") partials_select = partials.select(positive) partials_selected = partials_select.select(order) assert len(partials)==len(positive) for n in xrange(N): fig.plot([tt_selected[n]],[dp_selected[n]], color=CMAP(dnorm(dcolors[n])),marker=".", markersize=20*partials_selected[n]) # change the markersize to indicate partiality. negative = (self.reduction.i_sigi<0.) fig.plot(two_thetas.select(negative), degrees.select(negative), "r+", linewidth=1) else: strong = (self.reduction.i_sigi>=10.) positive = ((~strong) & (self.reduction.i_sigi>=0.)) negative = (self.reduction.i_sigi<0.) assert (strong.count(True)+positive.count(True)+negative.count(True) == len(self.reduction.i_sigi)) fig.plot(two_thetas.select(positive), degrees.select(positive), "bo") fig.plot(two_thetas.select(strong), degrees.select(strong), marker='.',linestyle='None', markerfacecolor='#00ee00', markersize=10) fig.plot(two_thetas.select(negative), degrees.select(negative), "r+") # indicate the imposed resolution filter wavelength = self.reduction.experiment.beam.get_wavelength() imposed_res_filter = self.reduction.get_imposed_res_filter(out) resolution_markers = [ a for a in [imposed_res_filter,self.reduction.measurements.d_min()] if a is not None] for RM in resolution_markers: two_th = (180./math.pi)*2.*math.asin(wavelength/(2.*RM)) plt.plot([two_th, two_th],[self.AD1TF7B_MAXDP*-0.8,self.AD1TF7B_MAXDP*0.8],'k-') plt.text(two_th,self.AD1TF7B_MAXDP*-0.9,"%4.2f"%RM) #indicate the linefit mean = flex.mean(degrees) minplot = flex.min(two_thetas) plt.plot([0,minplot],[mean,mean],"k-") LR = flex.linear_regression(two_thetas, degrees) model_y = LR.slope()*two_thetas + LR.y_intercept() plt.plot(two_thetas, model_y, "k-") #Now let's take care of the red and green lines. half_mosaic_rotation_deg = self.refined["half_mosaic_rotation_deg"] mosaic_domain_size_ang = self.refined["mosaic_domain_size_ang"] red_curve_domain_size_ang = self.refined.get("red_curve_domain_size_ang",mosaic_domain_size_ang) a_step = self.AD1TF7B_MAX2T / 50. a_range = flex.double([a_step*x for x in xrange(1,50)]) # domain two-theta array #Bragg law [d=L/2sinTH] d_spacing = (wavelength/(2.*flex.sin(math.pi*a_range/360.))) # convert two_theta to a delta-psi. Formula for Deffective [Dpsi=d/2Deff] inner_phi_deg = flex.asin((d_spacing / (2.*red_curve_domain_size_ang)) )*(180./math.pi) outer_phi_deg = flex.asin((d_spacing / (2.*mosaic_domain_size_ang)) + \ half_mosaic_rotation_deg*math.pi/180. )*(180./math.pi) plt.title("ML: mosaicity FW=%4.2f deg, Dsize=%5.0fA on %d spots\n%s"%( 2.*half_mosaic_rotation_deg, mosaic_domain_size_ang, len(two_thetas), os.path.basename(self.reduction.filename))) plt.plot(a_range, inner_phi_deg, "r-") plt.plot(a_range,-inner_phi_deg, "r-") plt.plot(a_range, outer_phi_deg, "g-") plt.plot(a_range, -outer_phi_deg, "g-") plt.xlim([0,self.AD1TF7B_MAX2T]) plt.ylim([-self.AD1TF7B_MAXDP,self.AD1TF7B_MAXDP]) #second plot shows histogram fig = plt.subplot(self.gs[1+nrow*self.ncols]) plt.xlim([-self.AD1TF7B_MAXDP,self.AD1TF7B_MAXDP]) nbins = 50 n,bins,patches = plt.hist(dp_selected, nbins, range=(-self.AD1TF7B_MAXDP,self.AD1TF7B_MAXDP), weights=self.reduction.i_sigi.select(positive), normed=0, facecolor="orange", alpha=0.75) #ersatz determine the median i_sigi point: isi_positive = self.reduction.i_sigi.select(positive) isi_order = flex.sort_permutation(isi_positive) reordered = isi_positive.select(isi_order) isi_median = reordered[int(len(isi_positive)*0.9)] isi_top_half_selection = (isi_positive>isi_median) n,bins,patches = plt.hist(dp_selected.select(isi_top_half_selection), nbins, range=(-self.AD1TF7B_MAXDP,self.AD1TF7B_MAXDP), weights=isi_positive.select(isi_top_half_selection), normed=0, facecolor="#ff0000", alpha=0.75) plt.xlabel("(degrees)") plt.title("Weighted histogram of Delta-psi")
def updateFigure(self): self.figure.clear() if (self.imageData is None) and \ (self.pixmapImage is None): return # The axes self.axes = self.figure.add_axes([.15, .15, .75, .8]) if self.config['xaxis'] == 'off': self.axes.xaxis.set_visible(False) else: self.axes.xaxis.set_visible(True) nLabels = self.config['nxlabels'] if nLabels not in ['Auto', 'auto', '0', 0]: self.axes.xaxis.set_major_locator(MaxNLocator(nLabels)) else: self.axes.xaxis.set_major_locator(AutoLocator()) if self.config['yaxis'] == 'off': self.axes.yaxis.set_visible(False) else: self.axes.yaxis.set_visible(True) nLabels = self.config['nylabels'] if nLabels not in ['Auto', 'auto', '0', 0]: self.axes.yaxis.set_major_locator(MaxNLocator(nLabels)) else: self.axes.yaxis.set_major_locator(AutoLocator()) if self.pixmapImage is not None: self._updatePixmapFigure() return interpolation = self.config['interpolation'] origin = self.config['origin'] cmap = self.__temperatureCmap ccmap = cm.gray if self.config['colormap'] in ['grey', 'gray']: cmap = cm.gray ccmap = self.__temperatureCmap elif self.config['colormap'] in ['yarg', 'yerg']: cmap = self.__reversedGrayCmap ccmap = self.__temperatureCmap elif self.config['colormap'] == 'jet': cmap = cm.jet elif self.config['colormap'] == 'hot': cmap = cm.hot elif self.config['colormap'] == 'cool': cmap = cm.cool elif self.config['colormap'] == 'copper': cmap = cm.copper elif self.config['colormap'] == 'spectral': cmap = cm.spectral elif self.config['colormap'] == 'hsv': cmap = cm.hsv elif self.config['colormap'] == 'rainbow': cmap = cm.gist_rainbow elif self.config['colormap'] == 'red': cmap = self.__redCmap elif self.config['colormap'] == 'green': cmap = self.__greenCmap elif self.config['colormap'] == 'blue': cmap = self.__blueCmap elif self.config['colormap'] == 'temperature': cmap = self.__temperatureCmap elif self.config['colormap'] == 'paired': cmap = cm.Paired elif self.config['colormap'] == 'paired_r': cmap = cm.Paired_r elif self.config['colormap'] == 'pubu': cmap = cm.PuBu elif self.config['colormap'] == 'pubu_r': cmap = cm.PuBu_r elif self.config['colormap'] == 'rdbu': cmap = cm.RdBu elif self.config['colormap'] == 'rdbu_r': cmap = cm.RdBu_r elif self.config['colormap'] == 'gist_earth': cmap = cm.gist_earth elif self.config['colormap'] == 'gist_earth_r': cmap = cm.gist_earth_r elif self.config['colormap'] == 'blues': cmap = cm.Blues elif self.config['colormap'] == 'blues_r': cmap = cm.Blues_r elif self.config['colormap'] == 'ylgnbu': cmap = cm.YlGnBu elif self.config['colormap'] == 'ylgnbu_r': cmap = cm.YlGnBu_r else: print("Unsupported colormap %s" % self.config['colormap']) if self.config['extent'] is None: h, w = self.imageData.shape x0 = self.config['xorigin'] y0 = self.config['yorigin'] w = w * self.config['xpixelsize'] h = h * self.config['ypixelsize'] if origin == 'upper': extent = (x0, w + x0, h + y0, y0) else: extent = (x0, w + x0, y0, h + y0) else: extent = self.config['extent'] vlimits = self.__getValueLimits() if vlimits is None: imageData = self.imageData vmin = self.imageData.min() vmax = self.imageData.max() else: vmin = min(vlimits[0], vlimits[1]) vmax = max(vlimits[0], vlimits[1]) imageData = self.imageData.clip(vmin, vmax) if self.config['linlogcolormap'] != 'linear': if vmin <= 0: if vmax > 0: vmin = min(imageData[imageData > 0]) else: vmin = 0.0 vmax = 1.0 self._image = self.axes.imshow(imageData.clip(vmin, vmax), interpolation=interpolation, origin=origin, cmap=cmap, extent=extent, norm=LogNorm(vmin, vmax)) else: self._image = self.axes.imshow(imageData, interpolation=interpolation, origin=origin, cmap=cmap, extent=extent, norm=Normalize(vmin, vmax)) ylim = self.axes.get_ylim() if self.config['colorbar'] is not None: barorientation = self.config['colorbar'] if barorientation == "vertical": xlim = self.axes.get_xlim() deltaX = abs(xlim[1] - xlim[0]) deltaY = abs(ylim[1] - ylim[0]) ratio = deltaY / float(deltaX) shrink = ratio self._colorbar = self.figure.colorbar( self._image, fraction=0.046, pad=0.04, #shrink=shrink, aspect=20 * shrink, orientation=barorientation) if ratio < 0.51: nTicks = 5 if ratio < 0.2: nTicks = 3 try: tick_locator = MaxNLocator(nTicks) self._colorbar.locator = tick_locator self._colorbar.update_ticks() except: print("Colorbar error", sys.exc_info()) pass else: self._colorbar = self.figure.colorbar( self._image, orientation=barorientation) #contour plot if self.config['contour'] != 'off': dataMin = imageData.min() dataMax = imageData.max() ncontours = int(self.config['contourlevels']) levels = (numpy.arange(ncontours)) *\ (dataMax - dataMin)/float(ncontours) contourlinewidth = int(self.config['contourlinewidth']) / 10. if self.config['contour'] == 'filled': self._contour = self.axes.contourf(imageData, levels, origin=origin, cmap=ccmap, extent=extent) else: self._contour = self.axes.contour(imageData, levels, origin=origin, cmap=ccmap, linewidths=contourlinewidth, extent=extent) if self.config['contourlabels'] != 'off': self.axes.clabel(self._contour, fontsize=9, inline=1, fmt=self.config['contourlabelformat']) if 0 and self.config['colorbar'] is not None: if barorientation == 'horizontal': barorientation = 'vertical' else: barorientation = 'horizontal' self._ccolorbar = self.figure.colorbar( self._contour, orientation=barorientation, extend='both') self.__postImage(ylim)
def __init__(self,linthresh,vmin=None,vmax=None,clip=False): Normalize.__init__(self,vmin,vmax,clip) self.linthresh=linthresh self.vmin, self.vmax = vmin, vmax
y_data.append(value) x_data = np.array(x_data) xprime_data = np.array(xprime_data) y_data = np.array(y_data) # fig = ff.create_quiver(x_data[:, 0], x_data[:, 1], xprime_data[:, 0] - x_data[:, 0], xprime_data[:, 1] - x_data[:, 1], scale=1) # fig.show() x = x_data[:, 0] y = x_data[:, 1] u = xprime_data[:, 0] - x_data[:, 0] v = xprime_data[:, 1] - x_data[:, 1] colors = y_data norm = Normalize(vmax=1.0, vmin=-1.0) norm.autoscale(colors) # we need to normalize our colors array to match it colormap domain # which is [0, 1] colormap = cm.bwr plt.figure(figsize=(18, 16), dpi=80) line_x = np.linspace(-30, 30, 1000) plt.plot(line_x, np.array([0] * 1000)) plt.plot(np.array([0] * 1000), np.linspace(-10, 40, 1000)) plt.quiver(x, y, u, v, angles='xy', color=colormap(colors), scale_units='xy', scale=1, pivot='mid') # colormap(norm(colors)) plt.xlim([-30, 30]) plt.ylim([-10, 40]) plt.show() # %%
def continuous_scatter( data, ax=None, coord_base="umap", x=None, y=None, scatter_kws=None, hue=None, hue_norm=None, hue_portion=0.95, cmap="viridis", colorbar=True, colorbar_label_kws=None, size=None, size_norm=None, size_portion=0.95, sizes=None, sizebar=True, text_anno=None, dodge_text=False, dodge_kws=None, text_anno_kws=None, text_anno_palette=None, text_transform=None, axis_format="tiny", max_points=50000, s='auto', labelsize=4, linewidth=0.5, cax=None, zoomxy=1.05, outline=None, outline_kws=None, outline_pad=2, return_fig=False, rasterized='auto', ): # init figure if not provided if ax is None: fig, ax = plt.subplots(figsize=(4, 4), dpi=300) else: fig = None # add coords _data, x, y = _extract_coords(data, coord_base, x, y) # _data has 2 cols: "x" and "y" # down sample plot data if needed. if max_points is not None: if _data.shape[0] > max_points: _data = _density_based_sample(_data, seed=1, size=max_points, coords=["x", "y"]) n_dots = _data.shape[0] # determine rasterized if rasterized == 'auto': if n_dots > 200: rasterized = True else: rasterized = False # auto size if user didn't provide one if s is 'auto': s = _auto_size(ax, n_dots) # default scatter options _scatter_kws = { "linewidth": 0, "s": s, "legend": None, 'rasterized': rasterized } if scatter_kws is not None: _scatter_kws.update(scatter_kws) # deal with color if hue is not None: if isinstance(hue, str): _data["hue"] = _take_data_series(data, hue).astype(float) colorbar_label = hue else: _data["hue"] = hue.astype(float) colorbar_label = hue.name hue = "hue" if hue_norm is None: # get the smallest range that include "hue_portion" of data hue_norm = tight_hue_range(_data["hue"], hue_portion) # cnorm is the normalizer for color cnorm = Normalize(vmin=hue_norm[0], vmax=hue_norm[1]) if isinstance(cmap, str): # from here, cmap become colormap object cmap = copy.copy(get_cmap(cmap)) cmap.set_bad(color=(0.5, 0.5, 0.5, 0.5)) else: if not isinstance(cmap, ScalarMappable): raise TypeError( f"cmap can only be str or ScalarMappable, got {type(cmap)}" ) else: hue_norm = None cnorm = None colorbar_label = "" # deal with size if size is not None: if isinstance(size, str): _data["size"] = _take_data_series(data, size).astype(float) else: _data["size"] = size.astype(float) size = "size" if size_norm is None: # get the smallest range that include "size_portion" of data size_norm = tight_hue_range(_data["size"], size_portion) # snorm is the normalizer for size size_norm = Normalize(vmin=size_norm[0], vmax=size_norm[1]) # replace s with sizes s = _scatter_kws.pop("s") if sizes is None: sizes = (min(s, 1), s) else: size_norm = None sizes = None sns.scatterplot( x="x", y="y", data=_data, hue=hue, palette=cmap, hue_norm=cnorm, size=size, sizes=sizes, size_norm=size_norm, ax=ax, **_scatter_kws, ) if text_anno is not None: if isinstance(text_anno, str): _data["text_anno"] = _take_data_series(data, text_anno) else: _data["text_anno"] = text_anno _text_anno_scatter( data=_data[["x", "y", "text_anno"]], ax=ax, x="x", y="y", dodge_text=dodge_text, dodge_kws=dodge_kws, palette=text_anno_palette, text_transform=text_transform, anno_col="text_anno", text_anno_kws=text_anno_kws, labelsize=labelsize, ) # deal with outline if outline: if isinstance(outline, str): _data["outline"] = _take_data_series(data, outline) else: _data["outline"] = outline _outline_kws = { "linewidth": linewidth, "palette": None, "c": "lightgray", "single_contour_pad": outline_pad, } if outline_kws is not None: _outline_kws.update(outline_kws) density_contour(ax=ax, data=_data, x="x", y="y", groupby="outline", **_outline_kws) # clean axis if axis_format == "tiny": _make_tiny_axis_label(ax, x, y, arrow_kws=None, fontsize=labelsize) elif (axis_format == "empty") or (axis_format is None): sns.despine(ax=ax, left=True, bottom=True) ax.set(xticks=[], yticks=[], xlabel=None, ylabel=None) else: pass return_axes = [ax] # make color bar if colorbar and (hue is not None): _colorbar_label_kws = dict(fontsize=labelsize, label=hue, labelpad=10, rotation=270) if colorbar_label_kws is not None: _colorbar_label_kws.update(colorbar_label_kws) # small ax for colorbar if cax is None: cax = inset_axes(ax, width="3%", height="25%", loc="lower right", borderpad=0) cax = plot_colorbar( cax=cax, cmap=cmap, cnorm=cnorm, hue_norm=hue_norm, label=colorbar_label, orientation="vertical", labelsize=labelsize, linewidth=0.5, ) return_axes.append(cax) # make size bar if sizebar and (size is not None): # TODO plot dot size bar pass if zoomxy is not None: zoom_ax(ax, zoomxy) if return_fig: return (fig, tuple(return_axes)), _data else: return
def plot_mse_loss_surface_3d(ax, x, y, v=0.0, l2=0.0, w1_range=(-2, 2), w2_range=(2, -2), angle=30): # create weight space n_w = 100 w1 = np.linspace(w1_range[0], w1_range[1], num=n_w) # weight 1 w2 = np.linspace(w2_range[0], w2_range[1], num=n_w) # weight 2 ws_x, ws_y = np.meshgrid(w1, w2) cost_ws = np.zeros((n_w, n_w)) # initialize cost matrix # Fill the cost matrix for each combination of weights for i in range(n_w): for j in range(n_w): y_pred = ws_x[i, j] * ws_y[i, j] * x y_true = y cost_ws[i, j] = 0.5 * (y_true - y_pred)**2 + \ 0.5 * l2 * (ws_x[i, j]**2 + ws_y[i, j]**2) + 0.5 * v * (ws_x[i, j]*ws_y[i, j])**2 X = ws_x Y = ws_y Z = cost_ws #fig, ax = plt.subplots(figsize=(8, 8)) #ax = fig.add_subplot(1,1,1, projection='3d') # fourth dimention - colormap # create colormap according to x-value (can use any 50x50 array) color_dimension = Z # change to desired fourth dimension minn, maxx = color_dimension.min(), color_dimension.max() norm = Normalize(minn, maxx) m = plt.cm.ScalarMappable(norm=norm, cmap='jet') m.set_array([]) fcolors = m.to_rgba(color_dimension) # plot # fig = plt.figure(figsize=(8, 8)) # ax = fig.gca(projection='3d') ax.scatter(0, 0, 1, c='red', marker='*', label='Saddle point') ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False, alpha=0.1) ax.set_xlabel('$w_1$', fontsize=15) ax.set_ylabel('$w_2$', fontsize=15) ax.set_zlabel('$Loss$', fontsize=15) settings = (x, y, v, l2, w1_range, w2_range) ax.view_init(angle, 10) return ax, settings
def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False): """Initialise utility function.""" self.midpoint = midpoint Normalize.__init__(self, vmin, vmax, clip)
# axs[1, 1].scatter(x_hist_bases_peaks, y_hist_bases_peaks, c=spread_colors) axs[1, 1].scatter(x_spread, y_spread, c=spread_colors) axs[1, 1].set_yticks([]) axs[1, 1].set_xticks(x_spread) axs[1, 1].set_xlabel('History Coefficient Index') axs[2, 1].plot(control_hist, c=hist_color) axs[2, 1].set_ylim(-15, 5) axs[2, 1].set_xlabel('Time (ms)') axs[2, 1].set_title('History Filter') '''####################### plots of example slopes with lambda ################''' stim_cmap = cm.Blues stim_vmin = 1 stim_vmax = 50 stim_norm = Normalize(stim_vmin, stim_vmax) ymin = -10 ymax = 25 x_minor = MultipleLocator(0.5) gs_kw = dict(height_ratios=[1, 0.3]) fig, axs = plt.subplots(figsize=(9, 3), nrows=2, ncols=3, gridspec_kw=gs_kw, constrained_layout=True) axs[0, 0].plot(slopes_0.stim_weights) axs[0, 1].plot(slopes_opt.stim_weights) axs[0, 2].plot(slopes_max.stim_weights) axs[0, 0].set_ylabel('Coefficient Values') axs[0, 0].set_title('\u03BB = 0')
def plt_slices(*fields, size=64, title=None, cmap=None, norm=None): """Plot slices of fields of more than 2 spatial dimensions. Each field should have a channel dimension followed by spatial dimensions, i.e. no batch dimension. """ plt.close('all') assert all(isinstance(field, torch.Tensor) for field in fields) fields = [field.detach().cpu().numpy() for field in fields] nc = max(field.shape[0] for field in fields) nf = len(fields) if title is not None: assert len(title) == nf cmap = np.broadcast_to(cmap, (nf, )) norm = np.broadcast_to(norm, (nf, )) im_size = 2 cbar_height = 0.2 fig, axes = plt.subplots( nc + 1, nf, squeeze=False, figsize=(nf * im_size, nc * im_size + cbar_height), dpi=100, gridspec_kw={'height_ratios': nc * [im_size] + [cbar_height]}) for f, (field, cmap_col, norm_col) in enumerate(zip(fields, cmap, norm)): all_non_neg = (field >= 0).all() all_non_pos = (field <= 0).all() if cmap_col is None: if all_non_neg: cmap_col = 'viridis' elif all_non_pos: warnings.warn('no implementation for all non-positive values') cmap_col = None else: cmap_col = 'RdBu_r' if norm_col is None: l2, l1, h1, h2 = np.percentile(field, [2.5, 16, 84, 97.5]) w1, w2 = (h1 - l1) / 2, (h2 - l2) / 2 if all_non_neg: if h1 > 0.1 * h2: norm_col = Normalize(vmin=0, vmax=quantize(h2)) else: norm_col = LogNorm(vmin=quantize(l2), vmax=quantize(h2)) elif all_non_pos: warnings.warn( 'no implementation for all non-positive values yet') norm_col = None else: vlim = quantize(max(-l2, h2)) if w1 > 0.1 * w2 or l1 * h1 >= 0: norm_col = Normalize(vmin=-vlim, vmax=vlim) else: linthresh = 0.1 * quantize(min(-l1, h1)) norm_col = SymLogNorm(linthresh=linthresh, vmin=-vlim, vmax=vlim) for c in range(field.shape[0]): s = (c, ) + tuple(d // 2 for d in field.shape[1:-2]) if size is None: s += (slice(None), ) * 2 else: s += ( slice( (field.shape[-2] - size) // 2, (field.shape[-2] + size) // 2, ), slice( (field.shape[-1] - size) // 2, (field.shape[-1] + size) // 2, ), ) axes[c, f].pcolormesh(field[s], cmap=cmap_col, norm=norm_col) axes[c, f].set_aspect('equal') axes[c, f].set_xticks([]) axes[c, f].set_yticks([]) if c == 0 and title is not None: axes[c, f].set_title(title[f]) for c in range(field.shape[0], nc): axes[c, f].axis('off') fig.colorbar( ScalarMappable(norm=norm_col, cmap=cmap_col), cax=axes[-1, f], orientation='horizontal', ) fig.tight_layout() return fig
def plot_nmb_map_colocateddata(coldata, in_percent=True, vmin=-100, vmax=100, cmap='bwr', s=80, marker='o', step_bounds=None, add_cbar=True, norm=None, cbar_extend='both', add_mean_edgecolor=True, ax=None, ax_cbar=None, cbar_outline_visible=False, cbar_orientation='vertical', ref_label=None, data_label=None, stats_area_weighted=False, **kwargs): """Plot map of normalised mean bias from instance of ColocatedData Note ---- THIS IS A BETA FEATURE AND WILL BE GENERALISED IN THE FUTURE FOR OTHER STATISTICAL PARAMETERS Parameters ---------- coldata : ColocatedData data object in_percent : bool plot bias in percent vmin : int minimum value of colormapping vmax : int maximum value of colormapping cmap : str or cmap colormap used, defaults to bwr s : int size of marker marker : str marker used step_bounds : int, optional step used for discrete colormapping (if None, continuous is used) cbar_extend : str extend colorbar ax : GeoAxes, optional axes into which the bias is supposed to be plotted ax_cbar : plt.Axes, optional axes for colorbar cbar_outline_visible : bool if False, borders of colorbar are removed **kwargs keyword args passed to :func:`init_map` Returns ------- GeoAxes """ try: mec = kwargs.pop('mec') except KeyError: try: mec = kwargs.pop('markeredgecolor') except KeyError: mec = 'face' try: mew = kwargs.pop('mew') except KeyError: mew = 1 #_arr = coldata.data mean_bias = coldata.calc_nmb_array() if mean_bias.ndim == 1: (lats, lons, data) = mean_bias.latitude, mean_bias.longitude, mean_bias.data elif 'latitude' in mean_bias.dims and 'longitude' in mean_bias.dims: stacked = mean_bias.stack(latlon=['latitude', 'longitude']) valid = ~stacked.isnull()#.all(dim='time') coords = stacked.latlon[valid].values lats, lons = list(zip(*list(coords))) data = stacked.data[valid] else: raise NotImplementedError('Dimension error...') if ref_label is None: ref_label = coldata.meta['data_source'][0] if data_label is None: data_label = coldata.meta['data_source'][1] if in_percent: data *= 100 if ax is None: ax = init_map(contains_cbar=True, **kwargs) if not isinstance(ax, GeoAxes): raise TypeError('Input axes need to be instance of cartopy.GeoAxes') fig = ax.figure if isinstance(cmap, str): cmap = plt.get_cmap(cmap) if norm is None and step_bounds is not None: bounds = np.arange(vmin, vmax+step_bounds, step_bounds) norm = BoundaryNorm(boundaries=bounds, ncolors=cmap.N, clip=False) if add_mean_edgecolor: nn = Normalize(vmin=vmin, vmax=vmax) nmb = coldata.calc_statistics(use_area_weights=stats_area_weighted)['nmb'] if in_percent: nmb*=100 ec = cmap(nn(nmb)) else: ec = mec _sc = ax.scatter(lons, lats, c=data, marker=marker, cmap=cmap, vmin=vmin, vmax=vmax, s=s, norm=norm, label=ref_label, edgecolors=ec, linewidths=mew) if add_cbar: if ax_cbar is None: ax_cbar = _add_cbar_axes(ax) cbar = fig.colorbar(_sc, cmap=cmap, norm=norm, #boundaries=bounds, extend=cbar_extend, cax=ax_cbar, orientation=cbar_orientation) cbar.outline.set_visible(cbar_outline_visible) cbar.set_label('NMB [%]') return ax
def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False): """ Requires a midpoint value """ self.midpoint = midpoint Normalize.__init__(self, vmin, vmax, clip)
def spectrogram(data, samp_rate, per_lap=0.9, wlen=None, log=False, outfile=None, fmt=None, axes=None, dbscale=False, mult=8.0, cmap=obspy_sequential, zorder=None, title=None, show=True, sphinx=False, clip=[0.0, 1.0]): """ Computes and plots spectrogram of the input data. :param data: Input data :type samp_rate: float :param samp_rate: Samplerate in Hz :type per_lap: float :param per_lap: Percentage of overlap of sliding window, ranging from 0 to 1. High overlaps take a long time to compute. :type wlen: int or float :param wlen: Window length for fft in seconds. If this parameter is too small, the calculation will take forever. :type log: bool :param log: Logarithmic frequency axis if True, linear frequency axis otherwise. :type outfile: str :param outfile: String for the filename of output file, if None interactive plotting is activated. :type fmt: str :param fmt: Format of image to save :type axes: :class:`matplotlib.axes.Axes` :param axes: Plot into given axes, this deactivates the fmt and outfile option. :type dbscale: bool :param dbscale: If True 10 * log10 of color values is taken, if False the sqrt is taken. :type mult: float :param mult: Pad zeros to length mult * wlen. This will make the spectrogram smoother. :type cmap: :class:`matplotlib.colors.Colormap` :param cmap: Specify a custom colormap instance. If not specified, then the default ObsPy sequential colormap is used. :type zorder: float :param zorder: Specify the zorder of the plot. Only of importance if other plots in the same axes are executed. :type title: str :param title: Set the plot title :type show: bool :param show: Do not call `plt.show()` at end of routine. That way, further modifications can be done to the figure before showing it. :type sphinx: bool :param sphinx: Internal flag used for API doc generation, default False :type clip: [float, float] :param clip: adjust colormap to clip at lower and/or upper end. The given percentages of the amplitude range (linear or logarithmic depending on option `dbscale`) are clipped. """ import matplotlib.pyplot as plt # enforce float for samp_rate samp_rate = float(samp_rate) # set wlen from samp_rate if not specified otherwise if not wlen: wlen = samp_rate / 100. npts = len(data) # nfft needs to be an integer, otherwise a deprecation will be raised # XXX add condition for too many windows => calculation takes for ever nfft = int(_nearest_pow_2(wlen * samp_rate)) if nfft > npts: nfft = int(_nearest_pow_2(npts / 8.0)) if mult is not None: mult = int(_nearest_pow_2(mult)) mult = mult * nfft nlap = int(nfft * float(per_lap)) data = data - data.mean() end = npts / samp_rate # Here we call not plt.specgram as this already produces a plot # matplotlib.mlab.specgram should be faster as it computes only the # arrays # XXX mlab.specgram uses fft, would be better and faster use rfft specgram, freq, time = mlab.specgram(data, Fs=samp_rate, NFFT=nfft, pad_to=mult, noverlap=nlap) # db scale and remove zero/offset for amplitude if dbscale: specgram = 10 * np.log10(specgram[1:, :]) else: specgram = np.sqrt(specgram[1:, :]) freq = freq[1:] vmin, vmax = clip if vmin < 0 or vmax > 1 or vmin >= vmax: msg = "Invalid parameters for clip option." raise ValueError(msg) _range = float(specgram.max() - specgram.min()) vmin = specgram.min() + vmin * _range vmax = specgram.min() + vmax * _range norm = Normalize(vmin, vmax, clip=True) if not axes: fig = plt.figure() ax = fig.add_subplot(111) else: ax = axes # calculate half bin width halfbin_time = (time[1] - time[0]) / 2.0 halfbin_freq = (freq[1] - freq[0]) / 2.0 # argument None is not allowed for kwargs on matplotlib python 3.3 kwargs = {k: v for k, v in (('cmap', cmap), ('zorder', zorder)) if v is not None} if log: # pcolor expects one bin more at the right end freq = np.concatenate((freq, [freq[-1] + 2 * halfbin_freq])) time = np.concatenate((time, [time[-1] + 2 * halfbin_time])) # center bin time -= halfbin_time freq -= halfbin_freq # Log scaling for frequency values (y-axis) ax.set_yscale('log') # Plot times ax.pcolormesh(time, freq, specgram, norm=norm, **kwargs) else: # this method is much much faster! specgram = np.flipud(specgram) # center bin extent = (time[0] - halfbin_time, time[-1] + halfbin_time, freq[0] - halfbin_freq, freq[-1] + halfbin_freq) ax.imshow(specgram, interpolation="nearest", extent=extent, **kwargs) # set correct way of axis, whitespace before and after with window # length ax.axis('tight') ax.set_xlim(0, end) ax.grid(False) if axes: return ax ax.set_xlabel('Time [s]') ax.set_ylabel('Frequency [Hz]') if title: ax.set_title(title) if not sphinx: # ignoring all NumPy warnings during plot with np.errstate(all='ignore'): plt.draw() if outfile: if fmt: fig.savefig(outfile, format=fmt) else: fig.savefig(outfile) elif show: plt.show() else: return fig
def __init__(self, vmin=None, vmax=None, vanchor=None, clip=False, canchor=0.5): self.vanchor = vanchor self.canchor = canchor Normalize.__init__(self, vmin, vmax, clip)
def display(self, ax, step=1, only_used=False, only_unused=False, only_contain_eddies=False, display_criterion=False, field=None, bins=None, cmap="Spectral_r", **kwargs): """ Display contour :param matplotlib.axes.Axes ax: :param int step: display only contour every step :param bool only_used: display only contour used in an eddy :param bool only_unused: display only contour unused in an eddy :param bool only_contain_eddies: display only contour which enclosed an eddiy :param bool display_criterion: display only unused contour with criterion color 0. - Accepted (green) 1. - Reject for shape error (red) 2. - Masked value in contour (blue) 3. - Under or over pixel limit bound (black) 4. - Amplitude criterion (yellow) :param str field: Must be 'shape_error', 'x', 'y' or 'radius'. If define display_criterion is not use. bins argument must be define :param array bins: bins use to colorize contour :param str cmap: Name of cmap to use for field display :param dict kwargs: look at :py:meth:`matplotlib.collections.LineCollection` .. minigallery:: py_eddy_tracker.Contours.display """ from matplotlib.collections import LineCollection overide_color = display_criterion or field is not None if display_criterion: paths = {0: list(), 1: list(), 2: list(), 3: list(), 4: list()} elif field is not None: paths = dict() for i in range(len(bins)): paths[i] = list() paths[i + 1] = list() for j, collection in enumerate(self.contours.collections[::step]): if not overide_color: paths = list() for i in collection.get_paths(): if only_used and not i.used: continue elif only_unused and i.used: continue elif only_contain_eddies and not i.contain_eddies: continue if display_criterion: paths[i.reject].append(i.vertices) elif field is not None: x, y, radius, shape_error = i.fit_circle() if field == "shape_error": i_ = digitize(shape_error, bins) elif field == "radius": i_ = digitize(radius, bins) elif field == "x": i_ = digitize(x, bins) elif field == "y": i_ = digitize(y, bins) paths[i_].append(i.vertices) else: paths.append(i.vertices) local_kwargs = kwargs.copy() if "color" not in kwargs: local_kwargs["color"] = collection.get_color() local_kwargs.pop("label", None) elif j != 0: local_kwargs.pop("label", None) if not overide_color: ax.add_collection(LineCollection(paths, **local_kwargs)) if display_criterion: colors = { 0: "limegreen", 1: "red", 2: "mediumblue", 3: "black", 4: "gold", } for k, v in paths.items(): local_kwargs = kwargs.copy() local_kwargs.pop("label", None) local_kwargs["colors"] = colors[k] ax.add_collection(LineCollection(v, **local_kwargs)) elif field is not None: nb_bins = len(bins) - 1 cmap = get_cmap(cmap, lut=nb_bins) for k, v in paths.items(): local_kwargs = kwargs.copy() local_kwargs.pop("label", None) if k == 0: local_kwargs["colors"] = cmap(0.0) elif k > nb_bins: local_kwargs["colors"] = cmap(1.0) else: local_kwargs["colors"] = cmap((k - 1.0) / nb_bins) mappable = LineCollection(v, **local_kwargs) ax.add_collection(mappable) mappable.cmap = cmap mappable.norm = Normalize(vmin=bins[0], vmax=bins[-1]) # TODO : need to create an object with all collections return mappable else: if hasattr(self.contours, "_mins"): ax.update_datalim([self.contours._mins, self.contours._maxs]) ax.autoscale_view()
def us_choropleth(t): import matplotlib.cm from matplotlib.patches import Polygon from matplotlib.collections import PatchCollection from matplotlib.colors import Normalize import shapefile import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap import numpy as np import random import pandas as pd from collections import Counter plt.title("NER", fontsize=12) us_locations_map = Basemap(resolution="l", llcrnrlon=-128.94, llcrnrlat=23.52, urcrnrlon=-60.12, urcrnrlat=50.93, lat_0=37.26, lon_0=-94.53) us_locations_map.drawmapboundary( fill_color="#46bcec") # Fills in the oceans us_locations_map.fillcontinents( color="#eabc77", lake_color="#46bcec") # Defines the continents us_locations_map.drawcoastlines() fig = matplotlib.pyplot.gcf() fig.set_size_inches(15.5, 12.5) # Sets the size of the map # Converts the coordinates to map points lons, lats = us_locations_map(t["longitude"], t["latitude"]) us_locations_map.scatter(lons, lats, color="black", zorder=10) # Draws the points on the map # Labels each point with the location name for i in range(t.num_rows): lat_lon = (t.row(i).item("longitude") + .2, t.row(i).item("latitude") - .1) plt.annotate(np.array(t.row(i).item("name")), lat_lon, fontsize=10) # Here we are reading in a shape file, which places state boundary # information for our Basemap us_locations_map.readshapefile("data/us_shapefiles/cb_2016_us_state_20m", "us_states") state_names = [] for shape_dict in us_locations_map.us_states_info: state_names.append(shape_dict['NAME']) ax = plt.gca() # get current axes instance cmap = plt.get_cmap('Reds') names = [] shapes = [] counts = [] state_counts = Counter(t["state"]) for index, state in enumerate(state_names): seg = us_locations_map.us_states[index] poly = Polygon(seg) names.append(state) shapes.append(poly) if state in t['state']: counts.append(state_counts[state]) else: counts.append(0) # Loading our lists into the DataFrame shape_table = pd.DataFrame() shape_table["State Name"] = np.array(names) shape_table["Shapes"] = np.array(shapes) shape_table["Count"] = np.array(counts) pc = PatchCollection(shape_table["Shapes"], zorder=2) norm = Normalize() pc.set_facecolor(cmap(norm(shape_table['Count'].fillna(0).values))) pc.set_edgecolor("black") ax.add_collection(pc) # Adds colorbar showing the scale mapper = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap) mapper.set_array(shape_table['Count']) plt.colorbar(mapper, shrink=0.4)
def plot_daywise_preds(self, plot_date, al_df=None,del_time=60,\ omn_train_params=["Bx", "By", "Bz", "Vx", "Np"],\ time_extent=24, plot_type="bar", gen_pred_diff_time=30.): """ Plot historical predictions as a bar graph! """ import matplotlib.dates as mdates import sys sys.path.append("../") import extract_rt_al if al_df is None: # get AL data al_obj = extract_rt_al.ExtractAL() al_df = al_obj.get_al_data() al_df.sort_values(by=['date'], inplace=True) plot_title = plot_date.strftime("%B %d, %Y") # set the plots based on the extent chosen plot_min_date = datetime.datetime(plot_date.year, plot_date.month, plot_date.day, 0, 0) plot_max_date = datetime.datetime(plot_date.year, plot_date.month, plot_date.day, 0, 0) + datetime.timedelta(days=1) plot_xlim = [ plot_min_date,\ plot_max_date,\ ] # now read stored data (if we have it) generate_predictions = True if os.path.isfile(self.hist_pred_data_file): stored_data = numpy.load(self.hist_pred_data_file, allow_pickle=True) stored_date_arr = stored_data['date_arr'] stored_sson_prob_arr = stored_data['sson_prob_arr'] if ( plot_min_date >= stored_date_arr.min() ) and\ ( plot_max_date <= stored_date_arr.max() ): generate_predictions = False date_arr = stored_date_arr sson_prob_arr = stored_sson_prob_arr else: if (plot_date - stored_date_arr.max() ).total_seconds() / 60. < gen_pred_diff_time: generate_predictions = False date_arr = stored_date_arr sson_prob_arr = stored_sson_prob_arr if generate_predictions: date_arr, sson_prob_arr = self.calculate_historical_preds(\ del_time=del_time,\ omn_train_params=omn_train_params ) hist_pred_date_lim = [min(date_arr), max(date_arr)] # check if we have data if (plot_date < plot_min_date) or (plot_date > plot_max_date): return None date_formatter = DateFormatter('%H:%M') cmap = custom_cmap.create_custom_cmap() #plt.cm.get_cmap('Reds') norm = Normalize(vmin=0, vmax=1) colors = cmap(norm(sson_prob_arr)) # finally!!! plt.style.use("fivethirtyeight") fig, ax = plt.subplots(nrows=1, ncols=1) ax2 = ax.twinx() if plot_type == "filled_curve": hist_plot = ax.fill_between(date_arr, 0, sson_prob_arr, cmap=cmap) else: # The unit for bar width on a date x axis is days bar_width = del_time / (60. * 24.) plt_date_arr = [x.to_pydatetime() for x in date_arr] hist_plot = ax.bar(plt_date_arr,sson_prob_arr, width=bar_width,\ alpha=0.7, align="edge",\ color=colors) # colorbar sm = ScalarMappable(cmap=cmap, norm=norm) sm.set_array([]) cbar = plt.colorbar(sm) cbar.ax.tick_params(labelsize=8) cbar.set_label('Ponset', fontsize=10) # plot a horizontal line at 0.5 # ax.axhline(y=0., color='#018571', linestyle='-',\ # linewidth=1.5) # ax.axhline(y=0.25, color='#80cdc1', linestyle='-',\ # linewidth=1.5) ax.axhline(y=0.5, color='k', linestyle='-',\ linewidth=1.)##fdb863 # ax.axhline(y=0.75, color='#d7191c', linestyle='-',\ # linewidth=1.5) ax2.plot_date(al_df["date"], al_df["al"],\ '--',color='#008fd5', linewidth=1.) ax2.set_ylim([-2000, 100]) ax2.set_yticks([-2000, -1000, -500, 0]) ax2.tick_params(axis='y', colors='#008fd5') # Hide grid lines ax2.grid(linestyle=':', linewidth='1.', color='#008fd5') # axes settings ax.set_ylim([0, 1]) ax.set_xlim(plot_xlim) ax.get_xaxis().set_major_formatter(date_formatter) ax.set_ylabel("Prob. of Onset", fontsize=12) ax.set_xlabel('UT TIME', fontsize=12) # set minor ticks ax.xaxis.set_major_locator(mdates.HourLocator(interval=3)) ax.xaxis.set_minor_locator(mdates.HourLocator(interval=1)) ax.grid(b=True, which='minor', linestyle=':') # plt.xticks(rotation=60, fontsize=10) plt.setp(ax.yaxis.get_majorticklabels(), rotation=70, fontsize=10) plt.setp(ax.xaxis.get_majorticklabels(), rotation=70, fontsize=10) plt.setp(ax2.yaxis.get_majorticklabels(), rotation=45, fontsize=6) plt.title(plot_title, fontsize=10) # plt.yticks(fontsize=10) plt.tight_layout() return fig
def __init__(self, cmap, vmin, vmax=None, label=True, label_position=None, label_rotation=None, clipmin=None, clipmax=None, orientation='horizontal', unit=None, contours=(), width=None, ticks=None, threshold=None, ticklocation='auto', background='white', tight=True, h=None, w=None, *args, **kwargs): # get Colormap if isinstance(cmap, np.ndarray): if threshold is not None: raise NotImplementedError("threshold parameter with cmap=array") if cmap.max() > 1: cmap = cmap / 255. cm = mpl.colors.ListedColormap(cmap, 'LUT') else: cm = mpl.cm.get_cmap(cmap) # prepare layout if orientation == 'horizontal': if h is None and w is None: h = 1 ax_aspect = 4 elif orientation == 'vertical': if h is None and w is None: h = 4 ax_aspect = 0.3 else: raise ValueError("orientation=%s" % repr(orientation)) layout = Layout(1, ax_aspect, 2, tight, False, h, w, *args, **kwargs) EelFigure.__init__(self, cm.name, layout) ax = self._axes[0] # translate between axes and data coordinates if isinstance(vmin, Normalize): norm = vmin else: vmin, vmax = fix_vlim_for_cmap(vmin, vmax, cm.name) norm = Normalize(vmin, vmax) # value ticks if ticks is False: ticks = () tick_labels = None elif isinstance(ticks, dict): tick_dict = ticks ticks = sorted(tick_dict) tick_labels = [tick_dict[t] for t in ticks] else: tick_labels = None if orientation == 'horizontal': axis = ax.xaxis contour_func = ax.axhline else: axis = ax.yaxis contour_func = ax.axvline if label is True: if unit: label = unit else: label = cm.name elif not label: label = '' # show only part of the colorbar if clipmin is not None or clipmax is not None: if isinstance(norm, SymmetricNormalize): raise NotImplementedError( "clipmin or clipmax with SymmetricNormalize") boundaries = norm.inverse(np.linspace(0, 1, cm.N + 1)) if clipmin is None: start = None else: start = np.digitize(clipmin, boundaries, True) if clipmax is None: stop = None else: stop = np.digitize(clipmax, boundaries) + 1 boundaries = boundaries[start:stop] else: boundaries = None colorbar = ColorbarBase(ax, cm, norm, boundaries=boundaries, orientation=orientation, ticklocation=ticklocation, ticks=ticks, label=label) # fix tick location if isinstance(norm, SymmetricNormalize) and ticks is not None: tick_norm = Normalize(norm.vmin, norm.vmax, norm.clip) axis.set_ticks(tick_norm(ticks)) # unit-based tick-labels if unit and tick_labels is None: formatter, label = find_axis_params_data(unit, label) tick_labels = tuple(map(formatter, colorbar.get_ticks())) if tick_labels is not None: if clipmin is not None: tick_labels = [l for l, t in zip(tick_labels, ticks) if t >= clipmin] axis.set_ticklabels(tick_labels) # label position/rotation if label_position is not None: axis.set_label_position(label_position) if label_rotation is not None: axis.label.set_rotation(label_rotation) if orientation == 'vertical': if (label_rotation + 10) % 360 < 20: axis.label.set_va('center') elif orientation == 'vertical' and len(label) <= 3: axis.label.set_rotation(0) axis.label.set_va('center') self._contours = [contour_func(c, c='k') for c in contours] self._draw_hooks.append(self.__fix_alpha) self._draw_hooks.append(self.__update_bar_tickness) self._background = background self._colorbar = colorbar self._orientation = orientation self._width = width self._show()
def plot_historical_preds(self, del_time=60,\ omn_train_params=["Bx", "By", "Bz", "Vx", "Np"],\ time_extent=24, plot_type="bar"): """ Plot historical predictions as a bar graph! """ import sys sys.path.append("../") import extract_rt_al # # get AL data al_obj = extract_rt_al.ExtractAL() al_df = al_obj.get_al_data() al_df.sort_values(by=['date'], inplace=True) # now get to substorms date_arr, sson_prob_arr = self.calculate_historical_preds(\ del_time=del_time,\ omn_train_params=omn_train_params ) # set the plots based on the extent chosen plot_xlim = [ max(date_arr) - datetime.timedelta(minutes=time_extent*60),\ max(date_arr) + datetime.timedelta(minutes=180) ] if time_extent <= 24: date_formatter = DateFormatter('%H:%M') else: date_formatter = DateFormatter('%m/%d-%H') cmap = custom_cmap.create_custom_cmap() #plt.cm.get_cmap('Reds') norm = Normalize(vmin=0, vmax=1) colors = cmap(norm(sson_prob_arr)) # finally!!! plt.style.use("fivethirtyeight") fig, ax = plt.subplots(nrows=1, ncols=1) ax2 = ax.twinx() if plot_type == "filled_curve": hist_plot = ax.fill_between(date_arr, 0, sson_prob_arr, cmap=cmap) else: # The unit for bar width on a date x axis is days bar_width = del_time / (60. * 24.) hist_plot = ax.bar(date_arr,sson_prob_arr, width=bar_width,\ alpha=0.7, align="edge",\ color=colors) # colorbar sm = ScalarMappable(cmap=cmap, norm=norm) sm.set_array([]) cbar = plt.colorbar(sm) cbar.ax.tick_params(labelsize=8) cbar.set_label('Ponset', fontsize=10) # plot a horizontal line at 0.5 # ax.axhline(y=0., color='#018571', linestyle='-',\ # linewidth=1.5) # ax.axhline(y=0.25, color='#80cdc1', linestyle='-',\ # linewidth=1.5) ax.axhline(y=0.5, color='k', linestyle='-',\ linewidth=1.)##fdb863 # ax.axhline(y=0.75, color='#d7191c', linestyle='-',\ # linewidth=1.5) ax2.plot_date(al_df["date"], al_df["al"],\ '--',color='#008fd5', linewidth=1.) ax2.set_ylim([-2000, 100]) ax2.set_yticks([-2000, -1000, -500, 0]) ax2.tick_params(axis='y', colors='#008fd5') # Hide grid lines ax2.grid(linestyle=':', linewidth='1.', color='#008fd5') # axes settings ax.set_ylim([0, 1]) ax.set_xlim(plot_xlim) ax.get_xaxis().set_major_formatter(date_formatter) ax.set_ylabel("Prob. of Onset", fontsize=12) ax.set_xlabel('UT TIME', fontsize=12) # plt.xticks(rotation=60, fontsize=10) plt.setp(ax.yaxis.get_majorticklabels(), rotation=70, fontsize=10) plt.setp(ax.xaxis.get_majorticklabels(), rotation=70, fontsize=10) plt.setp(ax2.yaxis.get_majorticklabels(), rotation=45, fontsize=6) # plt.yticks(fontsize=10) plt.tight_layout() return fig
def __call__(self, epoch, **kargs): if isinstance(epoch, np.ndarray) and len(epoch.shape) == 0: epoch = epoch.item() return Normalize.__call__(self, self.mktime(epoch), **kargs)
result_table_combined = cached( lambda: photometry.do_photometry(img_combined, guess_table), cache_dir / 'photometry_combined_naco_gauss', rerun=False) # %% photometry._last_image = img_combined - photometry.bkg_estimator(img_combined) #residual = photometry_quadratic.residual(result_table_combined_quadratic) residual = photometry.residual(result_table_combined) plt.figure() #plt.imshow(residual, norm=SymLogNorm(10,vmin=-7000,vmax=5000, clip=True)) cmap = plt.cm.get_cmap('viridis').copy() cmap.set_over('red') cmap.set_under('red') plt.imshow(residual, norm=Normalize(vmin=-700, vmax=400, clip=False), cmap=cmap) #plt.imshow(np.abs(img_combined/residual), norm=Normalize(vmin=0, vmax=200, clip=False)) plt.colorbar() #plt.plot(result_table_combined['x_fit'], result_table_combined['y_fit'], '.', markersize=2) plt.xlim(0, 800) plt.ylim(800, 0) save_plot(out_dir, "naco_residual") print(np.sum(np.abs(residual))) # %% fig, axs = plt.subplots(1, 2, sharey=True) #plt.hexbin(xs%1, ys%1,gridsize=200) axs[0].plot(result_table_combined_quadratic['x_fit'] % 1, result_table_combined_quadratic['y_fit'] % 1, 'x',
def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False): self.midpoint = midpoint # m=max(abs(vmin),abs(vmax)) Normalize.__init__(self, vmin, vmax, clip)
# box whisker plot fig = plt.figure() ax = fig.add_subplot(111) ax = sns.boxplot(n_hyper_pct.transpose(), whis=5, ax=ax) ax.scatter(range(len(pids)), np.diagonal(n_hyper_pct), facecolor='k') ax.set_ylim([0, 100]) ax.set_ylabel("% of hyper methylated DMRs") fig.savefig(os.path.join(outdir, "n_hyper_box_whisker.png"), dpi=200) # pie chart array hypo_cmap = plt.get_cmap('Greens_r') hyper_cmap = plt.get_cmap('Reds') hypo_norm = Normalize(vmin=-6, vmax=0) hyper_norm = Normalize(vmin=0, vmax=6) hypo_sm = cm.ScalarMappable(norm=hypo_norm, cmap=hypo_cmap) hyper_sm = cm.ScalarMappable(norm=hyper_norm, cmap=hyper_cmap) wedgeprops = {'edgecolor': 'k', 'linewidth': .5} scale_by_number = 'area' segment_width = 0.5 min_segment_width = 0.2 inner_radius = 0.5 edges = np.array( [-np.inf, -5., -4., -3., -2., -1., 0., 1., 2., 3., 4., 5., np.inf]) # create a new colour dict colour_dict = { 'Hypermethylated': consts.METHYLATION_DIRECTION_COLOURS['hyper'],
def plot_2D(X_mesh, Y_mesh, C=None, fig=None, ax=None, xlabel='', ylabel='', color_label='', xlim=None, ylim=None, vmin=None, vmax=None, log_scale=False, linthresh=None, linscale=None, title='', pcolormesh_kwargs={}, figsize=None): ## Check input arguments and assign into member variables if needed. for arg in [X_mesh, Y_mesh]: assert type(arg) is np.ndarray assert arg.ndim == 2 assert X_mesh.shape == Y_mesh.shape mesh_shape = X_mesh.shape # Equivalent to Y_mesh.shape if X_mesh.shape == Y_mesh.shape expected_C_shape = tuple(np.array(mesh_shape) - 1) if C is None: C = np.zeros(expected_C_shape) else: assert type(C) is np.ndarray assert C.shape == expected_C_shape # Process 'ax' and 'fig' fig, ax = process_fig_and_ax_argument(fig, ax, default_figsize=figsize) # if (fig is None) and (ax is None): # fig, ax = plt.subplots(figsize=(8,6)) # else: # assert (is_figure(fig)) and (is_axes(ax)) assert type(log_scale) is bool # Process limit-like objects for arg in [xlim, ylim]: check_limit_argument(arg) # for arg in [xlim, ylim]: # if arg is not None: # try: arg = list(arg) # except: # raise TypeError("'xlim', 'ylim' should be convertable to list") # assert len(arg) == 2 for arg in [vmin, vmax]: if arg is not None: assert is_real_number(arg) C_min, C_max = C.min(), C.max() vmin_given, vmax_given = vmin, vmax if vmin is None: vmin = C_min #else: assert vmin >= C_min if vmax is None: vmax = C_max #else: assert vmax <= C_max assert vmin <= vmax assert type(pcolormesh_kwargs) is dict ## Initial Settings for appearence set_global_fontsize_from_fig(fig, scale=1.5) rcParams['mathtext.fontset'] = 'stix' ## Set a normalization function norm = None if 'norm' in pcolormesh_kwargs.keys(): norm_given = pcolormesh_kwargs.pop('norm') assert is_norm(norm_given) norm = norm_given if norm.vmin is None: norm.vmin = vmin else: if vmin_given is not None: if vmin_given != norm.vmin: raise Exception( "given norm's vmin and given vmin are different.") if norm.vmax is None: norm.vmax = vmax else: # Set 'linscale' argument if linscale is None: linscale = 1.0 else: assert is_real_number(linscale) assert linscale > 0 # Set 'linthresh' argument min_radius = min(map(abs, [vmin, vmax])) if linthresh is not None: assert is_real_number(linthresh) assert (linthresh > 0) and (linthresh < min_radius) else: linthresh = 0.1 * linscale * min_radius #print('vmin, vmax: ',vmin, vmax) #print('linthresh, linscale: ', linthresh, linscale) # Instantiate 'SymLogNorm' norm = SymLogNorm(linthresh=linthresh, linscale=linscale, vmin=vmin, vmax=vmax) else: norm = Normalize(vmin=vmin, vmax=vmax) pcm = ax.pcolormesh(X_mesh, Y_mesh, C, norm=norm, **pcolormesh_kwargs) cb = fig.colorbar(pcm, ax=ax) ax.tick_params(axis='both', labelsize='large') cb.ax.tick_params(axis='y', labelsize='large') ax.axis('square') if xlim is None: xlim = (X_mesh.min(), X_mesh.max()) ax.set_xlim(*xlim) if ylim is None: ylim = (Y_mesh.min(), Y_mesh.max()) ax.set_ylim(*ylim) ax.set_title(title, fontsize='xx-large') ax.set_xlabel(xlabel, fontsize='xx-large') ax.set_ylabel(ylabel, fontsize='xx-large') cb.set_label(color_label, fontsize='xx-large', rotation=270, va='bottom') return fig, ax, pcm
def results(): import numpy as np from pandas import DataFrame import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap, rgb2hex, Normalize import datetime import get_flightstats as gfs import apply_RF_model as am import cache as c _, carrier_dict_code = mu.read_carrier_dict() ## cmap = plt.get_cmap('RdYlGn_r') cdict = { 'red': ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)), 'green': ((0.0, 1.0, 1.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)) } cmap = LinearSegmentedColormap('my_colormap', cdict, 100) departure_date = datetime.datetime.strptime(request.args['date'], '%Y-%m-%d') departure_date_string = departure_date.strftime('%a, %b %d %Y') request_info = { 'date': departure_date, 'date_string': departure_date_string } ### Looking to buy a flight if 'origin' in request.args.keys(): request_info['mode'] = 0 request_info['origin'] = request.args['origin'] request_info['destination'] = request.args['destination'] ### Already have a flight if 'carrier' in request.args.keys(): _, carrier_dict_code = mu.read_carrier_dict() request_info['mode'] = 1 request_info['carrier'] = request.args['carrier'] request_info['flightnumber'] = int(request.args['flightnumber']) print request_info ### get cached results, if available (return None if not found) cached_results = c.get_cached_results(request_info) ### temporarily turn off caching ## cached_results = None if cached_results is None: fs_json = gfs.get_flightstats_json(request_info) flightstats = gfs.parse_flightstats_json(fs_json) ### If carrier-flightnumber, remove all but the desired flighs ### from flightstats if request_info['mode'] == 1: for fs in flightstats: if ((fs['CarrierCode'][0] == request_info['carrier']) & (fs['FlightNumber'][0] == request_info['flightnumber'])): break flightstats = [fs] ### Flatten flightstats into a list of all flights (connecting ### legs are treated as a separate flight) flights = DataFrame(gfs.flatten_flightstats(flightstats)) Pdelay_dict_orig, Pdelay_dict_dest, model_summary_orig, model_summary_dest = am.apply_RF_model( flights, request_info['origin'], request_info['destination']) def assign_Pdelay(fid, Pdelay_dict_orig, Pdelay_dict_dest): if fid in Pdelay_dict_dest.keys(): return Pdelay_dict_dest[fid] elif fid in Pdelay_dict_orig.keys(): return Pdelay_dict_orig[fid] else: return -1.0 color_norm = Normalize(0.0, 0.5) for fs in flightstats: ### Pdelay = np.random.uniform(size=len(fs['FlightID'])) Pdelay = [ assign_Pdelay(x, Pdelay_dict_orig, Pdelay_dict_dest) for x in fs['FlightID'] ] fs['IconColor'] = [rgb2hex(cmap(color_norm(x))) for x in Pdelay] ## fs['DelayProbability'] = ['%.1f%%' % (100.0*x) for x in Pdelay] fs['DelayProbability'] = Pdelay ### model summaries model_summaries = {} if model_summary_orig: importance, feature = zip( *sorted(zip(model_summary_orig['feature_importances'], model_summary_orig['training_columns']), reverse=True)) model_summary_orig['top5_features'] = [ (feature[i], importance[i]) for i in xrange(5) ] model_summaries['origin'] = model_summary_orig if model_summary_dest: importance, feature = zip( *sorted(zip(model_summary_dest['feature_importances'], model_summary_dest['training_columns']), reverse=True)) model_summary_dest['top5_features'] = [ (feature[i], importance[i]) for i in xrange(5) ] model_summaries['destination'] = model_summary_dest ## end of 'for fs in flightstats:' ## cache the results c.cache_results(request_info, flightstats, model_summaries) else: (flightstats, model_summaries) = cached_results ## end of 'if not cached_results:' carriers = [x['Carrier'] for x in flightstats] unique_carriers = list( np.unique(np.array([item for sublist in carriers for item in sublist]))) ### remove any carriers that aren't known for uc in unique_carriers: if uc not in carrier_dict_code.keys(): print 'Removing unknown carrier "%s" from unique_carriers list.' % uc unique_carriers.remove(uc) unique_carriers = [{ 'name': x, 'code': carrier_dict_code[x] } for x in unique_carriers] ### Render the page return render_template('results.html', title='Results', request_info=request_info, flightstats=flightstats, unique_carriers=unique_carriers, model_summaries=model_summaries)
@pytest.mark.parametrize('label', [ 'instant', 'beginning', 'ending', 'event', pytest.param('other', marks=pytest.mark.xfail(raises=ValueError)) ]) def test_line_or_step_plotly(label): out = utils.line_or_step_plotly(label) assert isinstance(out, dict) color_map = cm.get_cmap('viridis') color_scaler = cm.ScalarMappable( Normalize(vmin=0, vmax=1), color_map, ) @pytest.mark.parametrize('percentile,expected', [ (100, '#fde725'), (90, '#bddf26'), (50, '#21918c'), (20, '#414487'), (5, '#471365'), (1, '#450457'), ]) def test_distribution_fill_color(percentile, expected): assert utils.distribution_fill_color(color_scaler, percentile) == expected