/**/ Python & MySQL: June 2020

Monday, June 29, 2020

PLOTTING WITH PYPLOT II – HISTOGRAM, FREQUENCY DISTRIBUTION, BOXPLOTS (PART - 2) CLASS - XII

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.


NOTE: This is the notes of Chapter - IV Plotting with Pyplot - II  - Histogram, Frequency, BoxPlots. It is important for every students, who are going to appear in Class - XII CBSE BOARD Exam as well as competitive level exam.

THANK YOU !!!












Friday, June 26, 2020

PLOTTING WITH PYPLOT II – HISTOGRAM, FREQUENCY DISTRIBUTION, BOXPLOTS (PART-1) - CLASS-XII

PLOTTING WITH PYPLOT II – HISTOGRAM, 

FREQUENCY DISTRIBUTION, BOXPLOTS

Chapter – 4 (FIRST PART)

Class – XII


Introduction

In this chapter, we will learn to create histogram, frequency polygons and boxplots using pyplot module of matplotlib library.

Creating Histogram with Pyplot: A histogram is a summarization tool for discrete or continuous data. A histogram provides a visual interpretation of numerical data by showing the number of data points that fall within a specified range of values (called bins). It is similar to a vertical bar graph.

Difference between a Histogram and Bar Chart:

A Bar Chart majorly represents categorical data, they are usually represented using rectangular bars with lengths proportional to the values that they represent.

While histogram on the other hand, is used to describe distribution. Given the set of data, what are their distributions



To draw Histogram in Python following concepts must be clear:

Title: To display heading of the histogram.

Color: To show the color of the bar.

Axis: x-axis and y-axis.

Data: The data can be represented as an array.

Height and width of bars: This is determined based on the analysis. The width of the bar is called bin or intervals.

Border color: To display border color of the bar.

There are various ways to create histogram in python pandas. One of them is using matplotlib python library. Using this library, we can easily create histogram.

So, install matplotlib library using following statement at command prompt:

>pip install matplotlib

After installation we can create histogram. If pip does not work then copy pip.exe file to the folder where we want to run above command or move to the folder of pip.exe then write above command.

Example: Program in python. Develop a python program with below code and execute it.

import numpy as np

import matplotlib.pyplot as plt

 

data = [50,11,21,31,41]

plt.hist([5,15,25,35,45, 55], bins=[0,10,20,30,40,50, 60], weights=[20,10,45,33,6,8], edgecolor="red")

 

plt.show()

 


In the above example,

First argument of hist() method is position (x, y coordinate) of weight, where weight is to be displayed. No of coordinates must match with no of weight otherwise error will generate.

Second argument is interval.

And third argument is weight for bars.

For better understanding we develop the same program with minor change.

import numpy as np

import matplotlib.pyplot as plt

 

data = [50,11,21,31,41]

plt.hist([5,15,25,35,15, 55], bins=[0,10,20,30,40,50, 60], weights=[20,10,45,33,6,8], edgecolor="red")

plt.show()

 

 


Note: At interval (bin) 40 to 50 no bar because we have not mentioned from 40 to 50 in first argument of hist method. Where as in interval 10 to 20 width is being displayed as 16 (10+6 both weight are added) because 15 is twice in first argument.



















CLASS XI HALF YEARLY QP WTH MS 2024

  Half Yearly Examination: 2024-25 Informatics Practices (065) Class- XI       Time Allowed: 3hrs                                     ...