python - 如何在三元绘图中添加数据标签?

标签 python plot plotly diagram

我正在遵循 plotly documentation 中的示例。我获得了带有数据点的漂亮等值线图:

enter image description here

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

最佳答案

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

图 2

enter image description here

为什么不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

enter image description here

完整代码:

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()

关于python - 如何在三元绘图中添加数据标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70703256/

相关文章:

r - plotly 等值线图 : display country names

python - 你能用Python来乘以单词吗?

用于坐标平面上的线的 Javascript 图表库

python - plotly add_trace 与 append_trace

python - matplotlib 中的条件函数绘图

r - "abline"不 't work after "情节 "when inside "与“

r - 绘制相同颜色的地 block

python - 如何在 Django 2.1 中使用多种形式?

python - 一起遍历列表的列表和列表

python - Pymongo MongoClient : if you put a database in via URI, 如何将其恢复?