.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/matplotlib/plot_main05_c_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_c_clustermap.py: 05.c ``sns.clustermap`` multiple categories ------------------------------------------- Plot a matrix dataset as a hierarchically-clustered heatmap. .. note:: The hierarchical clustering has been deactivated. .. GENERATED FROM PYTHON SOURCE LINES 10-17 .. code-block:: default :lineno-start: 10 # Libraries import pandas as pd import seaborn as sns import matplotlib.pyplot as plt from matplotlib.pyplot import gcf .. GENERATED FROM PYTHON SOURCE LINES 18-19 Let's load the dataset .. GENERATED FROM PYTHON SOURCE LINES 19-24 .. code-block:: default :lineno-start: 20 # Load dataset networks = sns.load_dataset("brain_networks", index_col=0, header=[0, 1, 2]) .. GENERATED FROM PYTHON SOURCE LINES 25-26 Let's create the network colors .. GENERATED FROM PYTHON SOURCE LINES 26-37 .. code-block:: default :lineno-start: 27 # Create network colors network_labels = networks.columns.get_level_values("network") network_pal = sns.cubehelix_palette(network_labels.unique().size, light=.9, dark=.1, reverse=True, start=1, rot=-2) network_lut = dict(zip(map(str, network_labels.unique()), network_pal)) network_colors = \ pd.Series(network_labels, index=networks.columns) \ .map(network_lut) .. GENERATED FROM PYTHON SOURCE LINES 38-39 Let's create the node colors .. GENERATED FROM PYTHON SOURCE LINES 39-49 .. code-block:: default :lineno-start: 40 # Create node colors node_labels = networks.columns.get_level_values("node") node_pal = sns.cubehelix_palette(node_labels.unique().size) node_lut = dict(zip(map(str, node_labels.unique()), node_pal)) node_colors = \ pd.Series(node_labels, index=networks.columns) \ .map(node_lut) .. GENERATED FROM PYTHON SOURCE LINES 50-51 Let's combine them. .. GENERATED FROM PYTHON SOURCE LINES 51-57 .. code-block:: default :lineno-start: 52 # Combine network_node_colors = \ pd.DataFrame(network_colors) \ .join(pd.DataFrame(node_colors)) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Let's display the clustermap .. GENERATED FROM PYTHON SOURCE LINES 59-87 .. code-block:: default :lineno-start: 60 # Display g = sns.clustermap(networks.corr(), row_cluster=False, col_cluster=False, # turn off clusters row_colors = network_node_colors, # add colored labels col_colors = network_node_colors, # add colored labels linewidths=0, xticklabels=False, yticklabels=False, center=0, cmap="vlag", figsize=(7, 7)) # Add legend for networks for label in network_labels.unique(): g.ax_col_dendrogram.bar(0, 0, color=network_lut[label], label=label, linewidth=0) l1 = g.ax_col_dendrogram.legend(title='Network', loc="center", ncol=5, bbox_to_anchor=(0.53, 0.9), bbox_transform=gcf().transFigure) # Add legend for nodes for label in node_labels.unique(): g.ax_row_dendrogram.bar(0, 0, color=node_lut[label], label=label, linewidth=0) l2 = g.ax_row_dendrogram.legend(title='Node', loc="center", ncol=1, bbox_to_anchor=(0.86, 0.9), bbox_transform=gcf().transFigure) plt.show() .. image-sg:: /_examples/matplotlib/images/sphx_glr_plot_main05_c_clustermap_001.png :alt: plot main05 c clustermap :srcset: /_examples/matplotlib/images/sphx_glr_plot_main05_c_clustermap_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.405 seconds) .. _sphx_glr_download__examples_matplotlib_plot_main05_c_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_c_clustermap.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_main05_c_clustermap.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_