我正在遵循 plotly documentation 中的示例。我获得了带有数据点的漂亮等值线图:
现在我想通过向数据点或等值线添加标签来使其更具可读性。目前,我可以通过将鼠标悬停在数据点上来了解数据点的坐标和值,但我希望这些数字能够被永久注释,因此我可以将其打印在科学文章中。
最佳答案
我会用 fig.add_trace(go.Scatterternary()
突出显示兴趣点并调整text and marker
属性使其看起来不错。下面是我突出显示左下角的一个点的示例:
图 2
为什么不fig.add_annotatoins()
或者其他什么?
完美的可能是能够将任何给定标记的悬停信息设置为始终显示。但据我所知,目前这是不可能的。您还可以使用fig.add_annotations()
,但据我所知你必须依赖 x, y
仅坐标 xref
和yref
设置在纸上。
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()
关于python - 如何在三元绘图中添加数据标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70703256/