Mis a jour le 2024-10-20, 20:16

Heatmap et clustermap

Heatmap

heatmap : Pour la matrice suivante :
array([[ 0,  2,  4,  6,  8, 10],
       [ 0,  3,  6,  9, 12, 15],
       [ 0,  4,  8, 12, 16, 20],
       [ 0,  5, 10, 15, 20, 25],
       [ 0,  6, 12, 18, 24, 30],
       [ 0,  7, 14, 21, 28, 35]])
  
ar = numpy.array([0, 1, 2, 3, 4, 5]) mat = numpy.dot(ar.reshape(1, 6).T + 2, ar.reshape(1, 6)) df = pandas.DataFrame(mat, columns = ['A', 'B', 'C', 'D', 'E', 'F'], index = ['a', 'b', 'c', 'd', 'e', 'f']) seaborn.heatmap(df, annot = True, cbar = True, cmap = 'plasma') pyplot.tight_layout()
Heatmap avec des limites discrètes déterminées à l'avance (le constructeur BoundaryNorm renvoie une fonction myNorm qui transforme les valeurs en entiers qui va servir de numéro de la couleur) : df = pandas.DataFrame({'A': [1, 7, 20, 60], 'B': [3, 5, 30, 9]}, index = ['a', 'b', 'c', 'd']) boundaries = [0, 1, 5, 10, 50, 100] colors = seaborn.light_palette('navy', n_colors = len(boundaries)).as_hex() myColorMap = matplotlib.colors.ListedColormap(colors, name = 'myColorMap') myNorm = matplotlib.colors.BoundaryNorm(boundaries, len(boundaries)) ax = seaborn.heatmap(df, vmin = boundaries[0], vmax = boundaries[-1], cmap = myColorMap, norm = myNorm, xticklabels = True, yticklabels = True, cbar = False, linecolor = 'gray', linewidths = 0.5) pyplot.gcf().colorbar(matplotlib.cm.ScalarMappable(norm = myNorm, cmap = myColorMap), format = "%3d", ax = ax)

Clustermap

Utilise scipy pour faire un double clustering hierarchique.
On peut donner une array numpy ou un dataframe pandas : ar = numpy.array([[5, 4, 5, 6, 5], [6, 3, 4, 5, 1], [1, 6, 4, 2, 7], [4, 1, 2, 3, 0], [2, 7, 5, 3, 8]]) df = pandas.DataFrame(ar, columns = ['A', 'B', 'C', 'D', 'E'], index = ['a', 'b', 'c', 'd', 'e']) seaborn.clustermap(df, cmap = 'Spectral_r', method = 'ward')
df = pandas.DataFrame({'A': [2, 3, 5, 6], 'B': [7, 4, 9, 12], 'C': [1, 3, 4, 8]}, index = ['a', 'b', 'c', 'd']) g = seaborn.clustermap(df, cmap = 'RdBu_r', method = 'average', figsize = (5, 5), cbar_kws = {'ticks': [-1, 0, 1], 'label': 'myLabel'}, row_colors = ['red', 'green', 'red', 'red'], col_colors = ['blue', 'blue', 'gray'], z_score = 1, center = 0, dendrogram_ratio = (0.05, 0.3), colors_ratio = (0.05, 0.01), cbar_pos = (1, 0.2, 0.2, 0.5)) g.ax_heatmap.tick_params('x', rotation = 90) for lab in g.ax_heatmap.get_xticklabels(): lab.set_fontsize(20) lab.set_color('gray') lab.set_fontfamily('monospace')

Copyright python-simple.com
programmer en python, tutoriel python, graphes en python, Aymeric Duclert