Hi,
I created a Plotly pie chart with a few parameters and it displays a nice static pie chart. However when I try to add a slider, I only get errors, no doubt because my structure/syntax is incorrect.
Two questions:
-
Is what I’m trying to do possible - filter on only three types of ‘Fuel_Types’ (ELEC, HYBR, PHEV) and add a slider for the ‘Model_Year’ so a user can move the slider to any year and see the percentages of each (well, ‘count’ but Plotly Pie chart automatically converts to percentages, which is fine).
-
if yes, what am I doing incorrectly? I commented out the code for the slider, that’s most likely the problem area
Here is the code:
import plotly.express as px
import pandas as pd
import streamlit as st
#Import File:
df = pd.read_csv(r'/home/mike/Environments/Streamlit/Data/eCars.csv').query("Model_Year == 2023").query("Fuel_Code in ['ELEC']")#, 'HYBR', 'PHEV'
df = df.groupby(['Manufacturer']).size().reset_index(name='Total')
#Add Slider:
#all_years = df["Model_Year"] #tried: df.Model_Year.unique() and df["Model_Year"] and others
#selected_years = st.slider("Model_Year", all_years)
#filtered_df = df[df["Model_Year"].isin(selected_years)]
fig = px.pie(filtered_df, values='Total', names='Manufacturer', title="EV Totals 1993 - 2023",)
st.plotly_chart(fig, theme=None)
The file is in my github GitHub - mike-ua/Streamlit-Data
- running locally
- app is not deployed:
- app not on Github yet (locally only)
- Python 3.10.13, Streamlit 1.28.1