Skip to content

Commit ba51610

Browse files
committed
move plot from old atomdensity class
1 parent e6a5a51 commit ba51610

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

docs/analysis/atom_density.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Now, we import the analysis class `AtomDensity` and gather the above mentioned p
4848
We recommend that users instantiate an `Universe` object by themselves as follows. The dictionary `inp` is not required.
4949
```python
5050
from ectoolkits.analysis.atom_density import AtomDensity
51+
from MDAnalysis import Universe
5152

5253
# The following input
5354
inp={
@@ -103,8 +104,8 @@ all_cent_density = ad.get_ave_density(width_list)
103104

104105
# quick plot for denstiy
105106
# if you want to symmetrize the density profile, set sym=True
106-
ad.plot_density(sym=False)
107-
107+
fig = ad.plot_density(sym=False)
108+
fig.savefig('density.png', bbox_inches='tight')
108109
```
109110
![density](./figures/density.png)
110111

docs/analysis/figures/density.png

-4.43 KB
Loading

ectoolkits/analysis/atom_density.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from MDAnalysis.analysis.base import AnalysisBase
1111
from MDAnalysis.lib.mdamath import box_volume
1212

13+
import matplotlib as mpl
14+
import cp2kdata.plots.colormaps
15+
1316
from ectoolkits.structures.slab import Slab
1417
from ectoolkits.utils.utils import mic_1d
1518
from ectoolkits.log import get_logger
@@ -365,7 +368,41 @@ def get_unit_conversion(density_unit, dz, xy_area):
365368

366369
return unit_conversion
367370

371+
def get_ave_density(self, width_list):
372+
all_cent_density = {}
373+
all_cent_density[f"width"] = width_list
374+
for name, density in self.atom_density.items():
375+
z = self.atom_density_z[name]
376+
cent = z[-1]/2
377+
cent_density_list = []
378+
for width in width_list:
379+
left_bound = cent - width/2
380+
right_bound = cent + width/2
381+
part_density = density[np.logical_and(
382+
(z >= left_bound), (z <= right_bound))]
383+
cent_density = part_density.mean()
384+
cent_density_list.append(cent_density)
368385

386+
all_cent_density[f"{name}"] = cent_density_list
387+
388+
return pd.DataFrame(all_cent_density)
389+
390+
def plot_density(self, sym=False):
391+
plt.style.use('cp2kdata.matplotlibstyle.jcp')
392+
cp2kdata_cb_lcmap = mpl.colormaps['cp2kdata_cb_lcmap']
393+
plt.rcParams["axes.prop_cycle"] = plt.cycler("color", cp2kdata_cb_lcmap.colors)
394+
fig = plt.figure(figsize=(3.37, 1.89), dpi=400, facecolor='white')
395+
ax = fig.add_subplot()
396+
for name, density in self.atom_density.items():
397+
z = self.atom_density_z[name]
398+
density = density
399+
if sym:
400+
density = (np.flip(density) + density)/2
401+
ax.plot(z, density, label=name)
402+
ax.legend()
403+
ax.set_ylabel("Density")
404+
ax.set_xlabel(r"z ($\mathrm{\AA}$)")
405+
return fig
369406

370407

371408
# old density analysis function/class

0 commit comments

Comments
 (0)