Color setting

Color in Batoms is float array of 4 items in [0, 1]. The four values are represented as: [Red, Green, Blue, Alpha]. Transparency is controlled by the Alpha value.

Batom and Batoms

Take CH4 molecule as a example.

>>> from ase.build import molecule
>>> from batoms import Batoms
>>> ch4 = molecule("CH4")
>>> ch4 = Batoms(label = "ch4", from_ase = ch4)
>>> ch4.get_image(viewport = [1, 1, 4], output = "color_ch4_0.png")
../_images/color_ch4_0.png

The Batom object has a color attribute. One set the color by:

>>> ch4["H"].color = [0, 0, 0.8, 1.0]
>>> ch4["C"].color = [0.8, 0, 0, 1.0]
../_images/color_ch4_1.png

Make transparency for H atoms by setting Alpha value < 1:

>>> ch4["H"].color = [0, 0, 0.8, 0.2]
../_images/color_ch4_2.png

Color by attributes

It is useful to color atoms by their attributes (e.g. positions, charges). For example, one can color atoms by their z coordinate:

from ase.build import bulk
from batoms import Batoms
import numpy as np
au = bulk("Au", cubic=True)*[5, 5, 5]
au = Batoms(label = "au", from_ase = au)
# add z coordinate as a new attribute, normalized it to [0, 1]
z = au.positions[:, 2]
au.set_attributes({"z_coor": (z-np.min(z))/(np.max(z)-np.min(z)) })
# color atoms by their z coordinate
au.species.color_by_attribute("z_coor")
au.get_image(viewport = [1, 0, 0], output = "color_by_z_coordinate.png")
../_images/color_by_z_coordinate.png

One can select the color map by setting the parameters cmap, which uses the color map from matplotlib. e.g.

au.species.color_by_attribute("charges", cmap = "plasma")

Random color

# add random values as a attribute
au.set_attributes({"random_coor": np.random.random(len(au)) })
au.species.color_by_attribute("random_coor")

Bond

One can print the default color by:

>>> ch4.bond.settings[("C", "H")].color1[:]

One can change color for bond pair C-H by:

>>> ch4.bond.settings[("C", "H")].color1 = [0.8, 0.1, 0.3, 0.5]
>>> ch4.bond.settings[("C", "H")].color2 = [0.1, 0.3, 0.2, 1.0]
>>> ch4.model_style = 1

color1 is for the first species in the bond pair (C), and color2 is for the second species (H).

color3

color4

Polyhedra

One can print the default color by:

>>> ch4.polyhedras.setting["C"].color[:]

One can change color for Polyhedra C by:

>>> ch4.polyhedras.setting["C"].color = [0.8, 0.1, 0.3, 0.8]
>>> ch4.bond.settings[("C", "H")].polyhedra = True
>>> ch4.model_style = 2

color5

color6