Search code examples
pythonplotly-pythonternary

Ternary plot from plotly


enter image description here

I want to add lines (horizontal, vertical) into ternary diagramm to highlight some parts. Could help me please?

The ternary diagram

enter image description here

The code is bellow:

fig = px.scatter_ternary(df,a=df.a, b =df.b, c=df.c, symbol=data_small.Time,
                         color = df.x, size_max = 'size', opacity = [1,1], title=s,
                        symbol_sequence = [1,0])
fig.update_layout({
'ternary':
    {'sum':1,
    'aaxis':{'title': ' a<br>', 'min': 0, 
             'linewidth':2, 'ticks':'outside',
             'tickmode':'array','tickvals':[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]},
    'baxis':{'title': '<br>b', 'min': 0, 
             'linewidth':2, 'ticks':'outside',
             'tickmode':'array','tickvals':[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]},
    'caxis':{'title': '<br>c', 'min': 0, 
             'linewidth':2, 'ticks':'outside',
             'tickmode':'array','tickvals':[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]}}})
fig.show()

Solution

  • I've never used this type of graph at all, but since the official reference drew a circle, I thought it would be possible to draw a line, so I customized it. In short, it is a combination of px.scatter_ternary+go.Scatterternary. I can't answer inquiries about coordinates.

    import plotly.express as px
    import plotly.graph_objects as go
    
    df = px.data.election()
    fig = px.scatter_ternary(df, a="Joly", b="Coderre", c="Bergeron", hover_name="district",
        color="winner", size="total", size_max=15,
        color_discrete_map = {"Joly": "blue", "Bergeron": "green", "Coderre":"red"} )
    
    fig.add_trace(
        go.Scatterternary(a=[0.1, 0.5],
                          b=[0.9, 0.5],
                          c=[0.1, 0.4],
                          mode='lines',
                          marker=dict(color='purple'),
                          line=dict(width=4),
                          showlegend=False)
    )
    
    fig.show()
    

    enter image description here