Coverage for kwant/plotter.py : 81%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- # Copyright 2011-2017 Kwant authors. # # This file is part of Kwant. It is subject to the license terms in the file # LICENSE.rst found in the top-level directory of this distribution and at # http://kwant-project.org/license. A list of Kwant authors can be found in # the file AUTHORS.rst at the top-level directory of this distribution and at # http://kwant-project.org/authors.
This module provides iterators useful for any plotter routine, such as a list of system sites, their coordinates, lead sites at any lead unit cell, etc. If `matplotlib` is available, it also provides simple functions for plotting the system in two or three dimensions. """
# All matplotlib imports must be isolated in a try, because even without # matplotlib iterators remain useful. Further, mpl_toolkits used for 3D # plotting are also imported separately, to ensure that 2D plotting works even # if 3D does not. except ImportError: warnings.warn("3D plotting not available.", RuntimeWarning) has3d = False except ImportError: warnings.warn("matplotlib is not available, only iterator-providing " "functions will work.", RuntimeWarning) mpl_available = False
'interpolate_current', 'streamplot', 'sys_leads_sites', 'sys_leads_hoppings', 'sys_leads_pos', 'sys_leads_hopping_pos', 'mask_interpolate']
# TODO: Remove the following once we depend on matplotlib >= 1.4.1. global pre_1_4_matplotlib
warnings.warn("Matplotlib 1.4.0 has a bug that makes 3D plotting " "unusable (2D plotting is not affected). Please " "consider using a different version of matplotlib.", RuntimeWarning, stacklevel=2)
# Collections that allow for symbols and linewiths to be given in data space # (not for general use, only implement what's needed for plotter) else:
# Note: only works for aspect ratio 1! # 72.0 - there is 72 points in an inch self.reflen / self.figure.dpi) else: factor = 1
factor)
self.transforms = [matplotlib.transforms.Affine2D().scale(x) for x in sizes] else: [matplotlib.transforms.Affine2D().scale(x).get_matrix() for x in sizes])
# For the paths, use the data transformation but strip the # offset (will be added later with offsets) else: return Affine2D().scale(self.figure.dpi / 72.0)
# Note: only works for aspect ratio 1! self.figure.dpi * 72.0 * self.reflen)
# Sorting is optional.
# Compute the projection of a 3D length into 2D data coordinates # for this we use 2 3D half-circles that are projected into 2D. # (This gives the same length as projecting the full unit sphere.)
[xyz[2], xyz[1]]])
# Auxiliary array for calculating corners of a cube. corners[0, [0, 1, 4, 5], 2] = corners[0, [2, 3, 6, 7], 3] = \ corners[0, [0, 2, 4, 6], 4] = corners[0, [1, 3, 5, 7], 5] = 1.0
# The whole 3D ordering is flawed in mplot3d when several # collections are added. We just use normal zorder. Note the # "-" due to the different logic in the 3d plotting, we still # want larger zorder values to be plotted on top of smaller # ones.
# Note: unlike in the 2D case, where we can enforce equal # aspect ratio, this (currently) does not work with # 3D plots in matplotlib. As an approximation, we # thus scale with the average of the x- and y-axis # transformation. args[3]) * 0.5 * 72.0 / self.figure.dpi else: factor = 1
self.linewidths_orig * factor)
offsets=None, **kwargs):
self.orig_transforms = np.array( [Affine2D().scale(x) for x in sizes], dtype='object') else: [Affine2D().scale(x).get_matrix() for x in sizes])
# this is exact only for an isometric projection, for the # perspective projection used in mplot3d it's an approximation
# For the paths, use the data transformation but strip the # offset (will be added later with the offsets). else: return Affine2D().scale(self.figure.dpi / 72.0)
# numpy complains about zero-length index arrays return -self.zorder3d
paths = np.resize(self.paths_orig, (vs.shape[1],)) self.set_paths(paths[indx])
self.transforms = self.transforms[indx]
self.linewidths_orig2 = np.resize(lw_orig, (vs.shape[1],))[indx]
# Note: here array, facecolors and edgecolors are # guaranteed to be 2d numpy arrays or None. (And # array is the same length as the coordinates)
self).set_array(self.array_orig[indx])
self.facecolors_orig.shape[0] > 1): np.resize(self.facecolors_orig, shape)[indx])
self.edgecolors_orig.shape[0] > 1): shape = list(self.edgecolors_orig.shape) shape[0] = vs.shape[1] super().set_edgecolors( np.resize(self.edgecolors_orig, shape)[indx]) else: self.set_offsets(vs[:2].T)
# the whole 3D ordering is flawed in mplot3d when several # collections are added. We just use normal zorder, but correct # by the projected z-coord of the "center of gravity", # normalized by the projected z-coord of the world coordinates. # In doing so, several Path3DCollections are plotted probably # in the right order (it's not exact) if they have the same # zorder. Still, smaller and larger integer zorders are plotted # below or on top.
args[3]) * 0.5 * 72.0 / self.figure.dpi
# matplotlib helper functions.
warnings.warn( "Kwant's plotting functions have\nthe side effect of " "selecting the matplotlib backend. To avoid this " "warning,\nimport matplotlib.pyplot, " "matplotlib.backends or call matplotlib.use().", RuntimeWarning, stacklevel=3 ) # We import backends and pyplot only at the last possible moment (=now) # because this has the side effect of selecting the matplotlib backend # for good. Warn if backend has not been set yet. This check is the # same as the one performed inside matplotlib.use. from matplotlib import pyplot fig = pyplot.figure() else:
"""Output a matplotlib figure using a given output mode.
Parameters ---------- fig : matplotlib.figure.Figure instance The figure to be output. file : string or a file object The name of the target file or the target file itself (opened for writing). show : bool Whether to call ``matplotlib.pyplot.show()``. Only has an effect if not saving to a file.
Notes ----- The behavior of this function producing a file is different from that of matplotlib in that the `dpi` attribute of the figure is used by defaul instead of the matplotlib config setting. """
elif show: # If there was no file provided, pyplot should already be available and # we can import it safely without additional warnings. from matplotlib import pyplot pyplot.show()
"""Process a color specification to a format accepted by collections.
Parameters ---------- color : color specification collection : instance of a subclass of ``matplotlib.collections.Collection`` Collection to which the color is added. cmap : ``matplotlib`` color map specification or None Color map to be used if colors are specified as floats. norm : ``matplotlib`` color norm Norm to be used if colors are specified as floats. """
# matplotlib gets confused if dtype='object' color = tuple(color)
length = len(collection._segments3d) # Once again, matplotlib fault!
# check if it is an array of floats for color mapping except (TypeError, ValueError): pass
"""Return the path corresponding to the description in ``symbols``""" # Figure out if list of symbols or single symbol. symbols = [symbols] # Most likely a polygon specification (at least not a valid other # symbol). symbols = [symbols]
symbol in symbols]
return symbol kind, n, angle = symbol
if kind in ['p', 'P']: if kind == 'p': radius = 1. / cos(pi / n) else: # make the polygon such that it has area equal # to a unit circle radius = sqrt(2 * pi / (n * sin(2 * pi / n)))
angle = pi * angle / 180 patch = matplotlib.patches.RegularPolygon((0, 0), n, radius=radius, orientation=angle) else: raise ValueError("Unknown symbol definition " + str(symbol))
edgecolor='k', linewidth=None, cmap=None, norm=None, zorder=0, **kwargs): """Add a collection of symbols (2D or 3D) to an axes instance.
Parameters ---------- axes : matplotlib.axes.Axes instance Axes to which the lines have to be added. pos0 : 2d or 3d array_like Coordinates of each symbol. symbol: symbol definition. TODO To be written. size: float or 1d array Size(s) of the symbols. Defaults to 1. reflen: float or None, optional If ``reflen`` is ``None``, the symbol sizes and linewidths are given in points (absolute size in the figure space). If ``reflen`` is a number, the symbol sizes and linewidths are given in units of ``reflen`` in data space (i.e. scales with the scale of the plot). Defaults to ``None``. facecolor: color definition, optional edgecolor: color definition, optional Defines the fill and edge color of the symbol, repsectively. Either a single object that is a proper matplotlib color definition or a sequence of such objects of appropriate length. Defaults to all black. cmap : ``matplotlib`` color map specification or None Color map to be used if colors are specified as floats. norm : ``matplotlib`` color norm Norm to be used if colors are specified as floats. zorder: int Order in which different collections are drawn: larger ``zorder`` means the collection is drawn over collections with smaller ``zorder`` values. **kwargs : dict keyword arguments to pass to `PathCollection` or `Path3DCollection`, respectively.
Returns ------- `PathCollection` or `Path3DCollection` instance containing all the symbols that were added. """
#internally, size must be array_like
else:
paths = [] pos = np.empty((0, dim)) else:
offsets=pos, transOffset=axes.transData, zorder=zorder)
else:
cmap=None, norm=None, zorder=0, **kwargs): """Add a collection of line segments (2D or 3D) to an axes instance.
Parameters ---------- axes : matplotlib.axes.Axes instance Axes to which the lines have to be added. pos0 : 2d or 3d array_like Starting coordinates of each line segment pos1 : 2d or 3d array_like Ending coordinates of each line segment reflen: float or None, optional If `reflen` is `None`, the linewidths are given in points (absolute size in the figure space). If `reflen` is a number, the linewidths are given in units of `reflen` in data space (i.e. scales with the scale of the plot). Defaults to `None`. colors : color definition, optional Either a single object that is a proper matplotlib color definition or a sequence of such objects of appropriate length. Defaults to all segments black. linestyles :linestyle definition, optional Either a single object that is a proper matplotlib line style definition or a sequence of such objects of appropriate length. Defaults to all segments solid. cmap : ``matplotlib`` color map specification or None Color map to be used if colors are specified as floats. norm : ``matplotlib`` color norm Norm to be used if colors are specified as floats. zorder: int Order in which different collections are drawn: larger `zorder` means the collection is drawn over collections with smaller `zorder` values. **kwargs : dict keyword arguments to pass to `LineCollection` or `Line3DCollection`, respectively.
Returns ------- `LineCollection` or `Line3DCollection` instance containing all the segments that were added. """
raise ValueError('Incompatible lengths of coordinate arrays.')
else:
('linewidths' in kwargs and kwargs['linewidths'] == 0)): zorder=zorder) else:
zorder=zorder)
else: axes.add_collection3d(coll)
# Extracting necessary data from the system.
"""Return all the sites of the system and of the leads as a list.
Parameters ---------- sys : kwant.builder.Builder or kwant.system.System instance The system, sites of which should be returned. num_lead_cells : integer The number of times lead sites from each lead should be returned. This is useful for showing several unit cells of the lead next to the system.
Returns ------- sites : list of (site, lead_number, copy_number) tuples A site is a `~kwant.builder.Site` instance if the system is not finalized, and an integer otherwise. For system sites `lead_number` is `None` and `copy_number` is `0`, for leads both are integers. lead_cells : list of slices `lead_cells[i]` gives the position of all the coordinates of lead `i` within `sites`.
Notes ----- Leads are only supported if they are of the same type as the original system, i.e. sites of `~kwant.builder.BuilderLead` leads are returned with an unfinalized system, and sites of ``system.InfiniteSystem`` leads are returned with a finalized system. """ lead.builder.sites() for i in range(num_lead_cells))) # We will only plot leads with a graph and with a symmetry. len(syst.lead_interfaces[leadnr])): range(lead.cell_size) for i in range(num_lead_cells))) else: raise TypeError('Unrecognized system type.')
"""Return an array of positions of sites in a system.
Parameters ---------- sys : `kwant.builder.Builder` or `kwant.system.System` instance The system, coordinates of sites of which should be returned. site_lead_nr : list of `(site, leadnr, copynr)` tuples Output of `sys_leads_sites` applied to the system.
Returns ------- coords : numpy.ndarray of floats Array of coordinates of the sites.
Notes ----- This function uses `site.pos` property to get the position of a builder site and `sys.pos(sitenr)` for finalized systems. This function requires that all the positions of all the sites have the same dimensionality. """
# Note about efficiency (also applies to sys_leads_hoppings_pos) # NumPy is really slow when making a NumPy array from a tinyarray # (buffer interface seems very slow). It's much faster to first # convert to a tuple and then to convert to numpy array ...
else: else syst.leads[lead]) for i in site_lead_nr])) raise ValueError("pos attribute of the sites does not have consistent" " values.")
return np.zeros((dim,)), 0 except IndexError: return (0, 0) else: except (AttributeError, IndexError): # empty leads, or leads without symmetry aren't drawn anyways return (0, 0) # Conversion to numpy array here useful for efficiency
"""Return all the hoppings of the system and of the leads as an iterator.
Parameters ---------- sys : kwant.builder.Builder or kwant.system.System instance The system, sites of which should be returned. num_lead_cells : integer The number of times lead sites from each lead should be returned. This is useful for showing several unit cells of the lead next to the system.
Returns ------- hoppings : list of (hopping, lead_number, copy_number) tuples A site is a `~kwant.builder.Site` instance if the system is not finalized, and an integer otherwise. For system sites `lead_number` is `None` and `copy_number` is `0`, for leads both are integers. lead_cells : list of slices `lead_cells[i]` gives the position of all the coordinates of lead `i` within `hoppings`.
Notes ----- Leads are only supported if they are of the same type as the original system, i.e. hoppings of `~kwant.builder.BuilderLead` leads are returned with an unfinalized system, and hoppings of `~kwant.system.InfiniteSystem` leads are returned with a finalized system. """
# We need to make sure that the hopping is between a site in a # fundamental domain and a site with a negative domain. The # direction of the hopping is chosen arbitrarily # NOTE(Anton): This may need to be revisited with the future # builder format changes.
lead_hoppings(lead.builder) for i in range(num_lead_cells))) if i < j: yield i, j
start = len(hoppings) # We will only plot leads with a graph and with a symmetry. if (hasattr(lead, 'graph') and hasattr(lead, 'symmetry') and len(syst.lead_interfaces[leadnr])): hoppings.extend(((hop, leadnr, i) for hop in ll_hoppings(lead) for i in range(num_lead_cells))) lead_cells.append(slice(start, len(hoppings))) else: raise TypeError('Unrecognized system type.')
"""Return arrays of coordinates of all hoppings in a system.
Parameters ---------- sys : ``~kwant.builder.Builder`` or ``~kwant.system.System`` instance The system, coordinates of sites of which should be returned. hoppings : list of ``(hopping, leadnr, copynr)`` tuples Output of `sys_leads_hoppings` applied to the system.
Returns ------- coords : (end_site, start_site): tuple of NumPy arrays of floats Array of coordinates of the hoppings. The first half of coordinates in each array entry are those of the first site in the hopping, the last half are those of the second site.
Notes ----- This function uses ``site.pos`` property to get the position of a builder site and ``sys.pos(sitenr)`` for finalized systems. This function requires that all the positions of all the sites have the same dimensionality. """
tuple(i[0][1].pos)) for i in hop_lead_nr])) else: syst_from_lead = lambda lead: (syst if (lead is None) else syst.leads[lead]) pos = ta.array([ta.array(tuple(syst_from_lead(i[1]).pos(i[0][0])) + tuple(syst_from_lead(i[1]).pos(i[0][1]))) for i in hop_lead_nr]) pos = np.array(pos) raise ValueError("pos attribute of the sites does not have consistent" " values.")
return np.zeros((dim,)), 0 except IndexError: return (0, 0) else: try: sym = syst.leads[lead_nr].symmetry site = syst.sites[syst.lead_interfaces[lead_nr][0]] except (AttributeError, IndexError): # empyt leads or leads without symmetry are not drawn anyways return (0, 0)
# Useful plot functions (to be extended).
'site_size': {2: 0.25, 3: 0.5}, 'site_color': {2: 'black', 3: 'white'}, 'site_edgecolor': {2: 'black', 3: 'black'}, 'site_lw': {2: 0, 3: 0.1}, 'hop_color': {2: 'black', 3: 'black'}, 'hop_lw': {2: 0.1, 3: 0}, 'lead_color': {2: 'red', 3: 'red'}}
site_symbol=None, site_size=None, site_color=None, site_edgecolor=None, site_lw=None, hop_color=None, hop_lw=None, lead_site_symbol=None, lead_site_size=None, lead_color=None, lead_site_edgecolor=None, lead_site_lw=None, lead_hop_lw=None, pos_transform=None, cmap='gray', colorbar=True, file=None, show=True, dpi=None, fig_size=None, ax=None): """Plot a system in 2 or 3 dimensions.
An alias exists for this common name: ``kwant.plot``.
Parameters ---------- sys : kwant.builder.Builder or kwant.system.FiniteSystem A system to be plotted. num_lead_cells : int Number of lead copies to be shown with the system. unit : 'nn', 'pt', or float The unit used to specify symbol sizes and linewidths. Possible choices are:
- 'nn': unit is the shortest hopping or a typical nearst neighbor distance in the system if there are no hoppings. This means that symbol sizes/linewidths will scale as the zoom level of the figure is changed. Very short distances are discarded before searching for the shortest. This choice means that the symbols will scale if the figure is zoomed. - 'pt': unit is points (point = 1/72 inch) in figure space. This means that symbols and linewidths will always be drawn with the same size independent of zoom level of the plot. - float: sizes are given in units of this value in real (system) space, and will accordingly scale as the plot is zoomed.
The default value is 'nn', which allows to ensure that the images neighboring sites do not overlap.
site_symbol : symbol specification, function, array, or `None` Symbol used for representing a site in the plot. Can be specified as
- 'o': circle with radius of 1 unit. - 's': square with inner circle radius of 1 unit. - ``('p', nvert, angle)``: regular polygon with ``nvert`` vertices, rotated by ``angle``. ``angle`` is given in degrees, and ``angle=0`` corresponds to one edge of the polygon pointing upward. The radius of the inner circle is 1 unit. - 'no symbol': no symbol is plotted. - 'S', `('P', nvert, angle)`: as the lower-case variants described above, but with an area equal to a circle of radius 1. (Makes the visual size of the symbol equal to the size of a circle with radius 1). - matplotlib.path.Path instance.
Instead of a single symbol, different symbols can be specified for different sites by passing a function that returns a valid symbol specification for each site, or by passing an array of symbols specifications (only for kwant.system.FiniteSystem). site_size : number, function, array, or `None` Relative (linear) size of the site symbol. site_color : ``matplotlib`` color description, function, array, or `None` A color used for plotting a site in the system. If a colormap is used, it should be a function returning single floats or a one-dimensional array of floats. site_edgecolor : ``matplotlib`` color description, function, array, or `None` Color used for plotting the edges of the site symbols. Only valid matplotlib color descriptions are allowed (and no combination of floats and colormap as for site_color). site_lw : number, function, array, or `None` Linewidth of the site symbol edges. hop_color : ``matplotlib`` color description or a function Same as `site_color`, but for hoppings. A function is passed two sites in this case. (arrays are not allowed in this case). hop_lw : number, function, or `None` Linewidth of the hoppings. lead_site_symbol : symbol specification or `None` Symbol to be used for the leads. See `site_symbol` for allowed specifications. Note that for leads, only constants (i.e. no functions or arrays) are allowed. If None, then `site_symbol` is used if it is constant (i.e. no function or array), the default otherwise. The same holds for the other lead properties below. lead_site_size : number or `None` Relative (linear) size of the lead symbol lead_color : ``matplotlib`` color description or `None` For the leads, `num_lead_cells` copies of the lead unit cell are plotted. They are plotted in color fading from `lead_color` to white (alpha values in `lead_color` are supported) when moving from the system into the lead. Is also applied to the hoppings. lead_site_edgecolor : ``matplotlib`` color description or `None` Color of the symbol edges (no fading done). lead_site_lw : number or `None` Linewidth of the lead symbols. lead_hop_lw : number or `None` Linewidth of the lead hoppings. cmap : ``matplotlib`` color map or a sequence of two color maps or `None` The color map used for sites and optionally hoppings. pos_transform : function or `None` Transformation to be applied to the site position. colorbar : bool Whether to show a colorbar if colormap is used. Ignored if `ax` is provided. file : string or file object or `None` The output file. If `None`, output will be shown instead. show : bool Whether ``matplotlib.pyplot.show()`` is to be called, and the output is to be shown immediately. Defaults to `True`. dpi : float or `None` Number of pixels per inch. If not set the ``matplotlib`` default is used. fig_size : tuple or `None` Figure size `(width, height)` in inches. If not set, the default ``matplotlib`` value is used. ax : ``matplotlib.axes.Axes`` instance or `None` If `ax` is not `None`, no new figure is created, but the plot is done within the existing Axes `ax`. in this case, `file`, `show`, `dpi` and `fig_size` are ignored.
Returns ------- fig : matplotlib figure A figure with the output if `ax` is not set, else None.
Notes ----- - If `None` is passed for a plot property, a default value depending on the dimension is chosen. Typically, the default values result in acceptable plots.
- The meaning of "site" depends on whether the system to be plotted is a builder or a low level system. For builders, a site is a kwant.builder.Site object. For low level systems, a site is an integer -- the site number.
- color and symbol definitions may be tuples, but not lists or arrays. Arrays of values (linewidths, colors, sizes) may not be tuples.
- The dimensionality of the plot (2D vs 3D) is inferred from the coordinate array. If there are more than three coordinates, only the first three are used. If there is just one coordinate, the second one is padded with zeros.
- The system is scaled to fit the smaller dimension of the figure, given its aspect ratio.
""" raise RuntimeError("matplotlib was not found, but is required " "for plot()")
# Generate data.
# Choose plot type. :, : min(dim, array.shape[1])] else:
raise TypeError('{0} may not be a tuple, use list or ' 'array instead.'.format(name)) raise ValueError('Length of {0} is not equal to number of ' 'system sites.'.format(name))
'site_lw']:
# Apply transformations to the data
raise RuntimeError("Installed matplotlib does not support 3d plotting")
# Determine the reference length. reflen = None # If hoppings are present use their lengths to determine the # minimal one. else: # If no hoppings are present, use for the same purpose distances # from ten randomly selected points to the remaining points in the # system. points.reshape(-1, 1, dim)).reshape(-1, dim) # Then check if distances are present that are way shorter than the # longest one. Then take first distance longer than these short # ones. This heuristic will fail for too large systems, or systems with # hoppings that vary by orders and orders of magnitude, but for sane # cases it will work.
else: # The last allowed value is float-compatible. try: reflen = float(unit) except: raise ValueError('Invalid value of unit argument.')
# make all specs proper: either constant or lists/np.arrays: and not isinstance(spec, np.ndarray)): try: spec = np.asarray(spec) except: spec = np.asarray(spec, dtype='object')
and not isinstance(spec, np.ndarray)): try: spec = np.asarray(spec) except: spec = np.asarray(spec, dtype='object')
# separate different symbols (not done in 3D, the separation # would mess up sorting) (len(site_symbol) != 3 or site_symbol[0] not in ('p', 'P'))): symbol_dict = defaultdict(list) for i, symbol in enumerate(site_symbol): symbol_dict[symbol].append(i) symbol_slcs = [] for symbol, indx in symbol_dict.items(): symbol_slcs.append((symbol, np.array(indx))) fancy_indexing = True else:
# Choose defaults depending on dimension, if None was given
# if symbols are split up into different collections, # the colormapping will fail without normalization try: if site_color.ndim == 1 and len(site_color) == n_syst_sites: site_color = np.asarray(site_color, dtype=float) norm = matplotlib.colors.Normalize(site_color.min(), site_color.max()) except: pass
# take spec also for lead, if it's not a list/array, default, otherwise else defaults['site_symbol'][dim]) else defaults['site_size'][dim])
else defaults['site_edgecolor'][dim]) else defaults['site_lw'][dim]) else defaults['hop_lw'][dim])
try: cmap, hop_cmap = cmap except TypeError: pass
# make a new figure unless axes specified else: else: fig = None
# plot system sites and hoppings site_edgecolor)
reflen=reflen, symbol=symbol, facecolor=col, edgecolor=edgecol, linewidth=lw, cmap=cmap, norm=norm, zorder=2)
zorder=1, cmap=hop_cmap)
# plot lead sites and hoppings
dtype=float)
# Note: the previous version of the code had in addition this # line in the 3D case: # lead_site_colors = 1 / np.sqrt(1. + lead_site_colors) symbol=lead_site_symbol, facecolor=lead_site_colors, edgecolor=lead_site_edgecolor, linewidth=lead_site_lw, cmap=lead_cmap, zorder=2, norm=norm)
# Note: the previous version of the code had in addition this # line in the 3D case: # lead_hop_colors = 1 / np.sqrt(1. + lead_hop_colors) cmap=lead_cmap, norm=norm, zorder=1)
else: # make axis limits the same in all directions # (3D only works decently for equal aspect ratio. Since # this doesn't work out of the box in mplot3d, this is a # workaround)
# add separate colorbars for symbols and hoppings if ncessary
"""Interpolate a scalar function in vicinity of given points.
Create a masked array corresponding to interpolated values of the function at points lying not further than a certain distance from the original data points provided.
Parameters ---------- coords : np.ndarray An array with site coordinates. values : np.ndarray An array with the values from which the interpolation should be built. a : float, optional Reference length. If not given, it is determined as a typical nearest neighbor distance. method : string, optional Passed to ``scipy.interpolate.griddata``: "nearest" (default), "linear", or "cubic" oversampling : integer, optional Number of pixels per reference length. Defaults to 3.
Returns ------- array : 2d NumPy array The interpolated values. min, max : vectors The real-space coordinates of the two extreme ([0, 0] and [-1, -1]) points of ``array``.
Notes ----- - `min` and `max` are chosen such that when plotting a system on a square lattice and `oversampling` is set to an odd integer, each site will lie exactly at the center of a pixel of the output array.
- When plotting a system on a square lattice and `method` is "nearest", it makes sense to set `oversampling` to ``1``. Then, each site will correspond to exactly one pixel in the resulting array. """ # Build the bounding box.
# Select 10 sites to compare -- comparing them all is too costly. "interpolation may be confusing.", RuntimeWarning, stacklevel=2)
raise ValueError("The number of sites doesn't match the number of" "provided values.")
range(len(cmin))) # The numerical values in the following line are optimized for the common # case of a square lattice: # * 0.99 makes sure that non-masked pixels and sites correspond 1-by-1 to # each other when oversampling == 1. # * 0.4 (which is just below sqrt(2) - 1) makes tree.query() exact.
method='nearest', oversampling=3, num_lead_cells=0, file=None, show=True, dpi=None, fig_size=None, ax=None, pos_transform=None, background='#e0e0e0'): """Show interpolated map of a function defined for the sites of a system.
Create a pixmap representation of a function of the sites of a system by calling `~kwant.plotter.mask_interpolate` and show this pixmap using matplotlib.
Parameters ---------- sys : kwant.system.FiniteSystem or kwant.builder.Builder The system for whose sites `value` is to be plotted. value : function or list Function which takes a site and returns a value if the system is a builder, or a list of function values for each system site of the finalized system. colorbar : bool, optional Whether to show a color bar if numerical data has to be plotted. Defaults to `True`. If `ax` is provided, the colorbar is never plotted. cmap : ``matplotlib`` color map or `None` The color map used for sites and optionally hoppings, if `None`, ``matplotlib`` default is used. vmin : float, optional The lower saturation limit for the colormap; values returned by `value` which are smaller than this will saturate vmax : float, optional The upper saturation limit for the colormap; valued returned by `value` which are larger than this will saturate a : float, optional Reference length. If not given, it is determined as a typical nearest neighbor distance. method : string, optional Passed to ``scipy.interpolate.griddata``: "nearest" (default), "linear", or "cubic" oversampling : integer, optional Number of pixels per reference length. Defaults to 3. num_lead_cells : integer, optional number of lead unit cells that should be plotted to indicate the position of leads. Defaults to 0. file : string or file object or `None` The output file. If `None`, output will be shown instead. show : bool Whether ``matplotlib.pyplot.show()`` is to be called, and the output is to be shown immediately. Defaults to `True`. ax : ``matplotlib.axes.Axes`` instance or `None` If `ax` is not `None`, no new figure is created, but the plot is done within the existing Axes `ax`. in this case, `file`, `show`, `dpi` and `fig_size` are ignored. pos_transform : function or `None` Transformation to be applied to the site position. background : matplotlib color spec Areas without sites are filled with this color.
Returns ------- fig : matplotlib figure A figure with the output if `ax` is not set, else None.
Notes ----- - When plotting a system on a square lattice and `method` is "nearest", it makes sense to set `oversampling` to ``1``. Then, each site will correspond to exactly one pixel. """
raise RuntimeError("matplotlib was not found, but is required " "for map()")
else: 'for finalized systems.') else: fig = None
# Note that we tell imshow to show the array created by mask_interpolate # faithfully and not to interpolate by itself another time. origin='lower', interpolation='none', cmap=cmap, vmin=vmin, vmax=vmax) plot(syst, num_lead_cells, site_symbol='no symbol', hop_lw=0, lead_site_symbol='s', lead_site_size=0.501, lead_site_lw=0, lead_hop_lw=0, lead_color='black', colorbar=False, ax=ax)
fig_size=None, ax=None, *, params=None): """Plot band structure of a translationally invariant 1D system.
Parameters ---------- sys : kwant.system.InfiniteSystem A system bands of which are to be plotted. args : tuple, defaults to empty Positional arguments to pass to the ``hamiltonian`` method. Mutally exclusive with 'params'. momenta : int or 1D array-like Either a number of sampling points on the interval [-pi, pi], or an array of points at which the band structure has to be evaluated. file : string or file object or `None` The output file. If `None`, output will be shown instead. show : bool Whether ``matplotlib.pyplot.show()`` is to be called, and the output is to be shown immediately. Defaults to `True`. dpi : float Number of pixels per inch. If not set the ``matplotlib`` default is used. fig_size : tuple Figure size `(width, height)` in inches. If not set, the default ``matplotlib`` value is used. ax : ``matplotlib.axes.Axes`` instance or `None` If `ax` is not `None`, no new figure is created, but the plot is done within the existing Axes `ax`. in this case, `file`, `show`, `dpi` and `fig_size` are ignored. params : dict, optional Dictionary of parameter names and their values. Mutually exclusive with 'args'.
Returns ------- fig : matplotlib figure A figure with the output if `ax` is not set, else None.
Notes ----- See `~kwant.physics.Bands` for the calculation of dispersion without plotting. """
raise RuntimeError("matplotlib was not found, but is required " "for bands()")
# expand out the contents of 'physics.Bands' to get the H(k), # because 'spectrum' already does the diagonalisation. raise ValueError('The cell Hamiltonian is not Hermitian.')
# H_k = H_0 + V e^-ik + V^\dagger e^ik
fig_size=fig_size, ax=ax)
show=True, dpi=None, fig_size=None, ax=None): """Plot the spectrum of a Hamiltonian as a function of 1 or 2 parameters
Parameters ---------- syst : `kwant.system.FiniteSystem` or callable If a function, then it must take named parameters and return the Hamiltonian as a dense matrix. x : pair ``(name, values)`` Parameter to ``ham`` that will be varied. Consists of the parameter name, and a sequence of parameter values. y : pair ``(name, values)``, optional Used for 3D plots (same as ``x``). If provided, then the cartesian product of the ``x`` values and these values will be used as a grid over which to evaluate the spectrum. params : dict, optional The rest of the parameters to ``ham``, which will be kept constant. mask : callable, optional Takes the parameters specified by ``x`` and ``y`` and returns True if the spectrum should not be calculated for the given parameter values. file : string or file object or `None` The output file. If `None`, output will be shown instead. show : bool Whether ``matplotlib.pyplot.show()`` is to be called, and the output is to be shown immediately. Defaults to `True`. dpi : float Number of pixels per inch. If not set the ``matplotlib`` default is used. fig_size : tuple Figure size `(width, height)` in inches. If not set, the default ``matplotlib`` value is used. ax : ``matplotlib.axes.Axes`` instance or `None` If `ax` is not `None`, no new figure is created, but the plot is done within the existing Axes `ax`. in this case, `file`, `show`, `dpi` and `fig_size` are ignored.
Returns ------- fig : matplotlib figure A figure with the output if `ax` is not set, else None. """
raise RuntimeError("matplotlib was not found, but is required " "for plot_spectrum()") raise RuntimeError("Installed matplotlib does not support 3d plotting")
else: raise TypeError("Expected 'syst' to be a finite Kwant system " "or a function.")
# calculate spectrum on the grid of points else: # massage masked grid points into a list of NaNs of the appropriate length # make into a numpy array and reshape
# set up axes else: message=r'.*mouse rotation disabled.*') else: else:
# actually do the plot else: "axis should be created with 'projection=\"3d\"") # plot_surface cannot directly handle rank-3 values, so we # explicitly loop over the last axis
"""Interpolate currents in a system onto a regular grid.
The system graph together with current intensities defines a "discrete" current density field where the current density is non-zero only on the straight lines that connect sites that are coupled by a hopping term.
To make this vector field easier to visualize and interpret at different length scales, it is smoothed by convoluting it with the bell-shaped bump function ``f(r) = max(1 - (2*r / width)**2, 0)**2``. The bump width is determined by the `relwidth` and `abswidth` parameters.
This routine samples the smoothed field on a regular (square or cubic) grid.
Parameters ---------- syst : A finalized system The system on which we are going to calculate the field. current : '1D array of float' Must contain the intensity on each hoppings in the same order that they appear in syst.graph. relwidth : float or `None` Relative width of the bumps used to generate the field, as a fraction of the length of the longest side of the bounding box. This argument is only used if `abswidth` is not given. abswidth : float or `None` Absolute width ot the bumps used to generate the field. Takes precedence over `relwidth`. If neither is given, the bump width is set to four times the length of the shortest hopping. n : int Number of points the grid must have over the width of the bump.
Returns ------- field : n-d arraylike of float n-d array of n-d vectors. box : sequence of 2-sequences of float the extents of `field`: ((x0, x1), (y0, y1), ...)
"""
raise ValueError("Current and hoppings arrays do not have the same" " length.")
# hops: hoppings (pairs of points) # Take the average of the current flowing each way along the hoppings else:
# lens: scaled lengths of hoppings # dirs: normalized directions of hoppings
else: else:
# TODO: Generalize 'factor' prefactor to arbitrary dimensions and remove # this check. This check is done here to keep changes local raise ValueError("'interpolate_current' only works for 2D systems.")
# Create field array.
bbox_max[d] + 0.75*width, field_shape[d]) for d in range(dim)]
# F is the antiderivative of the smoothing function # f(ρ, z) = (1 - ρ^2 - z^2)^2 Θ(1 - ρ^2 - z^2), with respect to # z,with F(ρ, -∞) = 0 and where Θ is the heaviside function.
# Interpolate the field for each hopping.
# Zero volume: nothing to do. continue
# Coordinates of the grid points that are within range of the current # hopping. sparse=True, indexing='ij')
# Convert "coords" into scaled cylindrical coordinates with regard to # the hopping.
# 'field' contains contributions from both hoppings (i, j) and (j, i)
"""Compress linear sRGB into sRGB.""" else:
"""Expand sRGB into linear sRGB.""" else:
"""Make a colormap that linearly interpolates between the colors a and b.""" * color_diff.reshape((1, -1)))
max_linewidth=3, min_linewidth=1, density=2/9, colorbar=True, file=None, show=True, dpi=None, fig_size=None, ax=None): """Draw streamlines of a flow field in Kwant style
Solid colored streamlines are drawn, superimposed on a color plot of the flow speed that may be disabled by setting `bgcolor`. The width of the streamlines is proportional to the flow speed. Lines that would be thinner than `min_linewidth` are blended in a perceptually correct way into the background color in order to create the illusion of arbitrarily thin lines. (This is done because some plot backends like PDF do not support lines of arbitrarily thin width.)
Internally, this routine uses ``matplotlib.pyplot.streamplot``.
Parameters ---------- field : 3d arraylike of float 2d array of 2d vectors. box : 2-sequence of 2-sequences of float the extents of `field`: ((x0, x1), (y0, y1)) cmap : colormap, optional Colormap for the background color plot. When not set the colormap "kwant_red" is used by default, unless `bgcolor` is set. bgcolor : color definition, optional The solid color of the background. Mutually exclusive with `cmap`. linecolor : color definition Color of the flow lines. max_linewidth : float Width of lines at maximum flow speed. min_linewidth : float Minimum width of lines before blending into the background color begins. density : float Number of flow lines per point of the field. The default value is chosen to show two lines per default width of the interpolation bump of `~kwant.plotter.interpolate_current`. colorbar : bool Whether to show a colorbar if a colormap is used. Ignored if `ax` is provided. file : string or file object or `None` The output file. If `None`, output will be shown instead. show : bool Whether ``matplotlib.pyplot.show()`` is to be called, and the output is to be shown immediately. Defaults to `True`. dpi : float or `None` Number of pixels per inch. If not set the ``matplotlib`` default is used. fig_size : tuple or `None` Figure size `(width, height)` in inches. If not set, the default ``matplotlib`` value is used. ax : ``matplotlib.axes.Axes`` instance or `None` If `ax` is not `None`, no new figure is created, but the plot is done within the existing Axes `ax`. in this case, `file`, `show`, `dpi` and `fig_size` are ignored.
Returns ------- fig : matplotlib figure A figure with the output if `ax` is not set, else None. """ raise RuntimeError("matplotlib was not found, but is required " "for current()")
# Matplotlib's "density" is in units of 30 streamlines...
# Matplotlib plots images like matrices: image[y, x]. We use the opposite # convention: image[x, y]. Hence, it is necessary to transpose.
raise ValueError("Only 2D field can be plotted.")
elif cmap is not None: raise ValueError("The parameters 'cmap' and 'bgcolor' are " "mutually exclusive.")
else:
ax.set_axis_bgcolor(bgcolor) else: interpolation='bicubic', extent=[e for c in box for e in c], origin='lower')
density=density, linewidth=linewidth, color=color, cmap=line_cmap, arrowstyle='->', norm=matplotlib.colors.Normalize(0, 1))
"""Show an interpolated current defined for the hoppings of a system.
The system graph together with current intensities defines a "discrete" current density field where the current density is non-zero only on the straight lines that connect sites that are coupled by a hopping term.
To make this vector field easier to visualize and interpret at different length scales, it is smoothed by convoluting it with the bell-shaped bump function ``f(r) = max(1 - (2*r / width)**2, 0)**2``. The bump width is determined by the `relwidth` parameter.
This routine samples the smoothed field on a regular (square or cubic) grid and displays it using an enhanced variant of matplotlib's streamplot.
This is a convenience function that is equivalent to ``streamplot(*interpolate_current(syst, current, relwidth), **kwargs)``. The longer form makes it possible to tweak additional options of `~kwant.plotter.interpolate_current`.
Parameters ---------- syst : `kwant.system.FiniteSystem` The system for which to plot the ``current``. current : sequence of float Sequence of values defining currents on each hopping of the system. Ordered in the same way as ``syst.graph``. This typically will be the result of evaluating a `~kwant.operator.Current` operator. relwidth : float or `None` Relative width of the bumps used to generate the field, as a fraction of the length of the longest side of the bounding box. **kwargs : various Keyword args to be passed verbatim to `kwant.plotter.streamplot`.
Returns ------- fig : matplotlib figure A figure with the output if `ax` is not set, else None.
""" **kwargs)
# TODO (Anton): Fix plotting of parts of the system using color = np.nan. # Not plotting sites currently works, not plotting hoppings does not. # TODO (Anton): Allow a more flexible treatment of position than pos_transform # (an interface for user-defined pos). |