-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathptable_heatmap_plotly.py
166 lines (135 loc) · 4.89 KB
/
ptable_heatmap_plotly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# %%
import os
import numpy as np
import yaml
from matminer.datasets import load_dataset
import pymatviz as pmv
from pymatviz.enums import ElemCountMode, Key
# %%
df_steels = load_dataset("matbench_steels")
df_expt_gap = load_dataset("matbench_expt_gap")
module_dir = os.path.dirname(__file__)
# %% Plotly interactive periodic table heatmap
fig = pmv.ptable_heatmap_plotly(
pmv.df_ptable[Key.atomic_mass],
hover_props=[Key.atomic_mass, Key.atomic_number],
hover_data="density = " + pmv.df_ptable[Key.density].astype(str) + " g/cm^3",
show_values=False,
)
fig.layout.title = dict(text="<b>Atomic mass heatmap</b>", x=0.4, y=0.94, font_size=20)
fig.show()
pmv.io.save_and_compress_svg(fig, "ptable-heatmap-plotly-more-hover-data")
# %%
fig = pmv.ptable_heatmap_plotly(df_expt_gap[Key.composition], heat_mode="percent")
title = "Elements in Matbench Experimental Bandgap"
fig.layout.title = dict(text=f"<b>{title}</b>", x=0.4, y=0.94, font_size=20)
fig.show()
pmv.io.save_and_compress_svg(fig, "ptable-heatmap-plotly-percent-labels")
# %%
fig = pmv.ptable_heatmap_plotly(
df_expt_gap[Key.composition], log=True, colorscale="viridis"
)
title = "Elements in Matbench Experimental Bandgap (log scale)"
fig.layout.title = dict(text=f"<b>{title}</b>", x=0.45, y=0.94, font_size=20)
fig.show()
pmv.io.save_and_compress_svg(fig, "ptable-heatmap-plotly-log")
# %% ex 1: Electronegativity Heatmap with Custom Hover Data
fig = pmv.ptable_heatmap_plotly(
pmv.df_ptable[Key.electronegativity],
colorscale="Viridis",
hover_props=["atomic_mass", "melting_point"],
hover_data={
el: f"Fun fact about {el}!" for el in pmv.df_ptable[Key.electronegativity].index
},
font_colors=["white", "black"],
colorbar=dict(title="Electronegativity", orientation="v"),
font_size=11,
)
fig.show()
# %% ex 2: Log-scale Abundance with Excluded Elements
fig = pmv.ptable_heatmap_plotly(
pmv.df_ptable[Key.specific_heat_capacity].dropna(),
# colorscale="YlOrRd",
log=True,
font_colors=["black"],
exclude_elements=["H", "He", "C", "O"],
heat_mode="value",
fmt=".2",
colorbar=dict(title="Specific heat"),
gap=3,
# border=False
)
fig.show()
# %% ex 3: Fictional Data with Percent Mode and Custom Color Scale
rand_data = {
elem: np.random.default_rng(seed=0).random() * 100 for elem in pmv.df_ptable.index
}
custom_colorscale = [
(0, "rgb(0,0,255)"),
(0.25, "rgb(0,255,255)"),
(0.5, "rgb(0,255,0)"),
(0.75, "rgb(255,255,0)"),
(1, "rgb(255,0,0)"),
]
fig = pmv.ptable_heatmap_plotly(
rand_data,
colorscale=custom_colorscale,
heat_mode="percent",
fmt=".2f",
font_colors=["black"],
bg_color="#f0f0f0",
)
fig.show()
# %% ex 4: Multi-element Compositions with Fraction Mode
fig = pmv.ptable_heatmap_plotly(
df_expt_gap[Key.composition][:30],
count_mode=ElemCountMode.fractional_composition,
heat_mode="fraction",
colorscale="plasma",
show_values=True,
fmt=".3f",
hover_props={"electronegativity": "EN", "atomic_radius": "Radius (pm)"},
)
fig.show()
# %% ex 5: Atomic Radius with Custom Hover and Label Mapping
fig = pmv.ptable_heatmap_plotly(
pmv.df_ptable[Key.atomic_radius],
colorscale="RdYlBu",
hover_data={
elem: f"{radius} pm"
for elem, radius in pmv.df_ptable[Key.atomic_radius].items()
},
font_colors=["black"],
colorbar=dict(title="Atomic Radius (pm)"),
)
fig.show()
# %% ex 6: valence electrons in VASP PBE 64 pseudo-potentials
with open(f"{module_dir}/vasp-pbe-64-n-val-elecs.yml") as file:
elem_to_n_val_elecs = yaml.safe_load(file)
elem_to_n_val_elecs = { # convert Potcar symbol to element symbol
key.split("_")[0]: val for key, val in elem_to_n_val_elecs.items()
}
fig = pmv.ptable_heatmap_plotly(elem_to_n_val_elecs, fmt=".0f")
title = (
"Number of valence electrons in VASP PBE 64 pseudo-potentials<br>"
"(Materials Project input set)"
)
fig.layout.title.update(text=title, x=0.4, y=0.92)
fig.show()
pmv.io.save_and_compress_svg(fig, "ptable-heatmap-plotly-vasp-psp-n-valence-electrons")
# %% Compute element counts in 2 datasets and plot their ratios
# Normalize by dataset size to get average counts per composition
gap_counts = pmv.count_elements(df_expt_gap[Key.composition]) / len(df_expt_gap)
steel_counts = pmv.count_elements(df_steels[Key.composition]) / len(df_steels)
fig = pmv.ptable_heatmap_plotly(
gap_counts / steel_counts,
log=True, # ratios are better viewed on log scale
fmt=".2g", # 2 significant digits for values
colorscale="RdBu", # red-white-blue diverging colorscale
nan_color="#ddd", # light gray for elements not present in either dataset
colorbar=dict(title="Matbench (Band gap / Steel) element counts"),
)
title = "Matbench Experimental Band Gap vs Steel Dataset<br>Element frequency ratios"
fig.layout.title = dict(text=title, x=0.4, y=0.95)
fig.show()
# pmv.io.save_and_compress_svg(fig, "ptable-heatmap-ratio-plotly")