Introduction to Ogive Graph in Python

Filed Under: Python
Ogive Graph FeaImg

In this tutorial, we will learn about ogive graphs and we will also look at their implementation. These graphs help estimate how many numbers lie below or above a particular value in data. Another name for the graphs is cumulative frequency graph.


Code Implementation for Ogive Plot

We will be executing the following code snippets to create an ogive for a dataset in Python.

Creating a Dataset

We will be making use of numpy module and make use of the random.randint function to get a certain amount of integers in a certain range.

import numpy as np
#Get 1000 random integers btw 0 and 20
data = np.random.randint(0, 20, 1000)

Next, we make use of the histogram function to automatically find the classes along with their frequencies.

Then we can use matplotlib library to actually plot the ogive graph using the code below. The chart will change on the basis of the number of bins set in the histogram function.

import matplotlib.pyplot as plt 

values, base = np.histogram(data, bins=15)
cumulative = np.cumsum(values)
plt.plot(base[:-1], cumulative, 'ro-')
plt.show()
Ogive Chart Output 1
Ogive Chart Output 1

Let’s change the count of bins from 15 to 30 and see how different the chart looks now. The code and output for the same are below.

import matplotlib.pyplot as plt 

values, base = np.histogram(data, bins=30)
cumulative = np.cumsum(values)
plt.plot(base[:-1], cumulative, 'ro-')
plt.show()
Ogive Chart Output 2
Ogive Chart Output 2

One thing which you might be wondering is, what exactly is the ro- parameter in the plot function.

The ro- parameter describes three important things:

  1. The color of the plot ( r -> red )
  2. The marker for the bins that need to be plotted ( o -> circles )
  3. The connecting line style which connects the dots together

Let’s try to change and customize the color of the plot using the code below.

import matplotlib.pyplot as plt 

values, base = np.histogram(data, bins=30)
cumulative = np.cumsum(values)
plt.plot(base[:-1], cumulative, 'g*-')

plt.show()
Ogive Chart Output 3
Ogive Chart Output 3

As the final step, let us try to plot multiple ogive charts in a single plot using the code below.

We are gonna make things interesting and approach the second plot differently. We reverse the plot by making use of the flipud function.

import matplotlib.pyplot as plt 
import numpy as np

data = np.random.randint(0, 20, 1000)
values, base = np.histogram(data, bins=30)
cumulative = np.cumsum(values)
plt.plot(base[:-1], cumulative, 'g*-')

data = np.random.randint(0, 20, 2000)
values, base = np.histogram(data, bins=40)
cumulative = np.cumsum(values)
cumulative = np.flipud(cumulative)
plt.plot(base[:-1], cumulative, 'ro-')

plt.show()
Ogive Chart Output 4
Ogive Chart Output 4

Conclusion

I hope you loved reading the above tutorial about understanding what an ogive graph is and step by step guide to creating plots.

Thank you for reading! I would recommend you to read the following tutorials as well:

  1. Python diagrams module – Creating Architecture Diagrams
  2. Missingno – Visualize Missing Values In Python
  3. Python Plotly Tutorial

close
Generic selectors
Exact matches only
Search in title
Search in content