如何使用 plotly 和 streamlit 绘制 groupby

how to plot a groupby using plotly and streamlit

我有一个包含 3 列的数据框我想绘制 3 列的交叉表结果

到目前为止,我能够绘制分组 2 列的条形图。在 jupyter notebook 上 不使用 streamlit

当我尝试使用 streamlit 时它显示以下错误:

TypeError: add_trace() missing 1 required positional argument: 'trace'

那么我的代码哪里出错了??

代码:

import streamlit as st
import pandas as pd 
import plotly.express as px
import plotly.graph_objs as go

#create a canvas for each item
interactive =  st.beta_container()

df =pd.DataFrame({"source_number":[11199,11328,11287,32345,12342,1232,13456,123244,1235],
       "location":["loc1","loc2","loc3","loc1","loc1","loc2","loc3","loc2","loc1"],
       "category":["cat1","cat3","cat1","cat3","cat2","cat3","cat2","cat3","cat1"],
       }) 

all_columns_names= df.columns.tolist()
selected_column_names = st.multiselect("select column to plot",all_columns_names)

s = df[selected_column_names[0]].str.strip().value_counts()


with interactive:
    fig = go.Figure
    for name,group in df.groupby(selected_column_names[0]):
        trace =go.Histogram()
        trace.name = name 
        trace.x = group[selected_column_names[1]]
        fig.add_trace(trace)
    fig.show()
        

这里可能还有更多内容,但您在 fig = go.Figure() 中缺少 ()

with interactive:
    fig = go.Figure
    for name,group in df.groupby(selected_column_names[0]):