Advanced visualization tutorial

Next to the high level plotting function mendeleev.vis.periodic_table, mendeleev offers two lower level functions that give you more control over the result. There are two plotting backends supported:

  1. Plotly (default)

  2. Bokeh

Note

Depending on your environment being the classic jupyter notebook or jupyterlab you might have to do additional configuration steps, so if you’re not getting expected results see plotly of bokeh documentation.

Accessing lower level plotting functions

There are two plotting functions, one for each of the backends:

  • periodic_table_plotly in mendeleev.vis.plotly

  • periodic_table_bokeh in mendeleev.vis.bokeh

that you can use to customize the visualizations even further.

Both functions take the same keyword arguments as the periodic_table function but the also require a DataFrame with periodic table data. That dataframe needs to have x and y columns for each element that play the role of coordinates. You can get the default data using the create_vis_dataframe function. Let’s start with an example using the plotly backend.

[1]:
from mendeleev.vis import create_vis_dataframe, periodic_table_plotly

The function has only one required argument which is the data itself.

[2]:
elements = create_vis_dataframe()
periodic_table_plotly(elements)

Custom coloring scheme

To apply a custom color scheme you can assign color to all the elments in the DataFrame. This can be done by creating a custom column in the DataFrame and then using colorby argument to specify which column contains colors. Let’s try to color the elements according to the block they belong to.

[3]:
import seaborn as sns
from matplotlib import colors
blockcmap = {b : colors.rgb2hex(c) for b, c in zip(['s', 'p', 'd', 'f'], sns.color_palette('deep'))}

elements['block_color'] = elements['block'].map(blockcmap)
periodic_table_plotly(elements, colorby='block_color')

Custom properties

You can also visualize custom properties using pandas’ awesome methods for manipulating data. For example let’s consider the difference of electronegativity between every element and the Oxygen atom. To calculate the values we will use Allen scale this time and call our new value ENX-ENO.

[4]:
elements.loc[:, 'ENX-ENO'] =  elements.loc[elements['symbol'] == 'O', 'en_allen'].values - elements.loc[:, 'en_allen']

periodic_table_plotly(elements, attribute='ENX-ENO', colorby='attribute',
              cmap='viridis', title='Allen Electronegativity wrt. Oxygen')

As a second example let’s consider a difference between the covalent_radius_slater and covalent_radius_pyykko values

[5]:
elements['cov_rad_diff'] = elements['atomic_radius'] - elements['covalent_radius_pyykko']

periodic_table_plotly(elements, attribute='cov_rad_diff', colorby='attribute',
              title='Covalent Radii Difference', cmap='viridis')

Bokeh backend

We can also use the Bokeh backed in the same way but we need to take a few extra steps to render the result in a notebook

[6]:
from bokeh.plotting import show, output_notebook
from mendeleev.vis import periodic_table_bokeh

First we need to enable notebook output

[7]:
output_notebook()
Loading BokehJS ...
[8]:
fig = periodic_table_bokeh(elements)
show(fig)
[9]:
fig = periodic_table_bokeh(elements, attribute="atomic_radius", colorby="attribute")
show(fig)