python - 如何使用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)
但是图表变得非常小:
其中饼图位于第一列,条形图位于第二列
解决方案
use_container_width=True
绘图时使用
st.plotly_chart(fig, use_container_width=True)
推荐阅读
- python - Python:分组数据的多个过滤器
- java - Spring 为 SQL 表创建数据库存储库
- python - 在python3中编写csv文件
- java - Struts 绝对 uri:http://struts.apache.org/tags-bean 无法在 web.xml 或随此应用程序部署的 jar 文件中解析
- python - 如何在循环中使用 numpy.dstack?
- c++ - 如何使用 C++ 在 Qt 中创建垂直(旋转)按钮
- python - 在fabric2中创建别名
- android - 没有开始的意图和上下文崩溃的应用程序
- konvajs - 如何在图层中添加上传图片
- python - 使用 Beautifulsoup 错误解析嵌套在 Div 类中的数据