Dual plotly toggles on single graph – python

I’ve got two separate toggle’s that initiate color and size on a plotly scatter graph. These work fine independently but I’m hoping to combine them so both color and size are updated when both toggles are on.

I am unable to call a single scatter and then use update_trace because I need to assign discrete values to the species column.

I’m unaware if color and size can be updated to an existing figure but am content to change.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import plotly.express as px
import pandas as pd
import plotly.graph_objs as go
df = px.data.iris()
color_dict = {'versicolor': 'brown', 'setosa': 'orange', 'virginica': 'pink'}
size_dict = {'versicolor': 4, 'setosa': 6, 'virginica': 10}
sizemap = df["species"].map(size_dict)
df = df.sort_values(by = 'species', ascending= False).reset_index(drop = True)
app = dash.Dash(__name__)
app.layout = html.Div(
[
html.P("Toggle for the Color"),
dcc.Checklist(
id="color_toggle",
options=[{"label": "", "value": True}],
value=[],
inline=True
),
html.P("Toggle for the Size"),
dcc.Checklist(
id="size_toggle",
options=[{"label": "", "value": True}],
value=[],
inline=True
),
html.Div(
dcc.Graph(id="chart"),
),
]
)
@app.callback(
Output("chart", "figure"),
[Input("color_toggle", "value"),
Input("size_toggle", "value")]
)
def update_output(color_on, size_on):
fig = px.scatter(df, x="sepal_width", y="sepal_length", hover_data=["species"])
if color_on:
fig = px.scatter(df, x="sepal_width", y="sepal_length", color = "species", color_discrete_map = color_dict)
if size_on:
fig = go.Figure(data=go.Scatter(
x=df["sepal_width"],
y=df["sepal_length"],
mode = 'markers',
marker=dict(size=sizemap)
))
# return both color and szie
#if color_on & size_on:
return fig
if __name__ == "__main__":
app.run_server(debug=True)
</code>
<code>import dash from dash import dcc, html from dash.dependencies import Input, Output import plotly.express as px import pandas as pd import plotly.graph_objs as go df = px.data.iris() color_dict = {'versicolor': 'brown', 'setosa': 'orange', 'virginica': 'pink'} size_dict = {'versicolor': 4, 'setosa': 6, 'virginica': 10} sizemap = df["species"].map(size_dict) df = df.sort_values(by = 'species', ascending= False).reset_index(drop = True) app = dash.Dash(__name__) app.layout = html.Div( [ html.P("Toggle for the Color"), dcc.Checklist( id="color_toggle", options=[{"label": "", "value": True}], value=[], inline=True ), html.P("Toggle for the Size"), dcc.Checklist( id="size_toggle", options=[{"label": "", "value": True}], value=[], inline=True ), html.Div( dcc.Graph(id="chart"), ), ] ) @app.callback( Output("chart", "figure"), [Input("color_toggle", "value"), Input("size_toggle", "value")] ) def update_output(color_on, size_on): fig = px.scatter(df, x="sepal_width", y="sepal_length", hover_data=["species"]) if color_on: fig = px.scatter(df, x="sepal_width", y="sepal_length", color = "species", color_discrete_map = color_dict) if size_on: fig = go.Figure(data=go.Scatter( x=df["sepal_width"], y=df["sepal_length"], mode = 'markers', marker=dict(size=sizemap) )) # return both color and szie #if color_on & size_on: return fig if __name__ == "__main__": app.run_server(debug=True) </code>
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import plotly.express as px
import pandas as pd
import plotly.graph_objs as go

df = px.data.iris()
color_dict = {'versicolor': 'brown', 'setosa': 'orange', 'virginica': 'pink'}
size_dict = {'versicolor': 4, 'setosa': 6, 'virginica': 10}

sizemap = df["species"].map(size_dict)

df = df.sort_values(by = 'species', ascending= False).reset_index(drop = True)

app = dash.Dash(__name__)

app.layout = html.Div(
    [
        html.P("Toggle for the Color"),
        dcc.Checklist(
            id="color_toggle",
            options=[{"label": "", "value": True}],
            value=[],
            inline=True
        ),
        html.P("Toggle for the Size"),
        dcc.Checklist(
            id="size_toggle",
            options=[{"label": "", "value": True}],
            value=[],
            inline=True
        ),
        html.Div(
            dcc.Graph(id="chart"),
        ),
    ]
)


@app.callback(
    Output("chart", "figure"),
    [Input("color_toggle", "value"), 
     Input("size_toggle", "value")]
)
def update_output(color_on, size_on):
    fig = px.scatter(df, x="sepal_width", y="sepal_length", hover_data=["species"])

    if color_on:
        fig = px.scatter(df, x="sepal_width", y="sepal_length", color = "species", color_discrete_map = color_dict)

    if size_on:
        fig = go.Figure(data=go.Scatter(
                    x=df["sepal_width"], 
                    y=df["sepal_length"],
                    mode = 'markers', 
                    marker=dict(size=sizemap)
                    ))
    
    # return both color and szie
    #if color_on & size_on:

    return fig


if __name__ == "__main__":
    app.run_server(debug=True)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật