def _ticker(self):
'''
Return two sequences: ticks (colorbar data locations)
and ticklabels (strings).
'''
locator = self.locator
formatter = self.formatter
if locator is None:
if self.boundaries is None:
if isinstance(self.norm, colors.NoNorm):
nv = len(self._values)
base = 1 + int(nv / 10)
locator = ticker.IndexLocator(base=base, offset=0)
elif isinstance(self.norm, colors.BoundaryNorm):
b = self.norm.boundaries
locator = ticker.FixedLocator(b, nbins=10)
elif isinstance(self.norm, colors.LogNorm):
locator = ticker.LogLocator()
else:
locator = ticker.MaxNLocator()
else:
b = self._boundaries[self._inside]
locator = ticker.FixedLocator(b, nbins=10)
if isinstance(self.norm, colors.NoNorm):
intv = self._values[0], self._values[-1]
else:
intv = self.vmin, self.vmax
locator.create_dummy_axis(minpos=intv[0])
formatter.create_dummy_axis(minpos=intv[0])
locator.set_view_interval(*intv)
locator.set_data_interval(*intv)
formatter.set_view_interval(*intv)
formatter.set_data_interval(*intv)
b = np.array(locator())
ticks = self._locate(b)
inrange = (ticks > -0.001) & (ticks < 1.001)
ticks = ticks[inrange]
b = b[inrange]
formatter.set_locs(b)
ticklabels = [formatter(t, i) for i, t in enumerate(b)]
offset_string = formatter.get_offset()
return ticks, ticklabels, offset_string
python类LogNorm()的实例源码
def _ticker(self):
'''
Return the sequence of ticks (colorbar data locations),
ticklabels (strings), and the corresponding offset string.
'''
locator = self.locator
formatter = self.formatter
if locator is None:
if self.boundaries is None:
if isinstance(self.norm, colors.NoNorm):
nv = len(self._values)
base = 1 + int(nv / 10)
locator = ticker.IndexLocator(base=base, offset=0)
elif isinstance(self.norm, colors.BoundaryNorm):
b = self.norm.boundaries
locator = ticker.FixedLocator(b, nbins=10)
elif isinstance(self.norm, colors.LogNorm):
locator = ticker.LogLocator()
else:
locator = ticker.MaxNLocator()
else:
b = self._boundaries[self._inside]
locator = ticker.FixedLocator(b, nbins=10)
if isinstance(self.norm, colors.NoNorm):
intv = self._values[0], self._values[-1]
else:
intv = self.vmin, self.vmax
locator.create_dummy_axis(minpos=intv[0])
formatter.create_dummy_axis(minpos=intv[0])
locator.set_view_interval(*intv)
locator.set_data_interval(*intv)
formatter.set_view_interval(*intv)
formatter.set_data_interval(*intv)
b = np.array(locator())
ticks = self._locate(b)
inrange = (ticks > -0.001) & (ticks < 1.001)
ticks = ticks[inrange]
b = b[inrange]
formatter.set_locs(b)
ticklabels = [formatter(t, i) for i, t in enumerate(b)]
offset_string = formatter.get_offset()
return ticks, ticklabels, offset_string
def heatmap(X, Y, Z, ax=None, logy=True, cbar=True, hide_low=True,
cmap=default_cmap, fig_kws={}, cbar_kws={}, plot_kws={}, **kwargs):
"""Plot the heatmap of the particle size distribution.
"""
# Set the colorbar min and max based on the min and max of the values
cbar_min = kwargs.pop('cbar_min', Z.min() if Z.min() > 0.0 else 1.)
cbar_max = kwargs.pop('cbar_max', Z.max())
# Copy to avoid modifying original data
Z_plot = Z.copy()
if hide_low:
# Hide NaN values
Z_plot = nan_to_num(Z_plot)
# Increase values below cbar_min to cbar_min
below_min = Z_plot < cbar_min
Z_plot[below_min] = cbar_min
# Set the plot_kws
plot_kws = dict(dict(norm=LogNorm(vmin=cbar_min, vmax=cbar_max), cmap=cmap),
**plot_kws)
# Set the figure keywords
fig_kws = dict(dict(figsize=(16,8)), **fig_kws)
if ax is None:
plt.figure(**fig_kws)
ax = plt.gca()
# Plot the data as a pcolormesh
im = ax.pcolormesh(X, Y, Z_plot, **plot_kws)
# Set the ylim to match the data
ax.set_ylim([Y.min(), Y.max()])
# Set the axis to be log in the y-axis
if logy:
ax.semilogy()
ax.yaxis.set_major_formatter(ScalarFormatter())
ax.set_ylabel("$D_p \; [nm]$")
if cbar:
# Set the figure keywords
cbar_kws = dict(dict(label='$dN/dlogD_p \; [cm^{-3}]$'), **cbar_kws)
clb = plt.colorbar(im, **cbar_kws)
return ax
def plot_jet_image(
ax, image, vmin=1e-9, vmax=1e-2, cmap="jet", title="Intensity",
label_axes=True, visible_axes=False, show_colorbar=True, colorbar_inside=False):
"""Display jet image.
Args:
ax: matplotlib axes to plot on.
image: array representing image to plot.
vmin, vmax: min, max intensity values to plot.
"""
width, height = image.T.shape
dw, dh = 1./width, 1./height
if not (vmin is None) and not (vmax is None):
if vmin < 0:
norm = MidPointNorm(vmin=vmin, vmax=vmax)
ticks = None
else:
norm = LogNorm(vmin=vmin, vmax=vmax)
ticks = np.logspace(
np.log10(vmin), np.log10(vmax),
1 + np.log10(vmax) - np.log10(vmin))
else:
norm = None
ticks = None
p = ax.imshow(
image.T, extent=(-(1+dw), 1+dw, -(1+dh), 1+dh), origin='low',
interpolation='nearest', norm=norm, cmap=cmap)
if show_colorbar:
if colorbar_inside:
cax = ax.figure.add_axes([0.85, 0.08, 0.03, 0.82])
else:
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = plt.colorbar(p, cax=cax, ticks=ticks)
cbar.set_label(title, rotation=90, fontsize=18)
cbar.ax.tick_params(labelsize=12)
if colorbar_inside:
cbar.ax.yaxis.set_ticks_position('left')
if label_axes:
ax.set_xlabel(r'$x_1$', fontsize=18)
ax.set_ylabel(r'$x_2$', fontsize=18)
ax.tick_params(axis='both', which='major', labelsize=12)
else:
ax.axes.get_xaxis().set_ticks([])
ax.axes.get_yaxis().set_ticks([])
ax.axes.get_xaxis().set_visible(visible_axes)
ax.axes.get_yaxis().set_visible(visible_axes)
if visible_axes:
for spine in ['top', 'bottom', 'left', 'right']:
ax.spines[spine].set_linewidth(1)
ax.spines[spine].set_color('k')
else:
for spine in ['top', 'bottom', 'left', 'right']:
ax.spines[spine].set_visible(False)
ax.axes.grid(False)