PLOTTING WITH
PYPLOT II – HISTOGRAM, FREQUENCY DISTRIBUTION, BOXPLOTS
Chapter – 4 (SECOND PART)
Class – XII
Another example of Histogram:
Look
at the following example that plot histogram from two ndarrays x and y each
having randomly generated some numbers.
1. Plot a histogram from an ndarray x with 7 bins |
|
import
numpy as np import
matplotlib.pyplot as plt x =
np.random.normal(-1,1,100) y =
np.random.normal(-1,1,100) #print(x) plt.hist(x,
bins = 7) plt.show() |
|
2. Plot a histogram from an ndarray y with 5 bins |
|
import
numpy as np import
matplotlib.pyplot as plt x =
np.random.normal(-1,1,100) y =
np.random.normal(-1,1,100) #print(x) plt.hist(y,
bins = 5) plt.show() |
|
3. Plot a cumulative histogram of ndarray x with 20 bins |
|
import
numpy as np import
matplotlib.pyplot as plt x =
np.random.normal(-1,1,100) y =
np.random.normal(-1,1,100) #print(x) plt.hist(x,
bins = 20, cumulative = True) plt.show() |
|
4. Plot ndarray x’s histogram as ‘step’ type histogram with 9 bins |
|
import
numpy as np import
matplotlib.pyplot as plt x =
np.random.normal(-1,1,100) y =
np.random.normal(-1,1,100) plt.hist(x,
bins = 9, histtype = 'step') plt.show()
|
|
5. Plot both ndarray x and y in same histogram |
|
import
numpy as np import
matplotlib.pyplot as plt x =
np.random.normal(-1,1,100) y =
np.random.normal(-1,1,100) plt.hist([x,y]) plt.show() |
|
6. Plot a stacked bar type histogram from both ndarray x and y |
|
(a) regular histogram import
numpy as np import
matplotlib.pyplot as plt x =
np.random.normal(-1,1,100) y =
np.random.normal(-1,1,100) plt.hist([x,y],
histtype = 'barstacked') plt.show() |
|
(b) Cumulative histogram import
numpy as np import
matplotlib.pyplot as plt x =
np.random.normal(-1,1,100) y =
np.random.normal(-1,1,100) plt.hist([x,y],
histtype = 'barstacked', cumulative = True) plt.show()
|
|
7. Plot a horizontal histogram from ndarray y with 20 bins |
|
import
numpy as np import
matplotlib.pyplot as plt x =
np.random.normal(-1,1,100) y =
np.random.normal(-1,1,100) plt.hist(y,bins
= 20, orientation = 'horizontal') plt.show() |
|
Creating
Frequency Polygons:
A frequency polygon is a type of
frequency distribution graph. In a frequency polygon, the number of
observations is marked with a single point at the midpoint of an interval. A straight
line then connects each set of points. Frequency polygons make it easy to
compare two or more distributions on the same set of axes.