交互图与重复的图错误

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

我正在尝试使用变量选择器通过变量绘制数据框以节省空间,但是在绘制图形时,将打印之前选择的所有信息。

我已经尝试过:


import plotly.express as px
import pandas as pd
from ipywidgets import interact, widgets, Output
from IPython.display import display, clear_output
from sklearn.datasets import load_iris

iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['species'] = iris.target_names[iris.target]

text_output = Output()

def update_boxplot(variable):
    clear_output(wait=True)
    with text_output:
        display(widgets.Label(value=f"Variable: {variable}"))
    
    fig = px.box(df, x='species', y=variable,
                 title=f"Boxplot of {variable}",
                 labels={'species': 'Species', variable: variable})
    fig.show()

variable_selector = widgets.Dropdown(options=iris.feature_names, description='Variable:')
interact(update_boxplot, variable=variable_selector)
display(text_output)


我得到:

我不知道如何隐藏之前的选择。

非常感谢!!!

python jupyter-notebook plotly ipython ipywidgets
1个回答
0
投票

试试这个:

import pandas as pd
import plotly.express as px

from IPython.display import clear_output, display
from ipywidgets import Output, interact, widgets
from sklearn.datasets import load_iris


iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df["species"] = iris.target_names[iris.target]

text_output = Output()


def update_boxplot(variable):
    clear_output(wait=True)
    with text_output:
        display(widgets.Label(value=f"Variable: {variable}"))

    fig = px.box(
        df,
        x="species",
        y=variable,
        title=f"Boxplot of {variable}",
        labels={"species": "Species", variable: variable},
    )
    fig.show()


variable_selector = widgets.Dropdown(
    options=iris.feature_names, description="Variable:"
)
_ = interact(update_boxplot, variable=variable_selector)

© www.soinside.com 2019 - 2024. All rights reserved.