Heatmap

Sentiment Analysis Heatmap

Light theme heatmap showing sentiment scores by topic

Output
Sentiment Analysis Heatmap
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.patches import FancyBboxPatch

fig, ax = plt.subplots(figsize=(12, 8), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

np.random.seed(42)
topics = ['Product', 'Service', 'Pricing', 'Delivery', 'Support']
sources = ['Twitter', 'Reviews', 'Surveys', 'Email', 'Chat']
sentiment = np.random.uniform(-0.5, 0.8, (len(topics), len(sources)))

colors = ['#dc2626', '#f87171', '#334155', '#5eead4', '#14b8a6']
cmap = LinearSegmentedColormap.from_list('sentiment', colors, N=256)

cell_w, cell_h = 0.88, 0.82
for i in range(len(topics)):
    for j in range(len(sources)):
        val = sentiment[i, j]
        rect = FancyBboxPatch((j - cell_w/2, i - cell_h/2), cell_w, cell_h,
                               boxstyle="round,pad=0.02,rounding_size=0.12",
                               facecolor=cmap((val+1)/2), edgecolor='#e2e8f0', linewidth=1.5)
        ax.add_patch(rect)
        ax.text(j, i, f'{val:.2f}', ha='center', va='center', color='#1e293b', fontsize=10, fontweight='bold')

ax.set_xlim(-0.5, len(sources)-0.5)
ax.set_ylim(-0.5, len(topics)-0.5)
ax.set_aspect('equal')
ax.invert_yaxis()
ax.set_xticks(range(len(sources)))
ax.set_yticks(range(len(topics)))
ax.set_xticklabels(sources, color='#64748b', fontsize=10, fontweight='500')
ax.set_yticklabels(topics, color='#1e293b', fontsize=11, fontweight='500')

sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(-1, 1))
cbar = plt.colorbar(sm, ax=ax, shrink=0.8, pad=0.02)
cbar.set_label('Sentiment Score', color='#1e293b', fontsize=11)
cbar.outline.set_edgecolor('#e2e8f0')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#64748b')

for spine in ax.spines.values(): spine.set_visible(False)
ax.tick_params(length=0)
ax.set_title('Sentiment Analysis by Topic & Source', fontsize=18, color='#1e293b', fontweight='bold', pad=20)
plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Heatmaps & Density

Did this help you?

Support PyLucid to keep it free & growing

Support