Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are n bins and patches in matplotlib?

Tags:

matplotlib

I read this in the matplotlib tutorial:

n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)

I am wondering what are n, bins, and patches

like image 792
user2153984 Avatar asked May 21 '14 02:05

user2153984


People also ask

What are Matplotlib bins?

It is a type of bar graph. To construct a histogram, the first step is to “bin” the range of values — that is, divide the entire range of values into a series of intervals — and then count how many values fall into each interval. The bins are usually specified as consecutive, non-overlapping intervals of a variable.

What are Matplotlib patches?

The matplotlib. patches. Rectangle class is used to rectangle patch to a plot with lower left at xy = (x, y) with specified width, height and rotation angle.

Why is %Matplotlib inline?

%matplotlib inline sets the backend of matplotlib to the 'inline' backend: With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.


1 Answers

I was going to suggest reading the docs but this provides no extra explanation although I still advise you have a look.

  • n: is the number of counts in each bin of the histogram
  • bins: is the left hand edge of each bin
  • patchesis the individual patches used to create the histogram, e.g a collection of rectangles

The patches can be used to change the properties of individual bars as in these examples. Here is a simple example of its use

import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(size=100)
n, bins, patches = plt.hist(x)

plt.setp(patches[0], 'facecolor', 'g')
plt.show()

enter image description here

In general the n and bins are used for subsequent data analysis as in this demo

like image 199
Greg Avatar answered Oct 11 '22 16:10

Greg