Erstellen Sie einen Plotly -Streuplot mit zwei Dropdown -Tasten für x und eine für die y -AchsePython

Python-Programme
Anonymous
 Erstellen Sie einen Plotly -Streuplot mit zwei Dropdown -Tasten für x und eine für die y -Achse

Post by Anonymous »

Ich möchte ein Streudiagramm erstellen, aber mit 2 Dropdowns, bei dem ich zwischen den Variablen für X -Achse und der anderen für die Y -Achse (z. B. Gesamttests und Gesamtfälle oder Fälle in den letzten Fällen) wechseln kann

Code: Select all

cols_dd = ["Total tests", "Total cases", "Total deaths"][b]
for value in cols_dd:

with

cols_dd = {"Total tests":"Total cases", "Total deaths":"Recent cases", "Population":"Total vaccinations"}

k,v in cols_dd.items():

and the using k and v in place of values but it returns an error ('dict_keys' object is not subscriptable)
Kann jemand bitte einen Weg teilen, dies zu erreichen. Hier ist mein Code.

Code: Select all

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

# get OWID data
df = pd.read_csv(
"https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/latest/owid-covid-latest.csv"
)
# rename columns as sample code uses other names....
df = df.rename(
columns={
"location": "Location",
"iso_code": "Iso code",
"total_tests": "Total tests",
"people_vaccinated_per_hundred": "Vaccines",
"new_cases": "Recent cases",
"total_cases": "Total cases",
"total_deaths": "Total deaths",
"total_vaccinations": "Total vaccinations",
"people_vaccinated": "People vaccinated",
"population": "Population",
"total_boosters": "Vaccination policy",
}
).fillna(0)

cols_dd = {"Total tests":"Total cases", "Total deaths":"Recent cases", "Population":"Total vaccinations"}

fig = go.Figure()

for k,v in cols_dd.items():
fig.add_traces(px.scatter(df, x=k, y=v, color="Location",
hover_data={
"Iso code": False,
"Vaccines": True,
"Total tests": ": ,0.f",
"Recent cases": ": ,0.f",
"Total cases": ": ,0.f",
"Total deaths": ": ,0.f",
"Total vaccinations": ": ,0.f",
"People vaccinated": ": ,0.f",
"Population": ": ,0.f",
"Vaccination policy": ": 0.f",
},
color_continuous_scale="spectral_r",
hover_name="Location",
)
.update_traces(visible=(k == list(cols_dd.keys()[0]))
.data
))

fig.update_layout(
updatemenus=[
{
"buttons": [
{
"label": value,
"method": "update",
"args": [
{"visible": [v2 == value for v2 in cols_dd]},
{"title": f"{k}[/b] vs [b]{v}[/b]"},
],
}
for k in cols_dd
]
}
]
)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post