How To Make A Histogram In Python

Histograms are a useful way to visualize data in Python. They are essentially bar plots but with the bars representing ranges of values instead of individual categories. In this tutorial, we will go over how to create a histogram in Python.

Step 1: Import necessary libraries

To create a histogram in Python, we will be using the matplotlib library. We will need to import this library in our script. In addition, we will be using numpy to generate some sample data.

Step 2: Generate some sample data

Before we can create a histogram, we need some data to plot. We will generate some sample data using numpy’s random.randn() function. This function returns random samples from a normal (Gaussian) distribution.

Step 3: Create a histogram using matplotlib

Now that we have our data, we can create a histogram using matplotlib’s hist() function. We’ll pass in our data as the first argument, and specify the number of bins we want in our histogram using the bins parameter. We can also specify the color of our bars using the color parameter.

Step 4: Customize the histogram

We can customize our histogram by adding labels for the x and y axes using the xlabel() and ylabel() functions. We can also add a title to our plot using the title() function.

Step 5: Display the histogram

Finally, we can display our histogram using the show() function.

Congrats! You have successfully created a histogram in Python.

Here is the full code:

And here is the output: