Imagine we are interested in the following chemical structure, which is composed of atom types oxygen (O), hydrogen (H), and carbon (C).
The colors of the atoms in the structure visualization, which I made in VisIt visualization tool, correspond to the standard colors used in JMol. A data visualization regarding the atom types in this chemical structure should utilize the corresponding colors for consistency.
As a toy example, say we desire a bar plot that counts the number of atom types in the structure. We can make this in Python’s plotting library Matplotlib. What follows is a tutorial of how to make the colors in a Matplotlib visualization correspond to the colors in JMol. The final product:
A list of the RGB (red, green, blue) tuples that are used for the color of each atom is on the JMol website. I put a .csv of these colors on GitHub.
Using Pandas to load these RGB tuples into a DataFrame, I wrote a function that takes an atom type as an input and returns an RGB tuple that we will send to the plotting library Matplotlib:
I created the data that we seek to visualize regarding atom counts:
To make the bar plot, we need to define the array of colors corresponding to the atoms in df_toy
. We can do this with a list comprehension calling get_color()
:
Now, colors
is a list of RGB color tuples corresponding to C
, H
, and O
. We can pass this list of colors to Matplotlib to make the bar plot shown above: