How can I display both the index labels and values using streamlit and sns.heatmap for timeseries

I am currently trying to display a heatmap for timeseries data using sns. I have created the pivot table using a multi-indexed dataframe and will select the axis based on the index labels. The problem is that the axis values do not show up. It instead shows the labels/names of the index rather than the actually values in the index. Below is a snippet:

At the bottom, instead of the years showing, it shows the label ‘Day’. The same with the y-axis. Is there a remedy to this? My code is below:

> x_axis = st.selectbox("Choose x axis", options=Data_to_select_indexed.index.names)
> y_axis = st.selectbox("Choose y axis", options=Data_to_select_indexed.index.names)
>                 
> # Pivot table
>   Pivot_data = Data_to_select_indexed.pivot_table(index=x_axis,columns=y_axis,values=data_to_analyse)
> 
>   f, ax2 = plt.subplots(figsize=(12, 7))  
>   # show data
>   ax2 = sns.heatmap(Pivot_data, linecolor="white", yticklabels=y_axis, xticklabels=x_axis, cmap="Blues", vmin= 0.9, vmax=1.65, linewidth=0.3)
>  st.write(ax2)
> st.pyplot()

If I did not choose ‘.names’ in the select box, it comes up with all the numbers rather than the category assigned to the index. for example, if I chose:

'x_axis st.selectbox(“Choose y axis”, options=Data_to_select_indexed.index)

Then all the numbers representing the dates (month, year, day) would show up individually.

I also tried changing the x/y axis label names using the inbuilt seaborn function albeit to no success.

Is there a way to structure this so that the end result will look like this but in streamlit?

Foolish mistake on my part. Sorry for wasting your time.

1 Like

Hey @Rushmore,

If possible, could you share your full solution so newcomers can reuse part of your code ? This app looks super nice :slight_smile:

Cheers,
Fanilo

Sure. So the real problem was here:

show data

ax2 = sns.heatmap(Pivot_data, linecolor=“white”, yticklabels=y_axis, xticklabels=x_axis, cmap=“Blues”, vmin= 0.9, vmax=1.65, linewidth=0.3)

the x and y axis was already included in the plot when I pivoted the data here:

Pivot table

Pivot_data = Data_to_select_indexed.pivot_table(index=x_axis,columns=y_axis,values=data_to_analyse)

So there was no need to include them in the final heatmap plot here:

show data

ax2 = sns.heatmap(Pivot_data, linecolor=“white”, yticklabels=y_axis, xticklabels=x_axis, cmap=“Blues”, vmin= 0.9, vmax=1.65, linewidth=0.3)

When placed in this final code, it was only picking up the text rather than the data attached to the column indexes. Once I removed the ‘yticklabels’ and ‘xticklabels’, it worked fine.

1 Like