search
Search
Unlock 100+ guides
search toc
close
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
Matplotlib
83 guides
keyboard_arrow_down
check_circle
Mark as learned
thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

# Drawing a histogram in Matplotlib

schedule Aug 10, 2023
Last updated
local_offer
PythonMatplotlib
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

We can use the `ax.hist(~)` method to draw a basic histogram in Matplotlib.

# Drawing a basic histogram

## Object Oriented Interface

To plot a histogram showing the distribution of some numbers:

``` nums = [1,1,2,3,3,3,3,3,4,5,6,6,6,7,8,8,9,10,12,12,12,12,14,18]fig,ax = plt.subplots()ax.hist(nums)ax.set_xlabel('Number')ax.set_ylabel('Frequency')plt.show() ```

This produces the following output:

## Pyplot Interface

To plot the same histogram as above, but using Pyplot Interface:

``` nums = [1,1,2,3,3,3,3,3,4,5,6,6,6,7,8,8,9,10,12,12,12,12,14,18]_ = plt.hist(nums)_ = plt.xlabel('Number')_ = plt.ylabel('Frequency')plt.show() ```

It is good practice to assign the various plotting statements to a dummy variable `_` so that we do not display any unnecessary output.

# Setting the number of bins

By default the number of bins is 10, however, we can customize this using the `bins` argument:

``` nums = [1,1,2,3,3,3,3,3,4,5,6,6,6,7,8,8,9,10,12,12,12,12,14,18]fig,ax = plt.subplots()ax.hist(nums, bins=5)plt.show() ```

Now the output histogram only has 5 bins:

# Multiple variables on the same histogram

We can plot multiple variables on the same histogram:

``` nums1 = [1,1,2,3,3,3,3,3,4,5,6,6,6,7,8,8,9,10,12,12,12,12,14,18]nums2= [10,12,13,13,14,14,15,15,15,16,17,18,20,22,23]fig,ax = plt.subplots()ax.hist(nums1, label="nums1", histtype="step")ax.hist(nums2, label="nums2", histtype="step")plt.legend()plt.show() ```

The `histtype="step"` means we only see the outlines of each histogram. This enables us to easily see the distribution for each variable when there is overlap between the two.

# Visualizing the distribution of random numbers

We want to visualize the distribution of random numbers that we generate.

We generate 100 random integers between 0 (inclusive) and 200 (exclusive):

``` x = np.random.randint(low=0, high=200, size=100)x array([122, 76, 147, 160, 100, 173, 62, 21, 50, 150, 75, 19, 87, ... 83, 43, 184, 153, 148, 198, 88, 8, 196]) ```

We then draw a histogram showing the distribution of the data-points:

``` plt.hist(x, range=(0,200), bins=10, rwidth=0.9) ```

Here,

• `range` is a tuple indicating the lower and upper bound

• `bins` is number of bars you want

• `rwidth` is the width of each bin. If we set `rwidth=1`, then there will be no gap between the bins.

The output is as follows:

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!