Histogram with Boxplot above in Python

12,298

Solution 1

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="ticks")

x = np.random.randn(100)

f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, 
                                    gridspec_kw={"height_ratios": (.15, .85)})

sns.boxplot(x, ax=ax_box)
sns.distplot(x, ax=ax_hist)

ax_box.set(yticks=[])
sns.despine(ax=ax_hist)
sns.despine(ax=ax_box, left=True)

enter image description here

Solution 2

Expanding on the answer from @mwaskom, I made a little adaptable function.

import seaborn as sns
def histogram_boxplot(data, xlabel = None, title = None, font_scale=2, figsize=(9,8), bins = None):
    """ Boxplot and histogram combined
    data: 1-d data array
    xlabel: xlabel 
    title: title
    font_scale: the scale of the font (default 2)
    figsize: size of fig (default (9,8))
    bins: number of bins (default None / auto)

    example use: histogram_boxplot(np.random.rand(100), bins = 20, title="Fancy plot")
    """

    sns.set(font_scale=font_scale)
    f2, (ax_box2, ax_hist2) = plt.subplots(2, sharex=True, gridspec_kw={"height_ratios": (.15, .85)}, figsize=figsize)
    sns.boxplot(data, ax=ax_box2)
    sns.distplot(data, ax=ax_hist2, bins=bins) if bins else sns.distplot(data, ax=ax_hist2)
    if xlabel: ax_hist2.set(xlabel=xlabel)
    if title: ax_box2.set(title=title)
    plt.show()

histogram_boxplot(np.random.randn(100), bins = 20, title="Fancy plot", xlabel="Some values")

Image

Share:
12,298
Isura Nirmal
Author by

Isura Nirmal

Updated on June 29, 2022

Comments

  • Isura Nirmal
    Isura Nirmal almost 2 years

    Hi I wanted to draw a histogram with a boxplot appearing the top of the histogram showing the Q1,Q2 and Q3 as well as the outliers. Example phone is below. (I am using Python and Pandas) enter image description here

    I have checked several examples using matplotlib.pyplot but hardly came out with a good example. And I also wanted to have the histogram curve appearing like in the image below. enter image description here

    I also tried seaborn and it provided me the shape line along with the histogram but didnt find a way to incorporate with boxpot above it.

    can anyone help me with this to have this on matplotlib.pyplot or using pyplot

  • Isura Nirmal
    Isura Nirmal over 8 years
    Thank You! Exactly the way I was looking for. But it came with some problems, There are gaps between the columns and I tried to use bins argument but no luck. Also I want the y axis to have the frequency not the probability. I didnt find any way to do this in seaborn documentation. Can you help?
  • iayork
    iayork over 8 years
    These are completely separate from your original question, so you should ask them as two new questions.
  • Isura Nirmal
    Isura Nirmal over 8 years
    Dotn worry guys. I actually find the solutions after some time of search. Thank you for your corporation.
  • trock2000
    trock2000 over 6 years
    any idea how to combine 8 separate instances of the above-styled plots into a single plot?
  • DaveL17
    DaveL17 almost 3 years
    Please repair your code formatting and provide some context why your solution is preferable to the other answers provided.