4.8.2. Linear Density — MDAnalysis.analysis.lineardensity

A tool to compute mass and charge density profiles along the three cartesian axes of the simulation cell. Works only for orthorombic, fixed volume cells (thus for simulations in canonical NVT ensemble).

class MDAnalysis.analysis.lineardensity.LinearDensity(selection, grouping='atoms', binsize=0.25, **kwargs)[source]

Linear density profile

  • selection (AtomGroup) – any atomgroup

  • grouping (str {'atoms', 'residues', 'segments', 'fragments'}) – Density profiles will be computed on the center of geometry of a selected group of atoms [‘atoms’]

  • binsize (float) – Bin width in Angstrom used to build linear density histograms. Defines the resolution of the resulting density profile (smaller –> higher resolution) [0.25]

  • start (int) – The frame to start at [0]

  • stop (int) – The frame to end at [-1]

  • step (int) – The step size through the trajectory in frames [0]

  • verbose (bool (optional)) – Show detailed progress of the calculation if set to True; the default is False.


Keys ‘x’, ‘y’, and ‘z’ for the three directions. Under these keys, find ‘pos’, ‘pos_std’ (mass-weighted density and standard deviation), ‘char’, ‘char_std’ (charge density and its standard deviation), ‘slice_volume’ (volume of bin).




First create a LinearDensity object by supplying a selection, then use the run() method:

ldens = LinearDensity(selection)

New in version 0.14.0.


save is deprecated!

Save density profile to file

Allows to save the density profile to either a ASCII txt file or a binary numpy npz file. Output file has extension ‘ldens’ and begins with the name of the trajectory file.

  • description (str) – An arbitrary description added to the output filename. Can be useful

  • form (str {'txt', 'npz'}) – Format of the output. ‘txt’ will generate an ASCII text file while ‘npz’ will produce a numpy binary file.


After initializing and running a LinearDensity object, results can be written to file as follows:

ldens.save(description='mydensprof', form='txt')

which will output the linear density profiles in a file named <trajectory_filename>.mydensprof_<grouping>.ldens.

Deprecated since version 0.19.0: Instead save the results dictionary directly in your favorite format (pickle, json, hdf5, …). save will be removed in release 1.0.0.