从下拉菜单或按钮中将sqrt设置为yaxis比例-Python/Plotly [英] Set sqrt as yaxis scale from dropdown or button-Python/Plotly

查看:61
本文介绍了从下拉菜单或按钮中将sqrt设置为yaxis比例-Python/Plotly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中:

但是您不必真正更改与yaxis关联的数据即可显示给定序列的平方根.下面的完整代码段产生了一个图形,可以轻松地在显示原始数据和具有平方根的序列之间进行切换.

图1:原始数据

图2:np.sqrt

完整代码:

 将numpy导入为np随地导入plotly.graph_objects导入plotly.express为pxdf = px.data.gapminder().query(年份== 2007")#人物设置无花果= 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"]),可见= False)#按钮进行更新菜单button = [dict(method ='restyle',label ='linear',visible =是,args = [{'label':'linear','visible':[True,False],}]),dict(method ='restyle',label ='sqrt',visible =是,args = [{'label':'linear','visible':[False,True],}])]#指定updatemenuum = [{'buttons':buttons,'direction':'down'}]fig.update_layout(updatemenus = um)#fig.update_yaxes(type ="log")图show() 

From this example here : Set linear or log axes from button or dropdown menu I can use a button to change the yaxis from linear to log. However i need to change it to sqrt.

I have looked at from Plotly: reference layout axis I have found that there is no type ("sqrt")

type Code: 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.

Here is the exmaple code:

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

Is there a way to use a button to update the scale to sqrt ?

解决方案

To my knowledge there's currently no way to specify sqrt as scale for the axis that would trigger a visual change to the axis labels like the case is for fig.update_xaxes(type="log"):

But you don't really have to in order to change the data associated with the yaxis to display the square roots of a given series. The complete snippet below produce a figure that will easily let you switch between displaying the raw data and a series with square roots.

Plot 1: Raw data

Plot 2: np.sqrt

Complete code:

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

这篇关于从下拉菜单或按钮中将sqrt设置为yaxis比例-Python/Plotly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆