def _set_zaxis_ticks(ax, lightcone, zticks, z_axis): if zticks != "distance": loc = AutoLocator() # Get redshift ticks. lc_z = lightcone.lightcone_redshifts if zticks == "redshift": coords = lc_z elif zticks == "frequency": coords = 1420 / (1 + lc_z) * un.MHz else: try: coords = getattr(lightcone.cosmo_params.cosmo, zticks)(lc_z) except AttributeError: raise AttributeError( "zticks '{}' is not a cosmology function.".format(zticks) ) zlabel = " ".join(z.capitalize() for z in zticks.split("_")) units = getattr(coords, "unit", None) if units: zlabel += " [{}]".format(str(coords.unit)) coords = coords.value ticks = loc.tick_values(coords.min(), coords.max()) if ticks.min() < coords.min() / 1.00001: ticks = ticks[1:] if ticks.max() > coords.max() * 1.00001: ticks = ticks[:-1] if coords[1] < coords[0]: ticks = ticks[::-1] if zticks == "redshift": z_ticks = ticks elif zticks == "frequency": z_ticks = 1420 / ticks - 1 else: z_ticks = [ z_at_value(getattr(lightcone.cosmo_params.cosmo, zticks), z * units) for z in ticks ] d_ticks = ( lightcone.cosmo_params.cosmo.comoving_distance(z_ticks).value - lightcone.lightcone_distances[0] ) getattr(ax, "set_{}ticks".format(z_axis))(d_ticks) getattr(ax, "set_{}ticklabels".format(z_axis))(ticks) else: zlabel = "Line-of-Sight Distance [Mpc]" return zlabel
def set_xticks(self, ticks=None): if ticks: super(Radialplot, self).set_xticks(ticks) else: if self.transform == "linear": loc = AutoLocator() ticks = loc.tick_values(0., self.max_x) ticks2 = loc.tick_values(min(self.sez), max(self.sez)) ticks2 = ticks2[::-1] ticks2[-1] = min(self.sez) super(Radialplot, self).set_xticks(1.0 / ticks2) labels = ["{0:5.1}".format(val) for val in ticks2] self.xaxis.set_ticklabels(labels) self.spines["bottom"].set_bounds(0., 1. / ticks2[-1]) self.set_xlabel(r'$\sigma$') else: loc = MaxNLocator(5) ticks = loc.tick_values(0., self.max_x) super(Radialplot, self).set_xticks(ticks) self.spines["bottom"].set_bounds(ticks[0], ticks[-1])
def add_colorbar(ax, vmin, vmax, cmap, label='', title='', orientation='horizontal', scale='linear'): import draw.my_script as my from matplotlib.ticker import AutoLocator, LogLocator ax.set_xscale(scale) ax.set_xlim((vmin, vmax)) tickvals = ax.xaxis.get_ticklocs() ax_inv_transform = ax.transAxes.inverted().transform ax_transData = ax.transData.transform ticks = [ax_inv_transform(ax_transData(np.array([(tic, 0.1)])))[0][0] for tic in tickvals] #ticklabels = [my.num2str4fig(tv, scientific=False) for tv in tickvals] ticklabels = [str(tv) for tv in tickvals] ticks, ticklabels = zip(*filter(lambda x: 0.0<=x[0]<=1.0, zip(ticks, ticklabels))) if len(ticks) < 10: if scale == 'linear': L = AutoLocator() else: L = LogLocator(base=10, subs=np.linspace(1.0,10.0,10)) tkvals = L.tick_values(vmin, vmax) minticks = filter(lambda x: 0<=x<=1, [ax_inv_transform(ax_transData(np.array([(tic,0.1)])))[0][0] for tic in tkvals]) ax.set_xscale('linear') cbar = mpl.colorbar.ColorbarBase(ax, ticklocation='auto', cmap=cmap, ticks=ticks, label=label, norm=mpl.colors.Normalize(vmin=0, vmax=1, clip=False), orientation=orientation) cbar.solids.set_rasterized(True) cbar.set_ticklabels(ticklabels) if orientation == 'horizontal': ax_working = cbar.ax.xaxis else: ax_working = cbar.ax.yaxis if len(ticks) < 10: ax_working.set_ticks(minticks, minor=True) ax_working.set_tick_params(which='minor', length=4) ax_working.set_tick_params(which='major', length=7) ax.set_title(title) return
class ResponseLocator(Locator): def __init__(self, locs, nbins=None, numticks=None): self.locs = np.asarray(locs) self.numticks = numticks self.auto_locator = AutoLocator() self.nbins = nbins def set_params(self, nbins=None, numticks=None): """Set parameters within this locator.""" if nbins is not None: self.nbins = nbins if numticks is not None: self.numticks = numticks def __call__(self): vmin, vmax = self.axis.get_view_interval() return self.tick_values(vmin, vmax) @property def numticks(self): # Old hard-coded default. return self._numticks if self._numticks is not None else 11 @numticks.setter def numticks(self, numticks): self._numticks = numticks def tick_values(self, vmin, vmax): """ Return the locations of the ticks. .. note:: Because the values are fixed, vmin and vmax are not used in this method. """ if vmax < 3: return np.round(self.auto_locator.tick_values(vmin, vmax), 4) else: if self.nbins is None: return self.locs step = max(int(np.ceil(len(self.locs) / self.nbins)), 1) ticks = self.locs[::step] for i in range(1, step): ticks1 = self.locs[i::step] if np.abs(ticks1).min() < np.abs(ticks).min(): ticks = ticks1 return self.raise_if_exceeds(ticks)
def tick_values(self, vmin, vmax): ''' The idea of this function is to determine tick locations in psu that will be neat numbers once converted and labeled as EC. The way we do this is to convert the bounds vmin and vmax to ec and using AutoLocator to come up with neat EC values. Then we back convert these values to psu. ''' # vmin and vmax are in psu # Determine "neat values of EC for tick locations vmin_ec = psu_ec_25c(vmin) if vmin >= 0. else -psu_ec_25c(abs(vmin)) vmax_ec = psu_ec_25c(vmax) if vmax >= 0. else -psu_ec_25c(abs(vmax)) # Determine "neat" values of EC for tick locations auto_ticks = AutoLocator.tick_values(self, vmin_ec, vmax_ec) # Now determine the locations in psu, since the axis is psu for # locations and EC for labeling. convert_ticks = [x for x in ec_psu_25c(auto_ticks) if x >= 0. and x <= 35.] return convert_ticks
def tick_values(self, vmin, vmax): return [ v + offset for v in AutoLocator.tick_values(self, vmin, vmax) ]
def tick_values(self, vmin, vmax): return [v + offset for v in AutoLocator.tick_values(self, vmin, vmax)]
def nice_bin_edges(x): l = AutoLocator() l.create_dummy_axis() return l.tick_values(x.min(), x.max())
def add_colorbar(ax, vmin, vmax, cmap, label='', title='', orientation='horizontal', scale='linear'): #import draw.my_script as my from matplotlib.ticker import AutoLocator, LogLocator ax.set_xscale(scale) ax.set_xlim((vmin, vmax)) tickvals = ax.xaxis.get_ticklocs() ax_inv_transform = ax.transAxes.inverted().transform ax_transData = ax.transData.transform ticks = [ ax_inv_transform(ax_transData(np.array([(tic, 0.1)])))[0][0] for tic in tickvals ] #ticklabels = [my.num2str4fig(tv, scientific=False) for tv in tickvals] ticklabels = [str(tv) for tv in tickvals] ticks, ticklabels = zip( *filter(lambda x: 0.0 <= x[0] <= 1.0, zip(ticks, ticklabels))) if len(ticks) < 10: if scale == 'linear': L = AutoLocator() else: L = LogLocator(base=10, subs=np.linspace(1.0, 10.0, 10)) tkvals = L.tick_values(vmin, vmax) minticks = filter(lambda x: 0 <= x <= 1, [ ax_inv_transform(ax_transData(np.array([(tic, 0.1)])))[0][0] for tic in tkvals ]) ax.set_xscale('linear') cbar = mpl.colorbar.ColorbarBase(ax, ticklocation='auto', cmap=cmap, ticks=ticks, label=label, norm=mpl.colors.Normalize(vmin=0, vmax=1, clip=False), orientation=orientation) cbar.solids.set_rasterized(True) cbar.set_ticklabels(ticklabels) if orientation == 'horizontal': ax_working = cbar.ax.xaxis else: ax_working = cbar.ax.yaxis if len(ticks) < 10: ax_working.set_ticks([_ for _ in minticks], minor=True) ax_working.set_tick_params(which='minor', length=4) ax_working.set_tick_params(which='major', length=7) ax.set_title(title) return