#Parity plot
import plotly.graph_objects as go
import numpy as np
from sklearn.metrics import r2_score
from scipy.stats import gaussian_kde
# Calculate R²
r2 = r2_score(Labels_Testing_mof, Predictions_Testing_mof)
# Calculate point density
x = Labels_Testing_mof.flatten()
y = Predictions_Testing_mof.flatten()
xy = np.vstack([x, y])
density = gaussian_kde(xy)(xy) # Density estimate at each point
# Create scatter plot colored by density
scatter = go.Scatter(
x=x,
y=y,
mode='markers',
marker=dict(
size=8,
color=density, # Use density as the color value
colorscale=[
[0.0, '#F6F2FF'], #
[0.1, '#a066c2'], # soft purple
[0.2, '#6b8ed6'], # soft blue
[0.4, '#5fbba1'], # soft green
[0.6, '#e1d05f'], # soft yellow
[0.8, '#f7a072'], # soft orange
[0.9, '#e35c5c'], # soft red
[1.0, '#640206'] # dark red
],
opacity=1,
colorbar=dict(
title='Point Density',
title_font=dict(color='black',size=35, family='Arial'),
title_side='right',
tickvals=[],
outlinewidth=0
),
line=dict(width=0),
),
text=[f'Actual: {a}, Predicted: {p}' for a, p in zip(x, y)],
hoverinfo='text'
)
# Create a line for perfect parity
parity_line = go.Scatter(
x=[-1,60],
y=[-1,60],
mode='lines',
line=dict(color='black', dash='dash', width=4) # Dashed line style and color
)
# Create a layout
layout = go.Layout(
xaxis=dict(
title=dict(text='GCMC Result (g/L)', font=dict(color='black', size=40)),
range=[0, 10],
tickfont=dict(color='black', size=40),
linecolor='black',
showline=True,
linewidth=4,
dtick=2,
ticks='outside',
tickwidth=4,
tickcolor='black',
ticklen=8,
mirror=True, # Mirror the axis line on the opposite side
showticklabels=True, # Show tick labels
ticksuffix=' ', # Add spacing after tick labels
),
yaxis=dict(
title=dict(text='PoroNet-Base Result (g/L)', font=dict(color='black', size=40)),
range=[0, 10],
tickfont=dict(color='black', size=40),
linecolor='black',
showline=True,
linewidth=4,
dtick=2,
ticks='outside',
tickwidth=4,
tickcolor='black',
ticklen=8,
mirror=True, # Mirror the axis line on the opposite side
showticklabels=True, # Show tick labels
ticksuffix=' ', # Add spacing after tick labels
),
showlegend=False, # Hide legend
height=800,
width=830,
font=dict(
family='Arial', # Global font: Arial
size=35 # Global font size
),
annotations=[
dict(
x=0.01, # Text x position (0-1)
y=0.98, # Text y position (0-1)
xref='paper', # x reference frame
yref='paper', # y reference frame
text=f'H<sub>2</sub> Uptake (160K 5bar)<br>1000 Testing MOFs', # Text content
showarrow=False, # No arrow
font=dict(size=35, family='Arial', color='black'), # Font settings
align='center' # Center-align text
),
dict(
x=0.98, # Text x position (0-1)
y=0, # Text y position (0-1)
xref='paper', # x reference frame
yref='paper', # y reference frame
text=f'R² = {r2:.2f}<br>MAE = {Loss_Testing_mof.item():.2f} g/L', # Text content
showarrow=False, # No arrow
font=dict(size=35, family='Arial', color='black'), # Font settings
align='center' # Center-align text
)
],
plot_bgcolor='white', # White plot-area background
paper_bgcolor='white' # White canvas background
)
# Create a Figure and add the scatter plot and parity line
fig = go.Figure(data=[scatter, parity_line], layout=layout)
fig.show()