如何使用plotly按钮和plotly.express (px),以及颜色选项?

问题描述 投票:0回答:1

px.scatter(color='key')
与颜色选项一起使用时,使用绘图按钮会表现得不稳定。单击任一按钮后的数据是混乱的,不是人们所期望的,当单击 Plength(用于在 y 轴上显示 petal_length 的按钮)时,当然不会返回到原始图,这就是我们开始的内容和。这是怎么回事?

工作正常:

from plotly import express as px
import pandas as pd

import seaborn as sns

df = sns.load_dataset('iris')

# df.head()
# >   sepal_length  sepal_width  petal_length  petal_width species
# > 0           5.1          3.5           1.4          0.2  setosa
# > 1           4.9          3.0           1.4          0.2  setosa
# > 2           4.7          3.2           1.3          0.2  setosa
# > 3           4.6          3.1           1.5          0.2  setosa
# > 4           5.0          3.6           1.4          0.2  setosa

# df.species.unique()
# > array(['setosa', 'versicolor', 'virginica'], dtype=object)

fig = px.scatter(
        df,
        x=df.index,
        y="petal_length",
        #color="species", 
        title='Iris',
    )

updatemenus = [
        dict(
            type="buttons",
            buttons=[
                dict(
                    label="Plength",
                    method="update",
                    args=[
                        {
                            "y": [df['petal_length']]
                        },
                        {
                            "title": "Petal length vs index",
                            "yaxis": {"title": "petal_length"},
                        },
                    ],
                ),
                dict(
                    label="Slength",
                    method="update",
                    args=[
                        {
                            "y": [df['sepal_length']]
                        },
                        {
                            "title": "Sepal length vs index",
                            "yaxis": {"title": "sepal_length"},
                        },
                    ],
                ),
            ],
        )
    ]

# Update layout with the updatemenu
fig.update_layout(updatemenus=updatemenus)

使用按钮时出现奇怪的数据:

from plotly import express as px
import pandas as pd

import seaborn as sns

df = sns.load_dataset('iris')
df.index

fig = px.scatter(
        df,
        x=df.index,
        y="petal_length",
        color="species", 
        title='Iris',
    )

updatemenus = [
        dict(
            type="buttons",
            buttons=[
                dict(
                    label="Plength",
                    method="update",
                    args=[
                        {
                            "y": [df['petal_length']]
                        },
                        {
                            "title": "Petal length vs index",
                            "yaxis": {"title": "petal_length"},
                        },
                    ],
                ),
                dict(
                    label="Slength",
                    method="update",
                    args=[
                        {
                            "y": [df['sepal_length']]
                        },
                        {
                            "title": "Sepal length vs index",
                            "yaxis": {"title": "sepal_length"},
                        },
                    ],
                ),
            ],
        )
    ]

# Update layout with the updatemenu
fig.update_layout(updatemenus=updatemenus)
python plotly plotly-python
1个回答
0
投票

事实证明,在内部,使用

color="species"
选项会在图中创建多个迹线,每个迹线对应一种物种类型。因此,我们实际上正在处理 3 行(每个物种各一行),但是当我使用按钮更新它们时,我尝试仅设置一个数组 (
"y": [df['petal_length']]
),尽管在本例中我们需要 3 行:
 "y":  [df[df["species"] == pt]["petal_length"] for pt in df["species"].unique()]
(一种方式)生成 3 个数组/pd.Series)。

from plotly import express as px
import pandas as pd

import seaborn as sns

df = sns.load_dataset('iris')
df.index

fig = px.scatter(
        df,
        x=df.index,
        y="petal_length",
        color="species",  # Assuming 'project_type' is a column in the DataFrame
        title='Iris',
    )

updatemenus = [
        dict(
            type="buttons",
            buttons=[
                dict(
                    label="Plength",
                    method="update",
                    args=[
                        {
                            "y":  [df[df["species"] == pt]["petal_length"]
                                for pt in df["species"].unique()]
                        },
                        {
                            "title": "Petal length vs index",
                            "yaxis": {"title": "petal_length"},
                        },
                    ],
                ),
                dict(
                    label="Slength",
                    method="update",
                    args=[
                        {
                            "y": [df[df["species"] == pt]["sepal_length"]
                                for pt in df["species"].unique()]
                        },
                        {
                            "title": "Sepal length vs index",
                            "yaxis": {"title": "sepal_length"},
                        },
                    ],
                ),
            ],
        )
    ]

# Update layout with the updatemenu
fig.update_layout(updatemenus=updatemenus)
© www.soinside.com 2019 - 2024. All rights reserved.