gpt4 book ai didi

python - Plotly:如何利用 go.Scatter 等函数的元参数?

转载 作者:行者123 更新时间:2023-12-01 06:51:50 25 4
gpt4 key购买 nike

本质:

如何在以下设置的上下文中使用 help(go.Scatter) 输出中的 %{data[0[.meta[0]}

<小时/>

考虑一个简单的散点图,如下所示:

plotly :

enter image description here

代码:

import plotly.graph_objects as go
import numpy as np

t = [1,2,3]
fig = go.Figure(data=go.Scatter(x=t, y=t, mode='lines'))
fig.show()

运行 help(go.Scatter) 将给出一个输出,其中可以找到以下内容:

 |      meta
| Assigns extra meta information associated with this
| trace that can be used in various text attributes.
| Attributes such as trace `name`, graph, axis and
| colorbar `title.text`, annotation `text`
| `rangeselector`, `updatemenues` and `sliders` `label`
| text all support `meta`. To access the trace `meta`
| values in an attribute in the same trace, simply use
| `%{meta[i]}` where `i` is the index or key of the
| `meta` item in question. To access trace `meta` in
| layout attributes, use `%{data[n[.meta[i]}` where `i`
| is the index or key of the `meta` and `n` is the trace
| index.

问题:

我只是无法理解以下部分:

To access trace meta in layout attributes, use %{data[n[.meta[i]} where i is the index or key of the meta and n is the trace index.

在这个特定的图中,有一个跟踪,例如可以通过运行fig['data'][0]来访问:

Scatter({
'mode': 'lines', 'x': [1, 2, 3], 'y': [1, 2, 3]
})

根据帮助文件中有关 meta 的其他信息判断,我认为访问此信息将使我们能够编辑文本属性,例如 sliderslabel 因为它指出

Attributes such as trace name, graph, axis and colorbar title.text, annotation text rangeselector, updatemenues and sliders label text all support meta.

我尝试过各种方法,例如 fig['%{data[0[.meta[0]}'] 但我怀疑我完全误解了整个概念。谁能举个例子来说明如何在这种情况下使用元参数?

谢谢您的建议!

最佳答案

它看起来像是帮助文件中的类型错误。我认为应该是:%{data[n].meta[i]}.

以下代码设置图窗标题属性:

import plotly.graph_objects as go

meta = ["My Figure 1", "Data"]
t = [1,2,3]
data = go.Scatter(x=t,
y=t,
mode='lines')
layout = {'meta': meta,
'title': '%{meta[0]}' }

fig = go.Figure(data = data, layout = layout)
fig.show()

或者将数据放入跟踪中:

import plotly.graph_objects as go

meta = ["My Figure 1", "Data"]
t = [1,2,3]
data = go.Scatter(x=t,
y=t,
mode='lines',
meta= meta)
layout = {'title': '%{data[0].meta[0]}' }

fig = go.Figure(data = data, layout = layout)
fig.show()

这两种情况的结果都是:

enter image description here

关于python - Plotly:如何利用 go.Scatter 等函数的元参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58953331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com