.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_bimodal_kde_2d.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_auto_examples_plot_bimodal_kde_2d.py: 2D bimodal example ==================== This example shows how to use ``lightkde.kde_2d`` and how it compares to ``scipy.stats.gaussian_kde`` for a bimodal bivariate case. .. GENERATED FROM PYTHON SOURCE LINES 9-10 Import packages: .. GENERATED FROM PYTHON SOURCE LINES 10-16 .. code-block:: default import matplotlib.pyplot as plt import numpy as np from scipy.stats import gaussian_kde, multivariate_normal from lightkde import kde_2d .. GENERATED FROM PYTHON SOURCE LINES 17-18 Generate synthetic data from two univariate normal distributions: .. GENERATED FROM PYTHON SOURCE LINES 18-29 .. code-block:: default np.random.seed(42) sample = np.vstack( ( multivariate_normal.rvs(mean=[0, 0], cov=0.3, size=2000), multivariate_normal.rvs( mean=[2, 2], cov=[[0.5, -0.48], [-0.48, 0.5]], size=2000 ), ) ) .. GENERATED FROM PYTHON SOURCE LINES 30-31 Estimate kernel density using ``lightkde``: .. GENERATED FROM PYTHON SOURCE LINES 31-33 .. code-block:: default density_mx, x_mx, y_mx = kde_2d(sample_mx=sample) .. GENERATED FROM PYTHON SOURCE LINES 34-35 Estimate kernel density using ``scipy``: .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: default gkde = gaussian_kde(dataset=sample.T) xy_mx = np.hstack((x_mx.reshape(-1, 1), y_mx.reshape(-1, 1))) scipy_density_mx = gkde.evaluate(xy_mx.T).reshape(x_mx.shape) .. GENERATED FROM PYTHON SOURCE LINES 40-41 Plot the data against the kernel density estimates: .. GENERATED FROM PYTHON SOURCE LINES 41-84 .. code-block:: default # pre-process bins = (30, 30) data_density_mx, xedges, yedges = np.histogram2d( sample[:, 0], sample[:, 1], bins=bins, density=True ) z_min = 0 z_max = np.max(data_density_mx) x_min, x_max = min(xedges), max(xedges) y_min, y_max = min(yedges), max(yedges) # plot cmap = "afmhot_r" fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 3), sharex="all", sharey="all") # data h = ax1.hist2d( sample[:, 0], sample[:, 1], bins=bins, density=True, vmin=z_min, vmax=z_max, cmap=cmap, )[-1] ax1.set_title("data") # lightkde ax2.contourf(x_mx, y_mx, density_mx, levels=50, vmin=z_min, vmax=z_max, cmap=cmap) ax2.set_title("lightkde") # scipy ax3.contourf(x_mx, y_mx, scipy_density_mx, levels=50, vmin=z_min, vmax=z_max, cmap=cmap) ax3.set_xlim(x_min, x_max) ax3.set_ylim(y_min, y_max) ax3.set_title("scipy.stats.gaussian_kde") fig.subplots_adjust(right=0.89) cbar_ax = fig.add_axes([0.90, 0.15, 0.05, 0.7], aspect=30) fig.colorbar(h, cax=cbar_ax, label="density") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_bimodal_kde_2d_001.png :alt: data, lightkde, scipy.stats.gaussian_kde :srcset: /auto_examples/images/sphx_glr_plot_bimodal_kde_2d_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 85-87 The ``scipy`` method oversmooths the kernel density and it is far from the histogram of the data that it is expected to follow. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 5.877 seconds) .. _sphx_glr_download_auto_examples_plot_bimodal_kde_2d.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_bimodal_kde_2d.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_bimodal_kde_2d.ipynb `