简体   繁体   English

如何使用 plotly 和 streamlit 来 plot 一个组

[英]how to plot a groupby using plotly and streamlit

I have a dataframe consist of 3 columns I want to plot the crosstab result of 3 columns我有一个 dataframe 包含 3 列我想要 plot 3 列的交叉表结果

I am able until now to plot bar chart of grouping 2 columns.到目前为止,我能够将 2 列分组的 plot 条形图。 on jupyter notebook without using of streamlit在 jupyter notebook 上不使用 streamlit

when i try to use streamlit it display the below error:当我尝试使用 streamlit 时,它显示以下错误:

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

So where is the error in my code??那么我的代码中的错误在哪里?

code:代码:

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()
        

There's probably more going on here, but you're missing () in fig = go.Figure() here:这里可能还有更多内容,但是您在fig = go.Figure()中缺少()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM