

.. _sphx_glr_gallery_axes_grid1_scatter_hist.py:


============
Scatter Hist
============





.. image:: /gallery/axes_grid1/images/sphx_glr_scatter_hist_001.png
    :align: center





.. code-block:: python

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.axes_grid1 import make_axes_locatable

    # Fixing random state for reproducibility
    np.random.seed(19680801)


    # the random data
    x = np.random.randn(1000)
    y = np.random.randn(1000)


    fig, axScatter = plt.subplots(figsize=(5.5, 5.5))

    # the scatter plot:
    axScatter.scatter(x, y)
    axScatter.set_aspect(1.)

    # create new axes on the right and on the top of the current axes
    # The first argument of the new_vertical(new_horizontal) method is
    # the height (width) of the axes to be created in inches.
    divider = make_axes_locatable(axScatter)
    axHistx = divider.append_axes("top", 1.2, pad=0.1, sharex=axScatter)
    axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter)

    # make some labels invisible
    axHistx.xaxis.set_tick_params(labelbottom=False)
    axHisty.yaxis.set_tick_params(labelleft=False)

    # now determine nice limits by hand:
    binwidth = 0.25
    xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
    lim = (int(xymax/binwidth) + 1)*binwidth

    bins = np.arange(-lim, lim + binwidth, binwidth)
    axHistx.hist(x, bins=bins)
    axHisty.hist(y, bins=bins, orientation='horizontal')

    # the xaxis of axHistx and yaxis of axHisty are shared with axScatter,
    # thus there is no need to manually adjust the xlim and ylim of these
    # axis.

    axHistx.set_yticks([0, 50, 100])

    axHisty.set_xticks([0, 50, 100])

    plt.draw()
    plt.show()

**Total running time of the script:** ( 0 minutes  0.342 seconds)



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

     :download:`Download Python source code: scatter_hist.py <scatter_hist.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: scatter_hist.ipynb <scatter_hist.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
