【问题标题】:How to add data labels in ternary plotly diagram?如何在三元绘图中添加数据标签?
【发布时间】:2022-01-13 21:22:13
【问题描述】:

我正在按照plotly documentation 中的示例进行操作。我用数据点获得了漂亮的等高线图:

现在我想通过向数据点或等值线添加标签来使其更具可读性。目前,我可以通过将鼠标悬停在数据点上来了解数据点的坐标和值,但我希望这些数字被永久注释,因此我可以将其打印在科学文章中。

【问题讨论】:

    标签: python plot plotly diagram


    【解决方案1】:

    我会用fig.add_trace(go.Scatterternary() 突出显示兴趣点,并调整text amd marker 属性以使其看起来不错。这是一个示例,我在左下角突出显示了一个点:

    情节 2

    为什么不fig.add_annotatoins() 或别的什么?

    完美的可以说是能够为任何给定的标记设置悬停信息以始终显示。但据我所知,目前这是不可能的。您也可以使用fig.add_annotations(),但据我所知,您只能将x, y 坐标设置为纸上的xrefyref

    fig.add_trace(go.Scatterternary() 的一个优点是您也可以在图例中包含这些数据:

    fig.update_layout(showlegend = True)
    fig.for_each_trace(lambda t: t.update(showlegend = False))
    fig.data[-1].showlegend = True
    

    情节 2

    完整代码:

    import plotly.figure_factory as ff
    import numpy as np
    import plotly.graph_objects as go
    
    
    Al, Cu = np.mgrid[0:1:7j, 0:1:7j]
    Al, Cu = Al.ravel(), Cu.ravel()
    mask = Al + Cu <= 1
    Al, Cu = Al[mask], Cu[mask]
    Y = 1 - Al - Cu
    
    enthalpy = (Al - 0.5) * (Cu - 0.5) * (Y - 1)**2
    fig = ff.create_ternary_contour(np.array([Al, Y, Cu]), enthalpy,
                                    pole_labels=['Al', 'Y', 'Cu'],
                                    ncontours=20,
                                    coloring='lines',
                                    showmarkers=True)
    
    fig.add_trace(go.Scatterternary(
        a = [2],
        b = [8],
        c = [2],
        mode = "markers+text",
        text = ["A"],
        texttemplate = "%{text}<br>(%{a:.2f}, %{b:.2f}, %{c:.2f})",
        textposition = "bottom center",
        marker_symbol = 'circle-open',
        marker_color = 'green',
        marker_line_width = 3,
        marker_size = 12,
        textfont = {'family': "Times", 'size': [14, 14, 14],
    #                 'color': ["IndianRed", "MediumPurple", "DarkOrange"]
                   }
    ))
    
    fig.update_layout(showlegend = True)
    fig.for_each_trace(lambda t: t.update(showlegend = False))
    fig.data[-1].showlegend = True
    
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      相关资源
      最近更新 更多