.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/matplotlib/plot_main05_a_clustermap.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_matplotlib_plot_main05_a_clustermap.py: 05.a ``sns.clustermap`` basic sample ------------------------------------- Plot a matrix dataset as a hierarchically-clustered heatmap. .. note:: The hierarchical clustering has been deactivated. .. GENERATED FROM PYTHON SOURCE LINES 10-83 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /_examples/matplotlib/images/sphx_glr_plot_main05_a_clustermap_001.png :alt: plot main05 a clustermap :srcset: /_examples/matplotlib/images/sphx_glr_plot_main05_a_clustermap_001.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/matplotlib/images/sphx_glr_plot_main05_a_clustermap_002.png :alt: plot main05 a clustermap :srcset: /_examples/matplotlib/images/sphx_glr_plot_main05_a_clustermap_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 A17 A18 A19 A0 3 0 0 0 2 2 2 0 8 4 1 5 3 8 2 3 7 3 3 9 A1 2 4 6 4 2 1 4 8 2 7 3 7 3 4 8 9 1 8 1 9 A2 0 0 3 1 7 6 6 2 5 9 8 0 7 0 0 6 9 2 7 8 A3 4 5 4 2 1 9 0 9 7 1 1 3 2 0 7 2 1 1 1 9 A4 0 1 0 1 6 4 2 2 6 8 8 7 5 9 2 0 2 4 8 9 A5 5 3 8 5 4 2 4 7 3 3 2 9 9 1 0 5 6 3 0 2 A6 5 8 7 9 6 6 1 4 3 9 8 4 4 8 1 8 0 6 1 3 A7 2 8 2 8 8 1 5 9 9 7 0 6 3 9 2 0 4 4 6 3 A8 1 5 6 1 5 7 7 3 3 2 1 9 8 1 0 8 8 7 9 2 A9 1 9 8 4 0 8 2 4 0 4 2 2 2 3 3 5 2 8 1 0 A10 8 6 7 8 0 7 5 9 9 4 0 0 3 0 1 4 1 3 6 9 A11 3 7 8 6 4 4 8 7 0 3 1 8 8 0 0 2 5 9 8 9 A12 8 4 6 0 3 2 9 3 6 5 9 0 3 6 1 5 1 3 1 3 A13 5 8 8 4 6 5 8 4 4 8 8 2 5 2 2 2 3 5 1 6 A14 0 0 4 6 1 8 9 4 3 1 3 3 7 3 3 4 6 4 0 5 A15 8 2 4 7 8 0 7 8 3 4 0 9 8 7 3 4 5 1 2 2 A16 4 6 7 6 8 8 5 2 6 9 1 2 1 6 8 2 2 3 6 3 A17 2 2 8 7 5 7 7 3 5 7 0 0 3 1 7 5 4 2 3 8 A18 4 5 0 4 5 3 1 0 7 5 3 2 7 4 1 7 6 9 4 7 A19 5 5 3 9 0 3 7 8 8 2 6 3 1 2 0 5 2 6 1 8 ['g', 'r', 'r', 'b', 'b', 'g', 'b', 'g', 'r', 'r', 'r', 'b', 'b', 'r', 'g', 'g', 'b', 'r', 'g', 'g'] A0 Group g A1 Group r A2 Group r A3 Group b A4 Group b A5 Group g A6 Group b A7 Group g A8 Group r A9 Group r A10 Group r A11 Group b A12 Group b A13 Group r A14 Group g A15 Group g A16 Group b A17 Group r A18 Group g A19 Group g dtype: object | .. code-block:: default :lineno-start: 11 """ # Display 0 iris = sns.load_dataset("iris") species = iris.pop("species") lut = dict(zip(species.unique(), "rbg")) row_colors = species.map(lut) """ # Libraries import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt # Constants N = 20 C = sns.color_palette("Spectral", n_colors=5, as_cmap=False) # Create data data = np.random.randint(low=0, high=10, size=(N, N)) colors = [np.random.choice(['r', 'g', 'b']) for i in range(N)] series = pd.Series({'A%s'%i:'Group %s'%c for i,c in enumerate(colors)}) # Create DataFrame df = pd.DataFrame(data, index=['A%s'%i for i in range(N)], columns=['A%s'%i for i in range(N)] ) # Show print(df) print(colors) print(series) # Display 1 # --------- # Create colors dictionary col_colors = dict(zip(df.columns, colors)) # Show g = sns.clustermap(df, figsize=(5,5), row_cluster=False, col_cluster=False, #row_colors=col_colors, col_colors=pd.Series(col_colors), linewidths=0, xticklabels=False, yticklabels=False, center=0, cmap="vlag" ) # Display 2 # --------- # Create colors dictionary col_colors = series.map({ 'Group g': 'g', 'Group b': 'b', 'Group r': 'r' }) # Show g = sns.clustermap(df, figsize=(5,5), row_cluster=False, col_cluster=False, #row_colors=col_colors, col_colors=pd.Series(col_colors), linewidths=0, xticklabels=False, yticklabels=False, center=0, cmap="vlag" ) # Show plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.513 seconds) .. _sphx_glr_download__examples_matplotlib_plot_main05_a_clustermap.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_main05_a_clustermap.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_main05_a_clustermap.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_