gpt4 book ai didi

python - 从下拉菜单或按钮-Python/Plotly 将 sqrt 设置为 yaxis 比例

转载 作者:行者123 更新时间:2023-12-04 12:34:10 24 4
gpt4 key购买 nike

从这里的这个例子:Set linear or log axes from button or dropdown menu我可以使用一个按钮将 yaxis 从线性更改为日志。但是我需要将其更改为 sqrt。

我看过来自 Plotly: reference layout axis我发现没有类型 ("sqrt")

typeCode: fig.update_yaxes(type=)Type: enumerated , one of ( "-" | "linear" | "log" | "date" | "category" | "multicategory" )Default: "-" Sets the axis type. By default, plotly attempts to determined the axis type by looking into the data of the traces that referenced the axis in question.

这是示例代码:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[8, 7, 6, 5, 4, 3, 2, 1, 0]
))

fig.add_trace(go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
))

fig.update_layout(title_text="CIR plot ",
updatemenus=[
dict(
buttons=list([
dict(label="Linear", method="update", args=[{"yaxis":{"type": "linear"}}]),
dict(label="Log", method="update", args=[{"yaxis":{"type": "log"}}]),
]),
)])

#UPDATE Y AXIS HERE
fig.update_layout( updatemenus=[
dict(
buttons=list([
dict(label="Linear-ID",
method="relayout",
args=[{"yaxis.type": "linear"}]),
dict(label="Log-ID",
method="relayout",
args=[{"yaxis.type": "log"}]),
]),
)])
fig.show()

有没有办法使用按钮将比例更新为 sqrt ?

最佳答案

据我所知,目前无法将 sqrt 指定为轴的比例,这会触发轴标签的视觉变化,例如 fig.update_xaxes(type="日志"):

enter image description here

但您实际上不必更改与 yaxis 关联的数据以显示给定系列的平方根。下面的完整代码片段生成一个图形,可以让您轻松地在显示原始数据和带平方根的系列之间切换。

图 1:原始数据

enter image description here

图 2:np.sqrt

enter image description here

完整代码:

import numpy as np
import plotly.graph_objects as go
import plotly.express as px
df = px.data.gapminder().query("year == 2007")

# figure setup
fig = go.Figure()
fig.add_scatter(mode="markers", x=df["gdpPercap"], y=df["lifeExp"])
fig.add_scatter(mode="markers", x=df["gdpPercap"], y=np.sqrt(df["lifeExp"]), visible = False)

# buttons for updatemenu
buttons = [dict(method='restyle',
label='linear',
visible=True,
args=[{'label': 'linear',
'visible':[True, False],
}
]),
dict(method='restyle',
label='sqrt',
visible=True,
args=[{'label': 'linear',
'visible':[False, True],
}
])]

# specify updatemenu
um = [{'buttons':buttons,
'direction': 'down'}
]

fig.update_layout(updatemenus=um)
# fig.update_yaxes(type="log")
fig.show()

关于python - 从下拉菜单或按钮-Python/Plotly 将 sqrt 设置为 yaxis 比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66226542/

24 4 0