Streamlit:绘制数据帧值计数的流式条形图

问题描述 投票:0回答:2

如何显示

df['column_name'].value_counts()
的(流式)条形图?

python plot bar-chart streamlit
2个回答
3
投票

使用 Seabornst.pyplot

import seaborn as sns
import streamlit as st
val_count  = df['column_name'].value_counts()
fig = plt.figure(figsize=(10,5))
sns.barplot(val_count.index, val_count.values, alpha=0.8)
fig.title('Some title')
fig.ylabel('y label', fontsize=12)
fig.xlabel('x label', fontsize=12)


# Add figure in streamlit app
st.pyplot(fig)

将 pandas

value_counts
输出转换为
dataframe

df1 = df['column_name'].value_counts().rename_axis('unique_values').reset_index(name='counts')

使用st.bar_chart

st.bar_chart(df1)

0
投票

这是一个例子

enter image description here

import streamlit as st
import pandas as pd

# Sample DataFrame
data = {'column_name': ['A', 'B', 'A', 'C', 'B', 'A', 'D']}
df = pd.DataFrame(data)

# Get value counts
distribution = df['column_name'].value_counts()

# Display bar chart
st.bar_chart(distribution)
© www.soinside.com 2019 - 2024. All rights reserved.