首页 > 解决方案 > 如何使用python在streamlit和plotly中创建列?

问题描述

我正在使用新的数据科学 web 应用程序创建一个仪表板,它与plotly协调。在 streamlit 中,可以通过使用列分隔组件来创建网格布局 ,您可以使用以下语句根据需要调整列的大小:col1,col2,col3 = st.beta_columns(3)

问题是当我尝试这个选项时,图表会像这样显示在彼此上:

在此处输入图像描述

它必须在条形图旁边显示饼图

代码:

#Streamlit pckgs
import streamlit as st

# EDA pckgs
import pandas as pd
import numpy as np

#VIZ pckgs
import plotly.express as px
import plotly.graph_objs as go

col1,col2,col3 = st.beta_columns([2,2,2])

        data = st.file_uploader("Upload Dataset",type=["csv","xlsx","xls"])
        if data is not None:        
            #check the type of the file 
            if data.type =="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
                df=pd.read_excel(data)
            elif data.type =="application/vnd.ms-excel":
                df=pd.read_csv(data)
            
            all_columns_names = df.columns.tolist()
            col = st.selectbox("Choose Column",df.columns.tolist())
            selected_column_names__pyplot = st.multiselect("Select Columns",all_columns_names)
            plot_btn = st.button("Generate Plot")
            #count plot
            with col1:
                #pie chart
                if st.checkbox("Pie Plot"):
                    if plot_btn:
                        data_pie = df[col].value_counts().to_frame()
                        labels=data_pie.index.tolist()
                        datavals=data_pie[col].tolist()
                        
                        trace=go.Pie(labels=labels,
                                    values=datavals,
                                    hovertemplate = "%{label}: <br>Value: %{value} ",
                                    showlegend=True,
                                    textposition='inside',
                                    )
                        layout = go.Layout(
                        title = 'Percentage of {}'.format(col),
                        height=600,
                        margin=go.Margin(l=0, r=200, b=100, t=100, pad=4)   # Margins - Left, Right, Top Bottom, Padding
                        )
                        
                        data = [trace]
                        fig = go.Figure(data=data,layout = layout)
                        st.plotly_chart(fig)

            with col2:
                if st.checkbox("Count_plot"):
                    # all_columns_names = df.columns.tolist()
                    # s=df[all_columns_names[0]].str.strip().value_counts()
                    if plot_btn:
                        s=df[col].str.strip().value_counts()
                        trace  = go.Bar(
                                x=s.index,
                                y=s.values,
                                showlegend = True
                                )

                        layout = go.Layout(
                            title = 'Count of {}'.format(col),
                        )
                        data = [trace]
                        fig = go.Figure(data=data,layout = layout)
                        st.plotly_chart(fig)

基于----的答案,我将此行更改st.plotly_chart(fig)为此行st.plotly_chart(fig, use_container_width=True)

但是图表变得非常小:

其中饼图位于第一列,条形图位于第二列

在此处输入图像描述 在此处输入图像描述

标签: pythonpandasplotlystreamlit

解决方案


use_container_width=True绘图时使用

st.plotly_chart(fig, use_container_width=True)

推荐阅读