Here you will learn what is the easiest way to plot a histogram in Python. We make use of the seaborn library to create the distribution.
What is a Histogram?
A histogram is a representation of data using bars of different heights where each bar groups numbers represents a ranges. Taller bars mean that more data is in that range.
If you want to learn more about histograms, please visit the following youtube video:
Generating the Distribution Plot in Python
The function is very simple! All it asks you is for 5 parameters:
- A list with the data
- A string with the x-label
- A string with the y-label
- A string with the title
- An integer with the number of bins
- A string with the plot output path
Please see code below:
import seaborn import matplotlib.pyplot as matplotlib import numpy as np def plot_dist(data, x_label, y_label, tittle, number_bins, plot_output): """Plot data distribution Args: data (list): List with data to be plotted. x_label (str): Plot x-label. y_label (str): Plot y-label. tittle (str): Plot tittle. number_bins (int): Number of bins for distribution. plot_output (str): Path to plot output. """ seaborn.set(color_codes=True) matplotlib.figure(1, figsize=(9, 6)) sns_plot = seaborn.distplot(data, kde=False, rug=False, bins=number_bins) sns_plot.set(xlabel=x_label, ylabel=y_label) matplotlib.title(tittle) sns_plot.figure.savefig(plot_output, bbox_inches='tight', dpi=400) matplotlib.close() # set seed for same plot can be re-generated on example presented here using np.random.normal np.random.seed(11) # random sample data = np.random.normal(0.1, 0.5, 5000) plot_dist(data, "Random Variable", "# Frequency", "Random Variable sampled 5000 times", 200, "plot_histogram_python_example.png")
And here is how our plot looks like
If you enjoyed this tutorial and would love to learn about box-plots and how to plot it in Python, please check out the following tutorial.