Skip to content

Instantly share code, notes, and snippets.

@nikosavola
Last active June 1, 2023 08:57
Show Gist options
  • Save nikosavola/dc6c6781b96b4143589355dd48419699 to your computer and use it in GitHub Desktop.
Save nikosavola/dc6c6781b96b4143589355dd48419699 to your computer and use it in GitHub Desktop.
2D heatmap from DataFrame

Select two parameters and dependent variable from a DataFrame and plot an ´imshow´-style heatmap with isocontours.

Remember to:

import seaborn as sns
grid = df.pivot_table(index='param1', columns='param2', values='values')
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(grid, annot=False, cmap='magma', linewidths=.0, fmt='.3f', square=True,
ax=ax, annot_kws={"fontsize": 15}, cbar_kws={'label': 'values'})
ax.set_xlabel(ax.get_xlabel(), fontsize=14)
ax.set_ylabel(ax.get_ylabel(), fontsize=14)
# plot contours
z = scipy.ndimage.zoom(grid.to_numpy(), 2, order=1)
IC = ax.contour(
np.linspace(0, len(grid.columns), len(grid.columns) * 2),
np.linspace(0, len(grid.index), len(grid.index) * 2),
z, levels=[100, 150, 200, 250, 300, 500, 1000, 1500, 2000], linestyles='dashed', cmap='spring'
)
plt.clabel(IC, inline=True, fontsize=15) # add text to contours
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment