Plotly 中的
add_trace
和 append_trace
有什么区别吗?后者是前者的遗产吗?
在Plotly.py GitHub中,有88个markdown + 21个
add_trace
的Python实例和9个markdowbn + 7个append_trace
的Python实例。后者主要来自doc
和packages/python/plotly/plotly/figure_factory
。
在 Plotly 子图文档中,有 4 个
append_trace
实例,而所有其他 52 个实例都是 add_trace
。
这是从中摘录的示例:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=3, cols=1)
fig.append_trace(go.Scatter(
x=[3, 4, 5],
y=[1000, 1100, 1200],
), row=1, col=1)
fig.append_trace(go.Scatter(
x=[2, 3, 4],
y=[100, 110, 120],
), row=2, col=1)
fig.append_trace(go.Scatter(
x=[0, 1, 2],
y=[10, 11, 12]
), row=3, col=1)
fig.update_layout(height=600, width=600, title_text="Stacked Subplots")
fig.show()
我尝试将此代码片段中的
append_trace
实例替换为 add_trace
,但没有观察到任何明显的差异。
我没有技术背景向你解释,但官方参考有以下解释
可以使用 add_trace() 方法将新跟踪添加到图形对象图中。此方法接受图形对象跟踪(go.Scatter、go.Bar 等的实例)并将其添加到图中。这允许您从一个空图形开始,然后按顺序向其添加轨迹。 append_trace() 方法执行相同的操作,尽管它不返回数字。
方法已已弃用,并将在未来版本中删除。append_trace
请使用带有 row 和 col 参数的方法。add_trace
--
源代码:
def append_trace(self, trace, row, col):
"""
Add a trace to the figure bound to axes at the specified row,
col index.
A row, col index grid is generated for figures created with
plotly.tools.make_subplots, and can be viewed with the `print_grid`
method
Parameters
----------
trace
The data trace to be bound
row: int
Subplot row index (see Figure.print_grid)
col: int
Subplot column index (see Figure.print_grid)
Examples
--------
>>> from plotly import tools
>>> import plotly.graph_objs as go
>>> # stack two subplots vertically
>>> fig = tools.make_subplots(rows=2)
This is the format of your plot grid:
[ (1,1) x1,y1 ]
[ (2,1) x2,y2 ]
>>> fig.append_trace(go.Scatter(x=[1,2,3], y=[2,1,2]), row=1, col=1)
>>> fig.append_trace(go.Scatter(x=[1,2,3], y=[2,1,2]), row=2, col=1)
"""
warnings.warn(
"""\
The append_trace method is deprecated and will be removed in a future version.
Please use the add_trace method with the row and col parameters.
""",
DeprecationWarning,
)
self.add_trace(trace=trace, row=row, col=col)
情节==5.18.0
.venv\Lib\site-packages\plotly\basedatatypes.py
尽管如此,我从未看到弹出警告,我不知道为什么