Isn't streamlit able to apply df.style.background_gradient?

Officially, styler support is still experimental and limited. That being said, I get the same colors using that cmap whether sending the styler to st.dataframe or converting it to html and rendering it directly.

import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'A':[.0074,.0049,.0023,.0021],
    'B':[.0018,.0034,.0008,.0013],
    'C':[.0057,.0034,.0008,.0007]
})

cmap = plt.cm.get_cmap('RdYlGn')
st.dataframe(df.style.background_gradient(cmap=cmap,vmin=(-0.015),vmax=0.015,axis=None))

st.write(df.style.background_gradient(cmap=cmap,vmin=(-0.015),vmax=0.015,axis=None).to_html(), unsafe_allow_html=True)

image

To double check, can you apply the .to_html() method to your styler and render in directly to confirm? If you find there is a discrepency, can you try a different version of Streamlit. I was running Streamlit 1.22 on Python 3.10.11. Matplot lib was version 3.7.1 also.

1 Like