def _polish(self, f): # Handle properties of axes directly #a = plt.gca() # Current set of axes formatter_scalar = ScalarFormatter(useOffset=True, useMathText=False) formatter_scalar.set_powerlimits((-3, 3)) formatter_log = LogFormatterMathtext(base=10.0, labelOnlyBase=False) # Neaten axes formatters for ax in f.get_axes(): if not isinstance(ax.xaxis.get_major_formatter(), NullFormatter): if ax.xaxis.get_scale() == "log": ax.xaxis.set_major_locator( LogLocator(base=10.0, subs=[1.0], numdecs=1)) ax.xaxis.set_major_formatter(formatter_log) else: ax.xaxis.set_major_formatter(formatter_scalar) if not isinstance(ax.yaxis.get_major_formatter(), NullFormatter): if ax.yaxis.get_scale() == "log": ax.yaxis.set_major_locator( LogLocator(base=10.0, subs=[1.0], numdecs=1)) ax.yaxis.set_major_formatter(formatter_log) #ax.yaxis.set_minor_locator(LogLocator(base=10.0, subs=[10], numdecs=1)) # why is this necessary? else: ax.yaxis.set_major_formatter(formatter_scalar)
def lazy_field_coverage(field, log=False, polar=False): # set up figure fig = plt.figure(figsize=(8,8),dpi=100) if polar: ax = fig.add_subplot(111, projection='polar') else: ax = fig.add_subplot(111, aspect='equal') limit = np.ceil(np.max([np.abs(field.min()),np.abs(field.max())])) # create image if log: im = ax.imshow(field+1,cmap='jet',extent=(-12,12,-12,12),norm=LogNorm(),vmin=-limit,vmax=limit) cb = plt.colorbar(im, format=LogFormatterMathtext(),) else: im = ax.imshow(field,cmap='jet',extent=(-12,12,-12,12),vmin=-limit,vmax=limit) cb = plt.colorbar(im) # beautify plt.xticks(np.arange(-12,13,3),fontsize='16') plt.yticks(np.arange(-12,13,3),fontsize='16') plt.xlabel('degrees in X',fontsize='20') plt.ylabel('degrees in Y',fontsize='20') plt.xlim((-12,12)) plt.ylim((-12,12)) plt.subplots_adjust(left=0.12, right=0.98, top=0.98, bottom=.06) plt.grid('on',c='w') plt.show() return None
def plot_prob_density(x, y, cmap=cm.get_cmap('summer'), xscale='log', yscale='log', xParam=1.5, yParam=1.5, ax=None, use_gridspec=False): """ Plots the normalized probability density. See :py:func:`plot_counts` for explanations of the input variables. """ fig, ax, cax = _get_fig_ax_and_colorbar_ax(xscale, yscale, ax=ax) X, Y, counts_matrix, bin_centers, means, Bins2D = \ _get_count_data_to_plot(x, y, xscale, yscale, xParam, yParam) prob_density = counts_matrix / (Bins2D.sizes.T * np.sum(counts_matrix)) assert np.abs(np.sum(prob_density * Bins2D.sizes.T) - 1) < 0.000001 im = ax.pcolor(X, Y, prob_density, cmap=cmap, norm=colors.LogNorm()) ax.plot(bin_centers, means, "go-") cbar = fig.colorbar(im, cax, ax=ax, use_gridspec=use_gridspec, orientation='vertical', format=LogFormatterMathtext()) return fig, ax, cbar, im
def plot_conf_xval(xval_conf, ax=None, xlim=(1.01, 1000), probs=(2.5, 97.5), line=False, **kwargs): if not line and len(probs) != 2: raise ValueError( "Need exactly two 'probs' when plotting 'fill_between'") LAMBDA = xval_conf['LAMBDA'] ax, plot_LAMBDA, kwargs = __prepare_plot_xval__(ax, LAMBDA, **kwargs) conf_retv = xval_conf['conf_retv'] ret = xval_conf['ret'] xmin = np.max([1.00001 / LAMBDA, xlim[0], np.min(ret)]) xmax = np.min([xlim[1], np.max(ret)]) xlim = np.asarray([xmin, xmax]) plot_x = Y_of_T(xlim, LAMBDA=plot_LAMBDA) plot_x = np.linspace(*plot_x) xx = Y_of_T(ret, LAMBDA=plot_LAMBDA) av_probs = xval_conf['probs'] IDX = [] for i in probs: x = np.nonzero(i == av_probs)[0] if len(x): IDX.append(x[0]) if not len(IDX): raise ValueError("desired prob not available") if line: kwargs.setdefault('lw', 0.5) for i in IDX: # y = sp.interpolate.interp1d(xx, conf_retv[i, ], kind='cubic', bounds_error=False)(plot_x) y = sp.interpolate.UnivariateSpline(xx, conf_retv[i, ], k=3)(plot_x) ax.semilogx(np.e**plot_x, y, **kwargs) else: y1 = sp.interpolate.UnivariateSpline(xx, conf_retv[0, ], k=3)(plot_x) y2 = sp.interpolate.UnivariateSpline(xx, conf_retv[1, ], k=3)(plot_x) # add some options for the plot if not set already kwargs.setdefault('alpha', 0.15) kwargs.setdefault('zorder', -1000) kwargs.setdefault('lw', 0) ax.fill_between(np.e**plot_x, y1, y2, **kwargs) ax.xaxis.set_major_formatter(LogFormatterMathtext())
def set_default_locators_and_formatters(self, axis): # Copy of Symmetric LogScale version of this function axis.set_major_locator(SymmetricalLogLocator(self.get_transform())) axis.set_major_formatter(LogFormatterMathtext(self.base)) axis.set_minor_locator( SymmetricalLogLocator(self.get_transform(), self.subs)) axis.set_minor_formatter(NullFormatter())
def set_default_locators_and_formatters(self, axis): """ Set the locators and formatters to specialized versions for log scaling. """ axis.set_major_locator(LogLocator(self.base)) axis.set_major_formatter(LogFormatterMathtext(self.base)) axis.set_minor_locator(LogLocator(self.base, self.subs)) axis.set_minor_formatter(NullFormatter())
def plot_counts(x, y, cmap=cm.get_cmap('summer'), xscale='log', yscale='log', xParam=1.5, yParam=1.5, ax=None, use_gridspec=False): """ Plots the counts as a heatmap. Parameters ---------- x : list-like (1D numpy array) values on x-axis y : list-like (1D numpy array) values on x-axis if xbins and ybins are both given cmap : matplotlib.cm colormap to use for the plot, defaulting to summer xscale : {"log", "linear", "linlog", ...}, optional see binner.binner or binner.bins for more info on the options yscale : {"log", "linear", "linlog", ...}, optional see binner.binner or binner.bins for more info on the options xParam : varies according to xscale if xscale == 'log' xParam equals to the multiplier if xscale == 'lin' xParam equals to the number of bins yParam : varies according to yscale see xParam ax : axis object give this if plotting is used as a subroutine for existing axis. Created if None. use_gridspec : bool set true if subroutine plotting to a subplot with gridspec fixes the colorbar to the correct subplot """ fig, ax, cax = _get_fig_ax_and_colorbar_ax(xscale, yscale, ax=ax) X, Y, counts_matrix, bin_centers, means, Bins2D = \ _get_count_data_to_plot(x, y, xscale, yscale, xParam, yParam) im = ax.pcolor(X, Y, counts_matrix, cmap=cmap, norm=colors.LogNorm()) ax.plot(bin_centers, means, "go-") cbar = fig.colorbar(im, cax, ax=ax, use_gridspec=use_gridspec, orientation='vertical', format=LogFormatterMathtext()) return fig, ax, cbar, im
def plot_matrix(self, mat, x=None, y=None, aspect=None, zscale=None, vmin=None, vmax=None, ticks_fmt=None, num_label=None, colorbar=True, barloc="right", flip_axis=True, barsize=0.05, barpad=0.1, contour=False, norm=None, extent=None, formatter=None, **kwargs): if x is None: x = np.arange(mat.shape[1]) if y is None: y = np.arange(mat.shape[0]) x_size, y_size = len(x), len(y) if extent is None: extent = [0, x_size, 0, y_size] # setting aspect of figure if aspect == "square": aspect = x_size / y_size # setting zscale if zscale == "log": norm = LogNorm(vmin=vmin, vmax=vmax) formatter = LogFormatterMathtext() elif zscale == "discrete": if norm is not None: norm = BoundaryNorm(norm, len(norm)) else: norm = None # is contour if contour: im = self.contour(x, y, mat, norm=norm, extent=extent, vmin=vmin, vmax=vmax, **kwargs) else: im = self.imshow(mat[::-1], norm=norm, extent=extent, aspect=aspect, vmin=vmin, vmax=vmax, **kwargs) if flip_axis: self.invert_yaxis() if colorbar: divider = make_axes_locatable(self) cax = divider.append_axes( barloc, size="{}%".format(100 * barsize), pad=barpad) cb = self.figure.colorbar(im, cax=cax, format=formatter) if num_label is not None: x_skip = int(x_size / num_label) y_skip = int(y_size / num_label) self.set_xticks(np.arange(x_size)[::x_skip]) self.set_yticks(np.arange(y_size)[::y_skip]) if ticks_fmt is None: self.set_xticklabels(x[::x_skip]) self.set_yticklabels(y[::x_skip]) else: xlabels = [ticks_fmt.format(_) for _ in x] ylabels = [ticks_fmt.format(_) for _ in y] self.set_xticklabels(xlabels[::x_skip]) self.set_yticklabels(ylabels[::y_skip]) if colorbar: return im, cb else: return im
def build_chart(x_data, y_data, label_req, dur, scatter = False ): matplotlib.rcParams.update({'font.family': 'monospace'}) label = labels[label_req] dates = mdate.epoch2num(x_data) fig, ax = plt.subplots() if scatter: plt.scatter(dates, y_data,s=.01) else: ax.plot(dates, y_data,linewidth=.5,color = 'black') if dur !='1M': label['x-axis'] ='Year' if label['scale']: if 'ℳ' in list(label['y-axis']) : plt.ylabel('ℳ', fontdict = {'family': 'sans'}) ax.set(xlabel = label['x-axis'], ylabel = label['y-axis'], title = label['title'], yscale = label['scale']) else: ax.set(xlabel = label['x-axis'], ylabel = label['y-axis'], title = label['title']) if label['scale']=='log': ax.yaxis.set_major_locator(LogLocator(base=10)) ax.yaxis.set_minor_locator(LogLocator(base=10,subs=[0.0, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])) ax.yaxis.set_major_formatter(LogFormatterMathtext(labelOnlyBase=True)) if dur == '1M': date_formatter = mdate.DateFormatter('%m/%d/%y') ax.xaxis.set_minor_locator(mdate.DayLocator()) ax.xaxis.set_major_locator(mdate.WeekdayLocator(byweekday = mdate.SU)) ax.xaxis.set_major_formatter(date_formatter) elif dur == '1Y': date_formatter = mdate.DateFormatter("%b.%y") ax.xaxis.set_minor_locator(mdate.MonthLocator()) ax.xaxis.set_major_locator(mdate.MonthLocator(bymonth=[1,4,7,10])) ax.xaxis.set_major_formatter(date_formatter) else: date_formatter = mdate.DateFormatter('%y') ax.xaxis.set_minor_locator(mdate.MonthLocator(bymonth=[1,4,7,10])) ax.xaxis.set_major_locator(mdate.YearLocator()) ax.xaxis.set_major_formatter(date_formatter) ax.grid(which='major',linestyle = '--') ax.grid(which='minor',linestyle = ':') ax.grid(True) fig.set_size_inches(8,5) fig.savefig("static/data/"+ label['file-name'] +"_" + dur + ".png",dpi=150) plt.cla() plt.clf() plt.close('all') return True
def field_coverage(x, y, s, deg_x, deg_y, log=False, polar=False): # set up figure fig = plt.figure(figsize=(8, 8), dpi=100) if polar: ax = fig.add_subplot(111, projection='polar') else: ax = fig.add_subplot(111, aspect='equal') # set up a blank field field = np.zeros_like(deg_x) # create the RFs for r in np.arange(len(x)): rf = generate_og_receptive_field(x[r], y[r], s[r], deg_x, deg_y) # d = np.sqrt((x[r]-deg_x)**2 + (y[r]-deg_y)**2)<s[r] field += rf # normalize field /= np.max(field) field *= 100 # create image if log: im = ax.imshow(field, cmap='viridis', extent=(-12, 12, -12, 12), norm=LogNorm(), vmin=1e0, vmax=1e2) cb = plt.colorbar( im, format=LogFormatterMathtext(), ) else: im = ax.imshow(field, cmap='viridis', extent=(-12, 12, -12, 12), vmin=1e0, vmax=1e2) cb = plt.colorbar(im) # beautify plt.xticks(np.arange(-12, 13, 3), fontsize='16') plt.yticks(np.arange(-12, 13, 3), fontsize='16') plt.xlabel('degrees in X', fontsize='20') plt.ylabel('degrees in Y', fontsize='20') plt.xlim((-12, 12)) plt.ylim((-12, 12)) plt.subplots_adjust(left=0.12, right=0.98, top=0.98, bottom=.06) plt.grid('on', c='w') plt.show() return field
def update_ticks(axes, coord, components, is_log): """ Changes the axes to have the proper tick formatting based on the type of component. Returns `None` or the number of categories if components is Categorical. Parameters ---------- axes : `~matplotlib.axes.Axes` A matplotlib axis object to alter coord : { 'x' | 'y' } The coordinate axis on which to update the ticks components : iterable A list of components that are plotted along this axis if_log : boolean Whether the axis has a log-scale """ if coord == 'x': axis = axes.xaxis elif coord == 'y': axis = axes.yaxis else: raise TypeError("coord must be one of x,y") is_cat = any(comp.categorical for comp in components) is_date = any(comp.datetime for comp in components) if is_date: loc = AutoDateLocator() fmt = AutoDateFormatter(loc) axis.set_major_locator(loc) axis.set_major_formatter(fmt) elif is_log: axis.set_major_locator(LogLocator()) axis.set_major_formatter(LogFormatterMathtext()) elif is_cat: all_categories = np.empty((0, ), dtype=np.object) for comp in components: all_categories = np.union1d(comp.categories, all_categories) locator = MaxNLocator(10, integer=True) locator.view_limits(0, all_categories.shape[0]) format_func = partial(tick_linker, all_categories) formatter = FuncFormatter(format_func) axis.set_major_locator(locator) axis.set_major_formatter(formatter) return all_categories.shape[0] else: axis.set_major_locator(AutoLocator()) axis.set_major_formatter(ScalarFormatter())
def save_figure_frank_tamm(path, wavelength_start=250e-9, wavelength_end=700e-9): altitudes = np.logspace(np.log10(1e3), np.log10(100e3), 128) energies = np.logspace(np.log10(1e7 * tas.UNIT_CHARGE), np.log10(1e9 * tas.UNIT_CHARGE), 128) cherenkov_yield = np.zeros(shape=(altitudes.shape[0], energies.shape[0])) for aidx, altitude in enumerate(altitudes): for eidx, energy in enumerate(energies): current_n = tas.refraction_in_air(altitude) current_beta = tas.natural_velocity( kinetic_energy=energy, rest_energy=tas.ELECTRON_REST_ENERGY) if current_beta < 1. / current_n: cherenkov_yield[aidx, eidx] = 0. else: cherenkov_yield[aidx, eidx] = -tas.dE_over_dz( q=tas.UNIT_CHARGE, beta=current_beta, n=current_n, mu=tas.PERMABILITY_AIR, wavelength_start=wavelength_start, wavelength_end=wavelength_end) dpi = 200 fig = plt.figure(figsize=(8, 4.5), dpi=dpi) ax = fig.add_axes((0.1, 0.1, 0.9, 0.85)) im = ax.pcolormesh( energies / tas.UNIT_CHARGE, # GeV altitudes, # m cherenkov_yield / tas.UNIT_CHARGE, # eV/m norm=LogNorm()) ax.loglog() ax.set_xlabel(r"E / eV") ax.set_ylabel(r"z (above sea level)/ m") ax.set_title("Wavelength: {:.0f}nm - {:.0f}nm".format( wavelength_start * 1e9, wavelength_end * 1e9)) ax.grid(color='k', linestyle='-', linewidth=0.66, alpha=0.1) cbar = plt.colorbar(im, format=LogFormatterMathtext()) cbar.ax.set_ylabel(r'dE/dz / eV m$^{-1}$') fig.savefig(path)
def plot_conditional_prob_density(x, y, cmap=cm.get_cmap('summer'), xscale='log', yscale='log', xParam=np.sqrt(2), yParam=np.sqrt(2), ax=None, use_gridspec=False, plot_mean=True): """ Plots the conditional probability density. (P(y|x)). See plot_counts for explanations of the input variables. """ fig, ax, cax = _get_fig_ax_and_colorbar_ax(xscale, yscale, ax=ax) X, Y, counts_matrix, bin_centers, means, Bins2D = \ _get_count_data_to_plot(x, y, xscale, yscale, xParam, yParam) # count vals for each x-bin x_counts = np.sum(counts_matrix, axis=0) # normalize the counts for each x-bin by the total number norm_x_counts_mat = counts_matrix / x_counts[None, :] # to get the prob density, normalize by the bin widths cond_prob = norm_x_counts_mat / Bins2D.widths[1][:, None] im = ax.pcolor(X, Y, cond_prob, cmap=cmap, norm=colors.LogNorm()) if plot_mean: ax.plot(bin_centers, means, "go-") cbar = fig.colorbar(im, cax, ax=ax, use_gridspec=use_gridspec, orientation='vertical', format=LogFormatterMathtext()) return fig, ax, cbar, im
def show_beta(x, data, z, fname="attbsc.png", label="Attenuated Backscatter coefficient", maxalt=0.0, maxdays=0.0): x_low, data_low, z_low = resampling(x, data, z, maxalt, maxdays) data_low = np.ma.masked_invalid(data_low) levs1 = [i * 1E-4 for i in [1, 2, 4, 6, 8, 10]] levs2 = [i * 1E-3 for i in [1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3]] levs3 = [i * 1E-3 for i in [4, 5, 6]] levs4 = [i * 1E-3 for i in [7, 8, 9, 10, 20, 40, 60, 80, 100]] my_cmap, my_norm = build_palette4(levs1, levs2, levs3, levs4) auto_tick = maxdays > 12 fig, ax = get_figure(auto_tick) CS = ax.pcolormesh( x_low, z_low, data_low, cmap=my_cmap, norm=my_norm, ) my_ticks = np.array([1E-4, 1E-3, 1E-2, 1E-1]) cbar = plt.colorbar( CS, ticks=my_ticks, extend='both', format=LogFormatterMathtext(), orientation='horizontal', aspect=30, shrink=0.6, ) ax.set_title(label + r" $[/sr \, /km]$") ax.patch.set(hatch='/', edgecolor='black') plt.xlabel('') plt.ylabel('Height AGL [km]') fig.savefig(fname, bbox_inches='tight', dpi=150)
def update_ticks(axes, coord, components, is_log): """ Changes the axes to have the proper tick formatting based on the type of component. :param axes: A matplotlib axis object to alter :param coord: 'x' or 'y' :param components: A list() of components that are plotted along this axis :param is_log: Boolean for log-scale. :kwarg max_categories: The maximum number of categories to display. :return: None or #categories if components is Categorical """ if coord == 'x': axis = axes.xaxis elif coord == 'y': axis = axes.yaxis else: raise TypeError("coord must be one of x,y") is_cat = all(comp.categorical for comp in components) if is_log: axis.set_major_locator(LogLocator()) axis.set_major_formatter(LogFormatterMathtext()) elif is_cat: all_categories = np.empty((0, ), dtype=np.object) for comp in components: all_categories = np.union1d(comp.categories, all_categories) locator = MaxNLocator(10, integer=True) locator.view_limits(0, all_categories.shape[0]) format_func = partial(tick_linker, all_categories) formatter = FuncFormatter(format_func) axis.set_major_locator(locator) axis.set_major_formatter(formatter) return all_categories.shape[0] else: axis.set_major_locator(AutoLocator()) axis.set_major_formatter(ScalarFormatter())
def update_ticks(axes, coord, kinds, is_log, categories, projection='rectilinear'): """ Changes the axes to have the proper tick formatting based on the type of component. Returns `None` or the number of categories if components is Categorical. Parameters ---------- axes : `~matplotlib.axes.Axes` A matplotlib axis object to alter coord : { 'x' | 'y' } The coordinate axis on which to update the ticks components : iterable A list of components that are plotted along this axis if_log : boolean Whether the axis has a log-scale projection: str The name of the matplotlib projection for the axes object. Defaults to 'rectilinear'. Currently only the scatter viewer supports different projections. """ # Short circuit the full-sphere projections if projection in ['aitoff', 'hammer', 'mollweide', 'lambert']: return if coord == 'x': axis = axes.xaxis elif coord == 'y': axis = axes.yaxis else: raise TypeError("coord must be one of x,y") is_cat = 'categorical' in kinds is_date = 'datetime' in kinds if is_date: loc = AutoDateLocator() fmt = AutoDateFormatter(loc) axis.set_major_locator(loc) axis.set_major_formatter(fmt) elif is_log: axis.set_major_locator(LogLocator()) axis.set_major_formatter(LogFormatterMathtext()) elif is_cat: locator = MaxNLocator(10, integer=True) locator.view_limits(0, categories.shape[0]) format_func = partial(tick_linker, categories) formatter = FuncFormatter(format_func) axis.set_major_locator(locator) axis.set_major_formatter(formatter) # Have to treat the theta axis of polar plots differently elif projection == 'polar' and coord == 'x': axis.set_major_locator(ThetaLocator(AutoLocator())) axis.set_major_formatter(ThetaFormatter()) else: axis.set_major_locator(AutoLocator()) axis.set_major_formatter(ScalarFormatter())
def show(self, location='right', width=0.2, pad=0.05, ticks=None, labels=True, log_format=False, box=None, box_orientation='vertical', axis_label_text=None, axis_label_rotation=None, axis_label_pad=5): """ Show a colorbar on the side of the image. Parameters ---------- location : str, optional Where to place the colorbar. Should be one of 'left', 'right', 'top', 'bottom'. width : float, optional The width of the colorbar relative to the canvas size. pad : float, optional The spacing between the colorbar and the image relative to the canvas size. ticks : list, optional The position of the ticks on the colorbar. labels : bool, optional Whether to show numerical labels. log_format : bool, optional Whether to format ticks in exponential notation box : list, optional A custom box within which to place the colorbar. This should be in the form [xmin, ymin, dx, dy] and be in relative figure units. This overrides the location argument. box_orientation str, optional The orientation of the colorbar within the box. Can be 'horizontal' or 'vertical' axis_label_text str, optional Optional text label of the colorbar. """ self._base_settings['location'] = location self._base_settings['width'] = width self._base_settings['pad'] = pad self._base_settings['ticks'] = ticks self._base_settings['labels'] = labels self._base_settings['log_format'] = log_format self._base_settings['box'] = box self._base_settings['box_orientation'] = box_orientation self._base_settings['axis_label_text'] = axis_label_text self._base_settings['axis_label_rotation'] = axis_label_rotation self._base_settings['axis_label_pad'] = axis_label_pad if self._parent.image: if self._colorbar_axes: self._figure.delaxes(self._colorbar_axes) if box is None: divider = make_axes_locatable(self._parent.ax) if location == 'right': self._colorbar_axes = divider.new_horizontal( size=width, pad=pad, axes_class=maxes.Axes) orientation = 'vertical' elif location == 'top': self._colorbar_axes = divider.new_vertical( size=width, pad=pad, axes_class=maxes.Axes) orientation = 'horizontal' elif location == 'left': warnings.warn("Left colorbar not fully implemented") self._colorbar_axes = divider.new_horizontal( size=width, pad=pad, pack_start=True, axes_class=maxes.Axes) locator = divider.new_locator(nx=0, ny=0) self._colorbar_axes.set_axes_locator(locator) orientation = 'vertical' elif location == 'bottom': warnings.warn("Bottom colorbar not fully implemented") self._colorbar_axes = divider.new_vertical( size=width, pad=pad, pack_start=True, axes_class=maxes.Axes) locator = divider.new_locator(nx=0, ny=0) self._colorbar_axes.set_axes_locator(locator) orientation = 'horizontal' else: raise Exception("location should be one of: right/top") self._figure.add_axes(self._colorbar_axes) else: self._colorbar_axes = self._figure.add_axes(box) orientation = box_orientation if log_format: format = LogFormatterMathtext() else: format = None self._colorbar = self._figure.colorbar(self._parent.image, cax=self._colorbar_axes, orientation=orientation, format=format, ticks=ticks) if axis_label_text: if axis_label_rotation: self._colorbar.set_label(axis_label_text, rotation=axis_label_rotation) else: self._colorbar.set_label(axis_label_text) if location == 'right': for tick in self._colorbar_axes.yaxis.get_major_ticks(): tick.tick1line.set_visible(True) tick.tick2line.set_visible(True) tick.label1.set_visible(False) tick.label2.set_visible(labels) self._colorbar_axes.yaxis.set_label_position('right') self._colorbar_axes.yaxis.labelpad = axis_label_pad elif location == 'top': for tick in self._colorbar_axes.xaxis.get_major_ticks(): tick.tick1line.set_visible(True) tick.tick2line.set_visible(True) tick.label1.set_visible(False) tick.label2.set_visible(labels) self._colorbar_axes.xaxis.set_label_position('top') self._colorbar_axes.xaxis.labelpad = axis_label_pad elif location == 'left': for tick in self._colorbar_axes.yaxis.get_major_ticks(): tick.tick1line.set_visible(True) tick.tick2line.set_visible(True) tick.label1.set_visible(labels) tick.label2.set_visible(False) self._colorbar_axes.yaxis.set_label_position('left') self._colorbar_axes.yaxis.labelpad = axis_label_pad elif location == 'bottom': for tick in self._colorbar_axes.xaxis.get_major_ticks(): tick.tick1line.set_visible(True) tick.tick2line.set_visible(True) tick.label1.set_visible(labels) tick.label2.set_visible(False) self._colorbar_axes.xaxis.set_label_position('bottom') self._colorbar_axes.xaxis.labelpad = axis_label_pad else: warnings.warn( "No image is shown, therefore, no colorbar will be plotted")
def set_default_locators_and_formatters(self, axis): axis.set_major_locator(HlogMajorLocator()) axis.set_major_formatter(LogFormatterMathtext(10)) axis.set_minor_locator(HlogMinorLocator()) axis.set_minor_formatter(NullFormatter())
def plot(self, Lmax=True, Lpp=False, Kp=True, Dst=True, clims=[0, 10], title=None, values=None): """ Create a summary plot of the RadBelt object distribution function. For reference, the last closed drift shell, Dst, and Kp are all included. These can be disabled individually using the corresponding Boolean kwargs. The clims kwarg can be used to manually set the color bar range. To use, set it equal to a two-element list containing minimum and maximum Log_10 value to plot. Default action is to use [0,10] as the log_10 of the color range. This is good enough for most applications. The title of the top most plot defaults to 'Summary Plot' but can be customized using the title kwarg. The figure object and all three axis objects (PSD axis, Dst axis, and Kp axis) are all returned to allow the user to further customize the plots as necessary. If any of the plots are excluded, None is returned in their stead. Examples ======== >>> rb.plot(Lmax=False, Kp=False, clims=[2,10], title='Good work!') This command would create the summary plot with a color bar range of 100 to 10^10. The Lmax line and Kp values would be excluded. The title of the topmost plot (phase space density) would be set to 'Good work!'. """ import matplotlib.pyplot as p from matplotlib.colors import LogNorm from matplotlib.ticker import (LogLocator, LogFormatter, LogFormatterMathtext) import pdb # debugging command #pdb.set_trace() # test for default values if values is None: values = self.PSD # Initialize axis variables so that they can be returned, # even if not used. ax1 = None ax2 = None ax3 = None fig = p.figure() fig.subplots_adjust(left=0.10, right=0.999, top=0.92) # PLOT PSD if Kp or Dst: ax1 = p.subplot(2, 1, 1) else: ax1 = p.subplot(1, 1, 1) # Plot phase space density, masking out values of 0. map = ax1.pcolorfast(self.ticks.eDOY, self.Lgrid, np.where(values > 0.0, self.PSD, 10.0**-39), vmin=10.0**clims[0], vmax=10.0**clims[1], norm=LogNorm()) ax1.set_ylabel('L*') if title is not None: ax1.set_title(title) # Add color bar. cbar = p.colorbar(map, pad=0.01, shrink=.85, ticks=LogLocator(), format=LogFormatterMathtext()) cbar.set_label('Phase Space Density') # add Lmax line if Lmax: p.plot(self.ticks.eDOY, self.params['Lmax'], 'w') # Minimize time range. ax1.set_xlim([self.ticks.eDOY[0], self.ticks.eDOY[-1]]) # Finally, save the position of the plot to make next plot match. pos = ax1.get_position() if Dst is True: ax2 = p.subplot(2, 1, 2) pos2 = ax2.get_position() pos2.x1 = pos.x1 ax2.set_position(pos2) ax2.plot(self.ticks.eDOY, self.params['Dst'], color='r') ax2.set_xlabel('DOY in ' + str(self.ticks.UTC[0].year)) ax2.set_ylabel('Dst', color='r') for tl in ax2.get_yticklabels(): tl.set_color('r') ax2.set_xlim([self.ticks.eDOY[0], self.ticks.eDOY[-1]]) if Kp is True: if Dst is True: p.subplot(2, 1, 2) ax3 = p.twinx() else: ax3 = p.subplot(2, 1, 2) pos3 = ax3.get_position() pos3.x1 = pos.x1 ax3.set_position(pos3) ax3.plot(self.ticks.eDOY, self.params['Kp'], 'k:') if Dst is True: ax3.yaxis.tick_right() ax3.set_xlabel('DOY in ' + str(self.ticks.UTC[0].year)) ax3.set_ylabel('Kp') ax3.set_xlim([self.ticks.eDOY[0], self.ticks.eDOY[-1]]) #p.show() return fig, ax1, ax2, ax3
def plot_obs(self, Lmax=True, Lpp=False, Kp=True, Dst=True, clims=[0, 10], title=None, values=None): """ Create a summary plot of the observations. For reference, the last closed drift shell, Dst, and Kp are all included. These can be disabled individually using the corresponding boolean kwargs. The clims kwarg can be used to manually set the color bar range. To use, set it equal to a two-element list containing minimum and maximum Log_10 value to plot. Default action is to use [0,10] as the log_10 of the color range. This is good enough for most applications. The title of the top most plot defaults to 'Summary Plot' but can be customized using the title kwarg. The figure object and all three axis objects (PSD axis, Dst axis, and Kp axis) are all returned to allow the user to further customize the plots as necessary. If any of the plots are excluded, None is returned in their stead. Examples ======== >>> rb.plot_obs(Lmax=False, Kp=False, clims=[2,10], title='Observations Plot') This command would create the summary plot with a color bar range of 100 to 10^10. The Lmax line and Kp values would be excluded. The title of the topmost plot (phase space density) would be set to 'Good work!'. """ import spacepy.data_assimilation import matplotlib.pyplot as p from matplotlib.colors import LogNorm from matplotlib.ticker import (LogLocator, LogFormatter, LogFormatterMathtext) import pdb # debugging command #pdb.set_trace() # test for default values if values is None: values = self.PSDdata # Initialize axis variables so that they can be returned, # even if not used. ax1 = None ax2 = None ax3 = None fig = p.figure() fig.subplots_adjust(left=0.10, right=0.999, top=0.92) # compute time-window average observation value y = np.array([], dtype=float) Lobs = np.array([], dtype=float) eDOYobs = np.array([], dtype=float) nTAI = len(self.ticks) # time loop for i, Tnow, Tfut in zip( np.arange(nTAI - 1) + 1, self.ticks[:-1], self.ticks[1:]): if len(values[i - 1]) > 0: # get observations for time window ]Tnow-Twindow,Tnow] Lobs_tmp, y_tmp = spacepy.data_assimilation.average_window( values[i - 1], self.Lgrid) y = np.append(y, y_tmp) Lobs = np.append(Lobs, Lobs_tmp) eDOYobs = np.append( eDOYobs, np.ones(len(y_tmp)) * self.ticks.eDOY[i - 1]) # PLOT PSDdata if Kp or Dst: ax1 = p.subplot(2, 1, 1) else: ax1 = p.subplot(1, 1, 1) # Plot phase space density observations. map = ax1.scatter(eDOYobs, Lobs, c=y, norm=LogNorm(), vmin=10.0**clims[0], vmax=10.0**clims[1], edgecolor='none') ax1.set_ylabel('L*') if title is not None: ax1.set_title(title) ax1.set_ylim(self.Lgrid[0], self.Lgrid[len(self.Lgrid) - 1]) # Add color bar. cbar = p.colorbar(map, pad=0.01, shrink=.85, ticks=LogLocator(), format=LogFormatterMathtext()) cbar.set_label('Phase Space Density') # add Lmax line if Lmax: p.plot(self.ticks.eDOY, self.params['Lmax'], 'w') # Minimize time range. ax1.set_xlim([self.ticks.eDOY[0], self.ticks.eDOY[-1]]) # Finally, save the position of the plot to make next plot match. pos = ax1.get_position() if Dst is True: ax2 = p.subplot(2, 1, 2) pos2 = ax2.get_position() pos2.x1 = pos.x1 ax2.set_position(pos2) ax2.plot(self.ticks.eDOY, self.params['Dst'], color='r') ax2.set_xlabel('DOY in ' + str(self.ticks.UTC[0].year)) ax2.set_ylabel('Dst', color='r') for tl in ax2.get_yticklabels(): tl.set_color('r') ax2.set_xlim([self.ticks.eDOY[0], self.ticks.eDOY[-1]]) if Kp is True: if Dst is True: p.subplot(2, 1, 2) ax3 = p.twinx() else: ax3 = p.subplot(2, 1, 2) pos3 = ax3.get_position() pos3.x1 = pos.x1 ax3.set_position(pos3) ax3.plot(self.ticks.eDOY, self.params['Kp'], 'k:') if Dst is True: ax3.yaxis.tick_right() ax3.set_xlabel('DOY in ' + str(self.ticks.UTC[0].year)) ax3.set_ylabel('Kp') ax3.set_xlim([self.ticks.eDOY[0], self.ticks.eDOY[-1]]) #p.show() return fig, ax1, ax2, ax3
def show_noise_plot(self,cfig): cdiag = self.diag cparams = cdiag['params'] vox_size = cparams['voxel_size'] name = cparams['name'] maxfreq = cparams['max_frequency'] N = self.M.shape[0] rad_cutoff = cparams.get('rad_cutoff', 1.0) rad = min(rad_cutoff,maxfreq*2.0*vox_size) startI = int((1-rad)*N/2)+1 endI = N/2 + int(rad*N/2)+1 imextent = [startI-(N+1.0)/2,endI-(N+1.0)/2,startI-(N+1.0)/2,endI-(N+1.0)/2] imextent = [e/(2.0*vox_size)/(N/2) for e in imextent] sigma_est = cparams['sigma'] sigma_mle = np.sqrt(cdiag['sigma2_mle']) train_sigma_est = np.sqrt(cdiag['train_sigma2_est']).reshape((N,N)) test_sigma_est = np.sqrt(cdiag['test_sigma2_est']).reshape((N,N)) showsigma = isinstance(sigma_est,np.ndarray) vmin = min([np.min(sigma_est),sigma_mle[startI:endI,startI:endI].min()]) vmax = max([np.max(sigma_est),sigma_mle[startI:endI,startI:endI].max()]) imshow_kws = { 'interpolation':'nearest', \ 'vmin':vmin, 'vmax':vmax, 'extent':imextent, \ 'norm':LogNorm(vmin=vmin, vmax=vmax) } cbaxs = [] plt.figure(cfig.number) plt.clf() plt.subplot(2,1,1) raps = np.sqrt(cryoem.rotational_average(train_sigma_est**2)) fs = np.linspace(0,(len(raps)-1)/(N/2.0)/(2.0*vox_size),len(raps)) plt.plot(fs,raps,label='Training RMSE') raps = np.sqrt(cryoem.rotational_average(test_sigma_est**2)) fs = np.linspace(0,(len(raps)-1)/(N/2.0)/(2.0*vox_size),len(raps)) plt.plot(fs,raps,label='Testing RMSE') plt.legend() plt.grid() if showsigma: cbaxs.append(plt.subplot(2,2,3)) else: cbaxs.append(plt.subplot(2,1,2)) im = plt.imshow(sigma_mle[startI:endI,startI:endI], **imshow_kws) plt.title('Freq RMSE (MLE)') if showsigma: sigma_est = sigma_est.reshape((N,N)) cbaxs.append(plt.subplot(2,2,4)) im = plt.imshow(sigma_est[startI:endI,startI:endI], **imshow_kws) plt.title('Coloured Noise Std Dev') plt.subplot(2,1,1) if showsigma: raps = np.sqrt(cryoem.rotational_average(sigma_est**2)) fs = np.linspace(0,(len(raps)-1)/(N/2.0)/(2.0*vox_size),len(raps)) else: raps = [sigma_est,sigma_est] fs = [fs[0],fs[-1]] plt.plot(fs,raps,label='Noise Std Dev') plt.xlim((0,rad/(2.0*vox_size))) plt.yscale('log',basey=2) plt.legend() plt.title(name + ' Noise Levels') plt.colorbar(im, ax=cbaxs, ticks=LogLocator(base=2), format=LogFormatterMathtext(base=2))
def _make_contour(self, **kwargs): """ method for generalizanting contour and colorbar plotting kwargs cax = matplotlib.Axes used to print colorbar. Default use make_axes_locatable """ Z = self.data.copy() if self.kwargs['vlim'] is not None: vmin, vmax = self.kwargs['vlim'] else: vmin, vmax = [Z.min().min(), Z.max().max()] #np.nanpercentile(Z,[2.5,97.5]) # print vmin, vmax colorbarKwd = {'extend': 'both'} contourKwd = { 'cmap': self.colormap } #'levels':np.linspace(vmin,vmax,100), if self.kwargs['colorbarKind'] == 'Linear': contourKwd['norm'] = Normalize(vmin, vmax) elif self.kwargs['colorbarKind'] == 'Anomaly': contourKwd['norm'] = MidpointNormalize(midpoint=0., vmin=vmin, vmax=vmax) colorbarKwd['format'] = self.kwargs['format'] elif self.kwargs['colorbarKind'] == 'Log': Z.mask(Z <= 0, inplace=True) if self.kwargs['vlim'] is None: vmin, vmax = [Z.min().min(), Z.max().max()] # np.nanpercentile(Z,[1,99]))) # Z[Z < vmin] = vmin # Z[Z > vmax] = vmax contourKwd['norm'] = LogNorm(vmin, vmax) print vmin, vmax minorTicks = np.hstack( [np.arange(1, 10, 1) * log for log in np.logspace(-2, 16, 19)]) minorTicks = minorTicks[(minorTicks >= vmin) & (minorTicks <= vmax)] colorbarKwd.update( dict(format=LogFormatterMathtext(10), ticks=LogLocator(10))) args = (self.x, self.y, Z) cf = self.axes[0].pcolormesh(*args, **contourKwd) # cf = ax.contourf(X,Y,Z,**contourKwd) #extend='both') if 'cax' not in kwargs.keys(): divider = make_axes_locatable(self.axes[0]) cax = divider.append_axes("right", size="3%", pad='1%') else: cax = kwargs['cax'] cbar = plt.colorbar(cf, cax=cax, **colorbarKwd) cbar.set_label(self.kwargs['cbarLabel'], weight='bold') if self.kwargs['colorbarKind'] == 'Log': cbar.ax.yaxis.set_ticks(cf.norm(minorTicks), minor=True) cbar.ax.tick_params(which='minor', width=1, length=4) cbar.ax.tick_params(which='major', width=1, length=6) # ax.yaxis.set_minor_locator(LogLocator(10,subs=np.arange(2,10))) self.kwargs['colorbarKind'] = 'Linear' return cf, cbar
class ThorntonParameterStudy(object): """ Holds the reduced and fitted results for a full parameter study in the style of Thornton et al. (1998) """ formatter = LogFormatterMathtext(labelOnlyBase=False) locator = LogLocator(subs=np.logspace(0, 1, num=5, endpoint=False)[1:]) metallicity_fit_label = r"Fit($Z$)" number_density_fit_label = r"Fit($n$)" simultaneous_fit_label = r"Fit($Z$, $n$)" my_label = "our simulations" Thornton_label = "Thornton et al. (1998)" def __init__(self, data_dir, with_cooling=True, verbose=False): self.data_dir = data_dir columns = [ "background_density", "background_temperature", "metallicity", "number_density", "E_R_kin", "E_R_int", "E_R_tot", "t_f", "R_shock" ] overview_filenames = glob.glob(os.path.join(data_dir, "*_overview.dat")) overviews = [Overview(filename) for filename in overview_filenames] overviews = [ overview for overview in overviews if overview.with_cooling == with_cooling ] self.df = pd.DataFrame(index=range(len(overviews)), columns=columns, dtype=float) for i, overview in enumerate(overviews): run_summary = RunSummary(data_dir=data_dir, id=overview.id) cooling_index = np.argmin( np.abs(run_summary.times - run_summary.t_f)) self.df.loc[i] = [ overview.background_density, overview.background_temperature, overview.metallicity, overview.background_density / m_proton, run_summary.E_R_kin[cooling_index], run_summary.E_R_int[cooling_index], run_summary.E_R_tot[cooling_index], run_summary.t_f, run_summary.R_shock[cooling_index] ] if verbose: print() print("metallicity: ", overview.metallicity) print("background density:", overview.background_density) print("cooling index: ", cooling_index) ## add models sorted_args = np.argsort(self.df.number_density) self.slice_by_metallicity = sorted_args[np.isclose( self.df.metallicity[sorted_args], metallicity_solar, atol=0)] sorted_args = np.argsort(self.df.metallicity) self.slice_by_number_density = sorted_args[np.isclose( self.df.number_density[sorted_args], .1 * 1.33, atol=0)] self.thornton_models = dict() self.simultaneous_models = dict() self.number_density_models = dict() self.metallicity_models = dict() for energy in energies: self.thornton_models[energy] = PublishedModel(energy) self.simultaneous_models[energy] = SimultaneousModel( self.df.number_density, self.df.metallicity, self.df["E_R_{0}".format(energy)]) self.number_density_models[energy] = NumberDensityModel( self.df.number_density[self.slice_by_metallicity], self.df["E_R_{0}".format(energy)][self.slice_by_metallicity]) self.metallicity_models[energy] = MetallicityModel( self.df.metallicity[self.slice_by_number_density], self.df[ "E_R_{0}".format(energy)][self.slice_by_number_density]) if verbose: for energy in energies: print("") print("Thornton Model, E_R_{0}:".format(energy)) self.thornton_models[energy].print_model() print("========================") for energy in energies: print("") print("simultaneous model, E_R_{0}".format(energy)) self.simultaneous_models[energy].print_model() print("========================") for energy in energies: print("") print("number_density_fit_E_R_{0}: ".format(energy)) self.number_density_models[energy].print_model() print("========================") for energy in energies: print("") print("metallicity_fit_E_R_{0}: ".format(energy)) self.metallicity_models[energy].print_model() def plot_one_number_density(self, energy="tot"): mask = self.slice_by_metallicity y_variable = "E_R_" + energy self.number_density_models[energy].print_model() plt.figure() plt.scatter(self.df.number_density[mask], self.df[y_variable][mask], label=self.my_label, s=100, marker="o") plt.plot(self.df.number_density[mask], self.thornton_models[energy](self.df.metallicity[mask], self.df.number_density[mask]), label=self.Thornton_label) plt.plot(self.df.number_density[mask], self.number_density_models[energy]( self.df.number_density[mask]), label=self.number_density_fit_label, linestyle="dashed") plt.plot(self.df.number_density[mask], self.simultaneous_models[energy]( self.df.metallicity[mask], self.df.number_density[mask]), label=self.simultaneous_fit_label, linestyle="dashed") plt.legend(loc="best") plt.xscale("log") plt.yscale("log") plt.xlabel(r"$n$ $[\mathrm{cm}^{-3}]$") plt.ylabel( r"$E_{{R, \mathrm{{ {0} }} }}$ $[\mathrm{{ergs}}]$".format(energy)) xmin, xmax = plt.xlim() plt.xlim(xmin / 5, xmax * 5) ax = plt.gca() ax.yaxis.set_minor_formatter(self.formatter) ax.yaxis.set_minor_locator(self.locator) def plot_one_metallicity(self, energy="tot"): mask = self.slice_by_number_density y_variable = "E_R_" + energy self.metallicity_models[energy].print_model() plt.figure() plt.scatter(self.df.metallicity[mask] / metallicity_solar, self.df[y_variable][mask], label=self.my_label, s=100, marker="o") plt.plot(self.df.metallicity[mask] / metallicity_solar, self.thornton_models[energy](self.df.metallicity[mask], self.df.number_density[mask]), label=self.Thornton_label) plt.plot(self.df.metallicity[mask] / metallicity_solar, self.metallicity_models[energy](self.df.metallicity[mask]), label=self.metallicity_fit_label, linestyle="dashed") plt.plot(self.df.metallicity[mask] / metallicity_solar, self.simultaneous_models[energy]( self.df.metallicity[mask], self.df.number_density[mask]), label=self.simultaneous_fit_label, linestyle="dashed") plt.legend(loc="best") plt.xscale("log") plt.yscale("log") plt.xlabel(r"$Z / Z_\odot$") plt.ylabel(r"$E_{{R, {0} }}$ $[\mathrm{{ergs}}]$".format(energy)) xmin, xmax = plt.xlim() plt.xlim(xmin / 5, xmax * 5) ax = plt.gca() ax.yaxis.set_minor_formatter(self.formatter) ax.yaxis.set_minor_locator(self.locator)
pl = ( ggplot(across_samples[across_samples.n > 6], aes(x="tradeoff", y="rho")) + geom_hline(yintercept=0, linetype="dashed") + geom_point() + geom_line(aes(group=1)) + labs(x="tradeoff", y="Pearson rho")) pl.save("figures/across_sample_correlations.svg", width=5, height=5) across_samples.to_csv("data/correlation_all.csv") rates["log_rates"] = np.log10(rates.growth_rate) rates.loc[rates.growth_rate <= 0, "log_rates"] = -16 fig, axes = jp.joyplot(rates, by="tradeoff", column="log_rates", color="cornflowerblue") lf = LogFormatterMathtext() xax = axes[-1].xaxis xax.set_ticklabels(lf(10.0**ex) for ex in xax.get_ticklocs()) plt.xlabel("growth rate [1/h]") plt.ylabel("tradeoff") plt.savefig("figures/dists.svg") plt.close() non_zero = (rates.groupby([ "id", "tradeoff" ]).apply(lambda df: (df.growth_rate > 1e-6).sum() / df.shape[0]).reset_index( name="non_zero")) pl = (ggplot(non_zero, aes(x="tradeoff", y="non_zero")) + geom_boxplot(outlier_color="none") + geom_jitter(width=0.15, height=0, alpha=0.5, stroke=0) +
def plot_conf_sampled_xval(params, ax=None, LAMBDA=1, xlim=(1.01, 1000), probs=(2.5, 97.5), line=False, **kwargs): """ plot uncertainty of GEV given the sampled parameters Parameters ---------- params : array_like The three parameters of a GEV: alpha, chi, k ax : a matplotlib axes object or None If no ax is given, uses plt.gca() to create a new one. xlim : two-element list defines the x limit of the plotted line probs : tuple probability for which to plot the uncertainty bounds line : bool if true uses line else fill_between kwargs : named arguments passed to the plot/ fill_between plotting function Returns ------- handle : handle of the plot/ fill_between object """ if not line and len(probs) != 2: msg = "Need exactly two 'probs' when plotting 'fill_between'" raise ValueError(msg) ax, plot_LAMBDA, kwargs = __prepare_plot_xval__(ax, LAMBDA, **kwargs) xmin = np.amax([1.00001 / LAMBDA, xlim[0]]) xmax = xlim[1] xlim = np.log(np.asarray([xmin, xmax])) t_fit = np.linspace(xlim[0], xlim[1]) n_samples = params.shape[0] n_plot_points = t_fit.shape[0] sampled = np.empty(shape=(n_samples, n_plot_points)) ft = F_of_T(T_of_Y(t_fit, plot_LAMBDA), LAMBDA) for i, param in enumerate(params): y_fit = XGEV(ft, *param) sampled[i, :] = y_fit y = np.percentile(sampled, probs, axis=0) if line: kwargs.setdefault('lw', 0.5) y = y.transpose() return ax.semilogx(np.e**t_fit, y, basex=10, **kwargs) else: y1 = y[0, :] y2 = y[1, :] # add some options for the plot if not set already kwargs.setdefault('alpha', 0.15) kwargs.setdefault('zorder', -1000) kwargs.setdefault('lw', 0) ax.set_xscale('log', basex=10) ax.xaxis.set_major_formatter(LogFormatterMathtext()) return ax.fill_between(np.e**t_fit, y1, y2, **kwargs)
def plot_flux(self, timelim=None): ''' blerg ''' # Set default time limit if none given. if not timelim: timelim = [self.time[0], self.time[-1]] fig = plt.figure(figsize=(8.38, 10.88)) plt.subplots_adjust(hspace=0.30, wspace=0.36, top=0.93, right=0.94) add_orbits(self, title='Flux by Energy for '+ \ self.filename[0:self.filename.rfind('.')], timelim = timelim) # Flux (all pitch angles) vs energy self.create_omniflux() # Things shared by both plots. timelab = 'Universal Time from %s' % self.time[0].isoformat() energlab = 'Log$_{10}$ Energy ($KeV$)' fluxlab = '$cm^{-2}s^{-1}KeV^{-1}$' time = date2num(self.time) egrid = self.data['energy_grid'] yrng = [0.5, max(egrid)] Mtick, mtick, fmt = smart_timeticks(timelim) maxFlux = max([self.omniH.max(), self.omniO.max()]) fx1 = fig.add_subplot(3, 1, 2) fx1.set_yscale('log') flxH = fx1.pcolor(time, egrid, self.omniH.transpose(), norm=LogNorm(), vmin=1, vmax=maxFlux) # Nudge position pos = fx1.get_position() pos.x1 = pos.x1 + 0.1 pos.y0 = pos.y0 + 0.05 pos.y1 = pos.y1 + 0.05 fx1.set_position(pos) cbar1 = plt.colorbar(flxH, pad=0.01, shrink=.85, ticks=LogLocator(), format=LogFormatterMathtext()) cbar1.set_label(fluxlab) fx1.set_xlim(timelim) fx1.set_ylim(yrng) fx1.xaxis.set_major_locator(Mtick) fx1.xaxis.set_minor_locator(mtick) fx1.xaxis.set_major_formatter(fmt) fx1.set_ylabel(energlab) fx1.set_title('Omnidirectional H$^{+}$ Flux') fx2 = plt.subplot(3, 1, 3) fx2.set_yscale('log') flxO = fx2.pcolor(time, egrid, self.omniO.transpose(), norm=LogNorm(), vmin=1, vmax=maxFlux) # Nudge position pos = fx2.get_position() pos.x1 = pos.x1 + 0.1 pos.y0 = pos.y0 + 0.05 pos.y1 = pos.y1 + 0.05 fx2.set_position(pos) fx2.set_ylim([0.5, 500]) cbar2 = plt.colorbar(flxO, pad=0.01, shrink=.85, ticks=LogLocator(), format=LogFormatterMathtext()) cbar2.set_label(fluxlab) fx2.set_xlim(timelim) fx2.set_ylim(yrng) fx2.xaxis.set_major_locator(Mtick) fx2.xaxis.set_minor_locator(mtick) newfmt = FuncFormatter(self.orbit_formatter) fx2.xaxis.set_major_formatter(newfmt) fx2.set_xlabel(timelab) fx2.set_ylabel(energlab) fx2.set_title('Omnidirectional O$^{+}$ Flux') #fig.show() return fig