.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/matplotlib/plot_main07_b_2dbin_heatmap.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_main07_b_2dbin_heatmap.py: 07.b ``stats.2dbin`` and ``sns.heatmap`` ---------------------------------------- Use binned_statistic_2d and display using heatmap. .. GENERATED FROM PYTHON SOURCE LINES 8-41 .. image-sg:: /_examples/matplotlib/images/sphx_glr_plot_main07_b_2dbin_heatmap_001.png :alt: plot main07 b 2dbin heatmap :srcset: /_examples/matplotlib/images/sphx_glr_plot_main07_b_2dbin_heatmap_001.png :class: sphx-glr-single-img .. code-block:: default :lineno-start: 9 import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt from pathlib import Path from scipy import stats # Load data path = Path('../../datasets/shap/') data = pd.read_csv(path / 'shap.csv') # Get x, y and z x = data.timestep y = data.shap_values z = data.feature_values # Show data[['timestep', 'shap_values', 'feature_values']] # Binned stats statistic, x_edge, y_edge, binnumber = \ stats.binned_statistic_2d(x=x, y=y, values=z, statistic='count', bins=[20, x.nunique()], expand_binnumbers=False) # Display sns.heatmap(statistic, annot=True, linewidth=.5, cmap='coolwarm', annot_kws={"fontsize":6}, square=False) # Show plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.430 seconds) .. _sphx_glr_download__examples_matplotlib_plot_main07_b_2dbin_heatmap.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_main07_b_2dbin_heatmap.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_main07_b_2dbin_heatmap.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_