繁体   English   中英

使用 Streamlit 和 matplotlib 显示 pandas dataframe 条 Z32FA6E1B78A9D402895Z3E6AA54

[英]Using Streamlit and matplotlib to display a pandas dataframe bar plot

目前我有:

fig, ax = plt.subplots()
ax = df.plot.barh(stacked=True)
st.pyplot(fig)

如有必要,供参考的 dataframe 如下所示:

       A     B     C     D      E
Cat1  5.3   NaN   NaN   NaN    NaN
Cat2  NaN   NaN   12.1  NaN    NaN
Cat3  NaN   NaN   NaN   3.4    4.5
Cat4  NaN   2.8   NaN   NaN    NaN                                       

如果我去掉st.pyplot(fig)中的fig ,强制 function 渲染全局图形 - 它会产生一个很好的堆叠条 plot,但带有弃用警告。

So I know it's not a problem with matplotlib producing the plot from my dataframe, but actually with streamlit displaying the plot.

基本上,我需要什么 matplotlib 语法来获得流光来产生这个水平堆叠条 plot?

提前致谢

您可以从 st.pyplot() 中删除无花果,streamlit 将显示您的 plot。 或者您可以使用 altair 渲染水平条 plot。

内部流线图生成器(即 altair 的包装器)也将生成您的 plot,但不会生成水平条。

import pandas as pd
import altair as alt
import matplotlib.pyplot as plt
import streamlit as st
st.set_option('deprecation.showPyplotGlobalUse', False)
st.set_page_config(page_title="Stacked Bar",layout="wide")
df=pd.read_clipboard()
df1=df.reset_index().melt(id_vars='index')
chart=alt.Chart(df1).mark_bar().encode(
    y=alt.Y("index:N", title=""),
    x="value:Q",
    color="variable:N").properties(height=300)



fig, ax = plt.subplots()
ax = df.plot.barh(stacked=True)

col1, col2, col3 = st.columns(3)

with col1:
    st.title('Matplotlib plot')
    st.pyplot()

with col2:
    st.title('Altair plot')
    st.altair_chart(chart, use_container_width=True)

with col3:
    st.title('Streamlit plot')
    st.bar_chart(df)

bar_h.png

暂无
暂无

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

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