I have the following code that generates a Heatmap in Pandas:
def create_cohort(cohort_size,retention_matrix,titulo):
print(f"{titulo}\n")
with sns.axes_style("white"):
fig, ax = plt.subplots(1, 2, figsize=(12, 8), sharey=True, gridspec_kw={'width_ratios': [1, 11]})
# retention matrix
sns.heatmap(retention_matrix,
mask=retention_matrix.isnull(),
annot=True,
fmt='.0%',
cmap='Purples',
ax=ax[1])
ax[1].set_title(f'Cohort: {titulo}', fontsize=16)
ax[1].set(xlabel='Meses',
ylabel='')
# cohort size
cohort_size_df = pd.DataFrame(cohort_size).rename(columns={0: 'Tamanho da cohort'})
white_cmap = mcolors.ListedColormap(['white'])
sns.heatmap(cohort_size_df,
annot=True,
cbar=False,
fmt='.0f',
cmap=white_cmap,
ax=ax[0])
fig.tight_layout()
return
This is a example of graph:
I would like to change the format of the most left table to add '$' before the number. I know I have to change the fmt='.0f', but I do not know how. I also could not find documentation on the values I can pass in the fmt parameter. Can someone also explain to me how this works? What values can I use?
CodePudding user response:
Instead of just setting annot=True, a list of strings, with the same shape as the dataframe can be provided:
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib.ticker import PercentFormatter
import seaborn as sns
import pandas as pd
N = 12
cohort_size = pd.DataFrame({0: np.random.randint(20000, 50000, N)}, index=[f'2021:{i:02d}' for i in range(1, 13)])
retention_matrix = np.random.rand(N, N)
retention_matrix[:, 0] = 1
retention_matrix = np.where(np.tril(retention_matrix)[::-1], retention_matrix, np.NaN)
with sns.axes_style("white"):
fig, ax = plt.subplots(1, 2, figsize=(12, 8), sharey=True, gridspec_kw={'width_ratios': [1, 11]})
sns.heatmap(retention_matrix,
annot=True,
fmt='.0%',
cmap='Purples',
cbar_kws={'format': PercentFormatter(1)},
ax=ax[1])
ax[1].set(xlabel='Meses', ylabel='')
cohort_size_df = pd.DataFrame(cohort_size).rename(columns={0: 'Tamanho da cohort'})
labels = [[f'$ {s:,d}'] for s in cohort_size_df.iloc[:, 0]]
sns.heatmap(cohort_size_df,
annot=labels,
cbar=False,
fmt='',
cmap=ListedColormap(['white']),
ax=ax[0])
ax[0].tick_params(axis='y', labelrotation=0)
fig.tight_layout()
plt.show()


