如何使用 plotly 和 streamlit 绘制 groupby
Posted
技术标签:
【中文标题】如何使用 plotly 和 streamlit 绘制 groupby【英文标题】:how to plot a groupby using plotly and streamlit 【发布时间】:2021-07-01 23:09:46 【问题描述】:我有一个包含 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()
【问题讨论】:
【参考方案1】:这里可能还有更多内容,但您在fig = go.Figure()
中缺少()
:
with interactive:
fig = go.Figure
for name,group in df.groupby(selected_column_names[0]):
【讨论】:
以上是关于如何使用 plotly 和 streamlit 绘制 groupby的主要内容,如果未能解决你的问题,请参考以下文章
如何让 Plotly 根据 Streamlit 应用程序中的屏幕分辨率调整绘图大小?
matplotlib plot 在 streamlit 中的分辨率很差