1

I am building an application that displays stock correlations data in various visual forms, including a matrix with a heatmap applied. My heatmap is created by passing the correlation matrix dataframe into IPy Widgets Output, so I can display it as part of a VBox later on. I have successfully applied a background gradient and formatted my numbers to 2dp. Can anyone help me edit the function to also reduce the font size, I just want to shrink it up a little?

Note: I chose to do this using dataframe styling over matplotlib as I had a number of issues getting the output to display in the way I wanted. I also have a function that downloads the dataframe to excel with the styling applied.

I have tried putting the following line of code at the beginning of my notebook so I can leave it outside of the function, but it seems to get ignored once the dataframe is passed to Output.

pd.options.display.float_format = "{:,.2f}".format 

Here is my code sample:

import seaborn as sns
import ipywidgets as ipw
import pandas as pd
import numpy as np

#Sample Data
data = np.random.randint(5,30,size=500)
df = pd.DataFrame(data.reshape((50,10)))
corr = df.corr()

#Function produces dataframe as Output
def output_heatmap_df(df):
    out = ipw.Output()
    with out:
        display(df.style\
                .background_gradient(cmap=sns.diverging_palette(220,10, as_cmap=True),axis=None).format("{:,.2f}"))
        out.layout.width='1600px'
    return out

output_heatmap_df(corr)

1 Answer 1

5

In case anyone should come across this, the below code worked for me in the end:

def output_heatmap_df(df):
    out = ipw.Output()
    with out:
        display(df.style\
                .background_gradient(cmap=sns.diverging_palette(220,10, as_cmap=True),axis=None).format("{:,.2f}")
                .set_properties(**{'text-align':'center','font-size':'10px'})
                .set_table_styles([{'selector':'th','props':[('text-align','center'),('font-size','10px')]}])
               )
        out.layout.width='1600px'
    return out
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.