0

I have pandas dataframe which stores currency rate for given day. print(df[['Date', 'Currency', 'Rate']].head())

         Date Currency     Rate
18 2023-12-01      AUD  1.64655
19 2023-12-01      BRL  5.36325
13 2023-12-01      CAD  1.47360
20 2023-12-01      CHF  0.95290
6  2023-12-01      CNY  7.78240

I want to visualize heatmap so that for given day x currency I have colored rectangle. I am visualizing it via streamlit, but for given code it shows just one rectangle for each currency

def generate_heatmap(df):
    # Adding a bit more information on the color scale for debugging
    df = df.sort_values(by=['Date', 'Currency'])
    scale = alt.Scale(scheme='viridis', domain=[df['Rate'].min(), df['Rate'].max()])
    print(df[['Date', 'Currency', 'Rate']].head())
    heatmap = alt.Chart(df).mark_rect().encode(
        x=alt.Y('Date:T', axis=alt.Axis(title='Date', format='%Y-%m-%d')),
        y='Currency:N',
        
        color=alt.Color('Rate:Q', scale=scale, legend=alt.Legend(title="Exchange Rate"))
    ).configure_scale(
        bandPaddingInner=0.1
    )
   return heatmap

chart = generate_heatmap(df_chart)
st.altair_chart(chart, use_container_width=True)

enter image description here

Any ideas how to fix it? It works fine for mark_circles() so I am suspecting the rectangle just spreads too wide, but it should not.

3
  • Depending on exactly what you want, you can try using either mark_square or changing the x axis to 'Date:O' Commented Jun 14 at 14:17
  • I believe Date:0 might solve as suggested by @joelostblom. Commented Jun 14 at 19:50
  • mark_square did not have any effect, but Date:O works, although the date number do no make sense now. Instead 2023-12-01 I have 17138520000, so I think I need to find some meaningful conversion first Commented Jun 15 at 9:28

0

Browse other questions tagged or ask your own question.