Home > Enterprise >  How to customiza Seaborn/Matplotlib heatmap colorbars
How to customiza Seaborn/Matplotlib heatmap colorbars

Time:04-05

I need to costumize the labels and ticks of an heatmap colorbar obtained by using matplotlib with no success so far. My data have been already posted and can be found at:[1] [1]: Incomplete plt.show() figure from pandas dataframe

My working code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

df = pd.read_csv("deltaGdata.csv")

df1 = df[['VARIANT', 'DDgun','mCSM', 'SDM', 'DeepDDG', 'DynaMut2']]
df1.set_index(['VARIANT'],inplace=True)

sns.set(rc = {'figure.figsize':(7, 20)})  # (width_inches, width_height)
ax = sns.heatmap(df1, cmap='rocket')
ax.set_yticks(np.arange(len(df1))   .5)
ax.set_yticklabels(df1.index,fontname='DejaVu Sans', fontsize=14.5, fontweight='550' )
ax.set_xticklabels(df1,fontname='DejaVu Sans', fontsize=20, fontweight='550', rotation=90)

ax.set_title("ΔΔG (Kcal/mole)", fontname='DejaVu Sans', fontsize=24, fontweight='700')
figure = ax.get_figure()

figure.savefig('fig.png', dpi=300)
figure.savefig('fig.svg', dpi=300, format="svg")

This code produces an heatmap with a colorbar having very tiny ticks and numbers compared with the others in the final figure. I found that there is very little documentation about how to customize colorbars and nothing useful to fix my problem. I hope to get help also because I think it would be beneficial for others Matplotlib/Seaborn users.

CodePudding user response:

My understanding is that it consists of a heatmap and a color bar subplot, with the last subplot specifying the size of the label attribute.

print(ax.figure.axes)
[<AxesSubplot:title={'center':'ΔΔG (Kcal/mole)'}, ylabel='VARIANT'>, <AxesSubplot:label='<colorbar>'>]

# Add the following code
ax.figure.axes[-1].tick_params(labelsize=20)
  • Related