如何在Streamlit中显示热力图颜色相关性图

5
我将尝试使用streamlit进行可视化。其中一个我要处理的内容是像这样的相关性: enter image description here 但我想把它变成颜色热力图。
这是我的相关性代码:
df_col = pd.concat([df5, df6,df7,df8,df9], axis=1)
    df5.columns = ['month', 'price_kcl', 'change_kcl']
    df6.columns = ['month_fosfat', 'price_fosfat', 'change_fosfat']
    df7.columns = ['month_bb', 'price_bara', 'change_bb']
    df8.columns = ['month_urea', 'price_urea', 'change_urea']
    df9.columns = ['month_npk', 'price_npk', 'change_npk']
    df_col = pd.concat([df5, df6,df7,df8,df9], axis=1)
    df5.columns = ['month', 'price_kcl', 'change_kcl']
    df6.columns = ['month_fosfat', 'price_fosfat', 'change_fosfat']
    df7.columns = ['month_bb', 'price_bara', 'change_bb']
    df8.columns = ['month_urea', 'price_urea', 'change_urea']
    df9.columns = ['month_npk', 'price_npk', 'change_npk']
    df_col = df_col.set_index('month')
    df_corr = df_col.corr()
    st.write(df_corr)
    plt.matshow(df_col.corr())

谢谢您的提前帮助!
1个回答

11

您可以在Streamlit中编写Matplotlib图表。您只需要稍微修改您的代码:

import seaborn as sns
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
sns.heatmap(df_col.corr(), ax=ax)
st.write(fig)

谢谢!但是我遇到了这个错误“corr() got an unexpected keyword argument 'ax'”,这是什么意思? - adinda aulia
1
抱歉,我忘记了热力图。现在我已经更新了答案。 - jjsantoso

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接