.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples/matplotlib/plot_main07_e_displot.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_e_displot.py: 07.e ``sns.displot`` ------------------------- Figure-level interface for drawing distribution plots onto a FacetGrid. .. GENERATED FROM PYTHON SOURCE LINES 8-142 .. image-sg:: /_examples/matplotlib/images/sphx_glr_plot_main07_e_displot_001.png :alt: C-Reactive Protein :srcset: /_examples/matplotlib/images/sphx_glr_plot_main07_e_displot_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Unnamed: 0 sample timestep feature_values shap_values count 7000.000000 7000.000000 7000.000000 7000.000000 7000.000000 mean 126003.000000 499.500000 3.000000 -0.570943 0.000091 std 72751.329885 288.695612 2.000143 0.410149 0.042353 min 21.000000 0.000000 0.000000 -1.000000 -0.204305 25% 63012.000000 249.750000 1.000000 -0.900000 -0.011614 50% 126003.000000 499.500000 3.000000 -0.800000 0.000515 75% 188994.000000 749.250000 5.000000 0.000000 0.012057 max 251985.000000 999.000000 6.000000 0.600000 0.789859 0. Computing... C-Reactive Protein | .. code-block:: default :lineno-start: 9 import pandas as pd import seaborn as sns import matplotlib as mpl import matplotlib.pyplot as plt from pathlib import Path sns.set_style(style="white") def scalar_colormap(values, cmap, vmin, vmax): """This method creates a colormap based on values. Parameters ---------- values : array-like The values to create the corresponding colors cmap : str The colormap vmin, vmax : float The minimum and maximum possible values Returns ------- scalar colormap """ # Create scalar mappable norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax, clip=True) mapper = mpl.cm.ScalarMappable(norm=norm, cmap=cmap) # Get color map colormap = sns.color_palette([mapper.to_rgba(i) for i in values]) # Return return colormap, norm def scalar_palette(values, cmap, vmin, vmax): """This method creates a colorpalette based on values. Parameters ---------- values : array-like The values to create the corresponding colors cmap : str The colormap vmin, vmax : float The minimum and maximum possible values Returns ------- scalar colormap """ # Create a matplotlib colormap from name # cmap = sns.light_palette(cmap, reverse=False, as_cmap=True) cmap = sns.color_palette(cmap, as_cmap=True) # Normalize to the range of possible values from df["c"] norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax) # Create a color dictionary (value in c : color from colormap) colors = {} for cval in values: colors.update({cval: cmap(norm(cval))}) # Return return colors, norm # Load dataset path = Path('../../datasets/shap') data = pd.read_csv(path / 'shap.csv') data = data[data.features.isin(['C-Reactive Protein'])] # Since the colorbar is discrete, needs to round so that # the amount of bins is small and therefore visible. Would # it be possible to define a continuous colormap? data.feature_values = data.feature_values.round(1) # Show print(data.describe()) # Configuration cmap_name = 'coolwarm' # colormap name # .. note:: The function displot calls the histplot function. However, # the features allowed are count, frequency, probability or # proportion, percent and density. Thus, the median cannot # be computed. # .. note:: The resulting colormap is discrete. Could it be continuous? # Loop for i, (name, df) in enumerate(data.groupby('features')): # Info print("%2d. Computing... %s" % (i, name)) # Get colormap values = df.feature_values cmap, norm = scalar_colormap(values=values, cmap=cmap_name, vmin=values.min(), vmax=values.max()) # Display displot sns.displot(data=df, x='timestep', y='shap_values', hue='feature_values', palette='coolwarm', hue_norm=(values.min(), values.max()), rug=False) # bins """ # Display histplot plt.figure() sns.histplot( data=df, x='timestep', y='shap_values', discrete=(False, False), hue='feature_values', palette=cmap_name, hue_norm=(values.min(), values.max()), cbar=False, cbar_kws=dict(shrink=.75), #pthresh=.05, pmax=.9, bins=100 ) """ # Format figure plt.suptitle(name) plt.tight_layout() plt.legend([], [], frameon=False) # Show only first N if int(i) > 2: break # Show plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 2.329 seconds) .. _sphx_glr_download__examples_matplotlib_plot_main07_e_displot.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_e_displot.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_main07_e_displot.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_