I have % of some total table data that I want displayed as a heatmap in either matplotlib or plotly.
If I add totals rows/columns to these tables, the heatmap will not assign heatmap intensity colors to the body of the table distinctly from the totals, and thus the highest intensity will always be at 100% where both row/column total meet. This skews the visual impact of the heatmap.
How can I adjust the code to display a row/column total, but have it colored distinctly in intensity from the rest of the table.
(https://i.sstatic.net/v8lCI73o.png)](https://i.sstatic.net/xqbN6BiI.png)
# Creating the pivot table with 'DATE' also as an index
pivot_table_monthly2 = monthly_data2.pivot_table(index=['DATE', 'COUNTRY RISK'], columns='RATING', values='NOMINAL %', aggfunc='sum', fill_value=0)
# Assuming 'pivot_tables_per_month' is already defined and contains your pivot tables
desired_order = ['Aaa', 'Aa1', 'Aa2', 'Aa3', 'A1', 'A2', 'A3', 'Baa1', 'Baa2', 'Baa3']
# Loop through each date and its corresponding pivot table in the dictionary
for date, pivot_table in pivot_tables_per_month2.items():
print(f"Heatmap for: {date.strftime('%Y-%m-%d')}") # Adjust formatting as per your date format
# Plot heatmap using seaborn
plt.figure(figsize=(10, 8)) # Adjust the size to fit your needs
# Convert pivot table data to percentages by dividing by 100 if your data is in integer percentage points
# If your data is already in proportion form (like 0.17 for 17%), you can skip this conversion
pivot_table_percentage = pivot_table / 100 # Remove or adjust this line based on your actual data format
sns.heatmap(pivot_table_percentage, annot=True, fmt=".1%", cmap="coolwarm", linewidths=.5)
plt.title(f"Rating / Country Risk Exposure for {date.strftime('%Y-%m-%d')}")
plt.xlabel("Rating")
plt.ylabel("Country Risk")
plt.show()
```[enter image description here](https://i.sstatic.net/JyhjKx2C.png)
A.S. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.