Interactive Visualization with Plotly#

Overview

Questions:

  • How do I use Python to interactively visualize data?

Objectives:

  • Use plotly to visualize data

Plotly is a Python library that allows you to create interactive visualizations. For this demonstration, we’ll be using Plotly Express, a simple interface to the capabilities of Plotly.

We will first read in our data using pandas, then we will plot data using plotly. Plotly express is imported as import plotly.express as px

import pandas as pd

import plotly.express as px
df = pd.read_csv("data/PubChemElements_all.csv")
df.head()
AtomicNumber Symbol Name AtomicMass CPKHexColor ElectronConfiguration Electronegativity AtomicRadius IonizationEnergy ElectronAffinity OxidationStates StandardState MeltingPoint BoilingPoint Density GroupBlock YearDiscovered
0 1 H Hydrogen 1.008000 FFFFFF 1s1 2.20 120.0 13.598 0.754 +1, -1 Gas 13.81 20.28 0.000090 Nonmetal 1766
1 2 He Helium 4.002600 D9FFFF 1s2 NaN 140.0 24.587 NaN 0 Gas 0.95 4.22 0.000179 Noble gas 1868
2 3 Li Lithium 7.000000 CC80FF [He]2s1 0.98 182.0 5.392 0.618 +1 Solid 453.65 1615.00 0.534000 Alkali metal 1817
3 4 Be Beryllium 9.012183 C2FF00 [He]2s2 1.57 153.0 9.323 NaN +2 Solid 1560.00 2744.00 1.850000 Alkaline earth metal 1798
4 5 B Boron 10.810000 FFB5B5 [He]2s2 2p1 2.04 192.0 8.298 0.277 +3 Solid 2348.00 4273.00 2.370000 Metalloid 1808

To plot something using plotly, we can use px.line or px.scatter for line and scatter plots respectively.

fig = px.line(df, x="AtomicNumber", y="MeltingPoint")
fig.add_scatter(x=df["AtomicNumber"], y=df["BoilingPoint"], name="Boiling Point")
fig.show()

Once you have a variable representing a figure, you can continue to update it in different cells. For example, we might wish to change the y axis label.

fig.update_yaxes(title="Melting Point (K)")
fig.update_xaxes(title="Atomic Number")

Plotly allows you to set the color of your markers based on the value in another column. In the cell below, we color the points based on their block.

fig = px.scatter(df, x="AtomicNumber", y="IonizationEnergy", color="GroupBlock")
fig.show()

With a little bit of processing, we can also add information about the row the element is on in the periodic table. In the cell below, I use pandas and Python string methods to get the element used in the electron configuration. You would not be expected to be able to write this code based on the workshop, but it’s an example of something you can do with Python!

df["row"] = df["ElectronConfiguration"].str.split("]").str[0].str.replace("[", "")

Now we can examine the effect of periodic table row on properties like ionization energy. We can see a clear trend in each row.

fig = px.scatter(df, x="AtomicNumber", y="IonizationEnergy", color="row", hover_data="Name")
fig.show()

Check Your Understanding

Create a plot of AtomicRadius vs AtomicNumber. Update your axes to have appropriate labels. Try adding color visualizaton to your plot using GroupBlock and Row.

What other periodic trends can you explore with visualization?

Plotly offers a number of visualization types. For example, we can also do thing like visualize boiling points by standard states using a box plot.

fig = px.box(df, x="StandardState", y="BoilingPoint")
fig.show()

You can see an overview of available plot types in plotly express on this page.