如何使三元绘图看起来像倒三角形?

2024-05-17 12:42:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图得到一个三元图来理解某些化合物形状的多样性。当使用plotly绘制球形、棒状和圆盘状时,它给出了一个很好的三角图,显示了所有化合物的多样性。但传统上,分子的PMI图绘制为倒三角形。谁能告诉我如何反转三角形图,这是我在执行以下命令后得到的。谢谢

df.head(5)

    smiles  npr1    npr2    sphere_likeness     rod_likeness    disc_likeness
0   ClCC(Cl)(Cl)Cl  0.541834    0.936058    0.477893    0.127884    3.942238e-01
1   CC(Cl)(Cl)Cl    0.718463    0.718463    0.436926    0.563074    1.141665e-07
2   ClC(Cl)C(Cl)Cl  0.519768    0.841920    0.361688    0.316160    3.221519e-01
3   ClCC(Cl)Cl  0.498890    0.694875    0.193765    0.610249    1.959857e-01
4   FC(F)(Cl)C(F)(Cl)Cl     0.720879    0.944308    0.665187    0.111383    2.234296e-01

import plotly.express as px

fig = px.scatter_ternary(df, a="sphere_likeness", b="rod_likeness", c="disc_likeness")

fig.show()

enter image description here


Tags: dfclfig绘制plotly形状disc球形
1条回答
网友
1楼 · 发布于 2024-05-17 12:42:54
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.graph_objects as go

def makeAxis(title, tickangle=0, tickprefix='', ticksuffix='', tickformat=''):
    ''' all-in-one axis customizer '''
    return {
      'title': title,
      'titlefont': { 'size': 20 },
      'tickangle': tickangle,
      'tickfont': { 'size': 15 },
      'tickcolor': 'rgba(0,0,0,0)',
      'tickformat': tickformat,
      'showticklabels': True,
      'showtickprefix': 'all',
      'tickprefix': tickprefix,
      'showticksuffix': 'all',
      'ticksuffix': ticksuffix,
      'ticklen': 5,
      'showline': True,
      'showgrid': True
    }

# Construct a dataframe from ASCII strings
pmi_text = ["    smiles  npr1    npr2    sphere_likeness     rod_likeness    disc_likeness",
            "0   ClCC(Cl)(Cl)Cl  0.541834    0.936058    0.477893    0.127884    3.942238e-01",
            "1   CC(Cl)(Cl)Cl    0.718463    0.718463    0.436926    0.563074    1.141665e-07",
            "2   ClC(Cl)C(Cl)Cl  0.519768    0.841920    0.361688    0.316160    3.221519e-01",
            "3   ClCC(Cl)Cl  0.498890    0.694875    0.193765    0.610249    1.959857e-01",
            "4   FC(F)(Cl)C(F)(Cl)Cl     0.720879    0.944308    0.665187    0.111383    2.234296e-01"]
t_smiles, t_npr1, t_npr2 = 'smiles', 'npr1', 'npr2'
data_raw = dict(zip(pmi_text[0][4:].split(), np.array([r[4:].split() for r in pmi_text[1:]]).T))
data_pmi = {k: (v if k == t_smiles else np.asfarray(v)) for k, v in data_raw.items()}
df = pd.DataFrame(data=data_pmi)
mat_customdata = np.stack((df[t_npr1], df[t_npr2]), axis=-1)
display(df)

t_A, t_B, t_C, t_suffix = 'rod', 'disc', 'sphere', '_likeness'
trace_name, figure_title = 'PMI', 'Principal Moments of Inertia'
fig = go.Figure( go.Scatterternary(  
    name=trace_name,
    text=df[t_smiles],
    a=df[f'{t_A}{t_suffix}'],
    b=df[f'{t_B}{t_suffix}'],
    c=df[f'{t_C}{t_suffix}'],
    customdata=mat_customdata,
    hoverlabel_align='right',
    hovertemplate = "".join(['<b>%{text}:</b><br><br>',
                             t_A + ': %{a:.6f}<br>',
                             t_B + ': %{b:.6f}<br>',
                             t_C + ': %{c:.6f}<br><br>',
                             t_npr1 + ': %{customdata[0]:g}<br>',
                             t_npr2 + ': %{customdata[1]:g}',
                             '<extra></extra>']), 
    mode='markers',
    marker={'size': 12} 
))

fig.update_layout({
    'title': figure_title,
    'ternary':
        {
        'sum': 1,
        'aaxis': makeAxis(t_A.upper() + '      .',   # '.' is for sake of alignment
                          tickprefix='r=',
                          tickformat='.2f'),
        'baxis': makeAxis(t_B.upper() + '      .', 
                          tickangle=60, 
                          tickprefix='d=',
                          tickformat='.2f'),
        'caxis': makeAxis('.           ' + t_C.upper(), 
                          tickangle=-60, 
                          tickprefix='s=',
                          tickformat='.2f'),
    },
    'showlegend': False
})
fig.show()

相关问题 更多 >