How to use Seaborn Library In Python
- Using color palette for gradient fill in DataFrame: By importing the light palette of colors from the seaborn library, we can map the color gradient for the background of the data frame.
Python3
# Import seaborn library import seaborn as sns # Declaring the cm variable by the # color palette from seaborn cm = sns.light_palette( "green" , as_cmap = True ) # Visualizing the DataFrame with set precision print ( "\nModified Stlying DataFrame:" ) df.style.background_gradient(cmap = cm).set_precision( 2 ) |
Output:
- Using color palette with highlight null or missing values: Here, we highlight the NaN values in red color with gradient color palette of seaborn.
Python3
# Highlight the NaN values in DataFrame # using seaborn color palette print ( "\nModified Stlying DataFrame:" ) df.style.background_gradient(cmap = cm).set_precision( 2 ).highlight_null( 'red' ) |
Output:
- Assemble Seaborn properties with DataFrame.style property: Customizing the seaborn color palette with highlight properties of a data frame for more impactful data visualization.
Python3
# Highlight the NaN values in DataFrame # using seaborn color palette as well as # min('lighblue') and max('blue') values # in each column print ( "\nModified Stlying DataFrame:" ) df.style.background_gradient(cmap = cm).set_precision( 2 ).highlight_null( 'red' ).highlight_min(axis = 0 , color = 'lightblue' ).highlight_max(axis = 0 , color = 'blue' ) |
Output:
Set Pandas dataframe background Color and font color in Python
As we know, the basic idea behind styling is to make more impactful for the end-user readability. We can make changes like the color and format of the data visualized in order to communicate insight more efficiently. For the more impactful visualization on the pandas DataFrame, generally, we DataFrame.style property, which returns styler object having a number of useful methods for formatting and visualizing the data frames.