Plotting Tutorial in Python with Matplolib.pyplot – Part 2

This is Part 2 of Plotting Tutorial in Python with Matplotlib.pyplot. You can find Part 1 here.

Here, we cover the following

  1. Example 2 – Plotting the Heart Curve
  2. Example 3 – The Figure 8 Curve
  3. Working with Subplots
  4. Working with Scatterplots
  5. Working with Histograms
  6. Creating a Bar Plot


1. Example 2 – Plotting the Heart Curve

This is a curve with the shape of a heart! It is based on the formulas below:

The Python code for the heart plot is given in the figure below:

# We plot the line with RGB tuple (red = 1, green = 0.2, blue = 0.5)
# and 20pt line width
plt.plot(x, y, c='red', lw=18)

# Add features to our figure
plt.title('My Heart!')


The output is shown below:

Heart Curve output

I recommend you try it. Also change up things a bit to see how it appears. For example, change the color, linewidth etc.


2. Example 3 – The Figure 8 Curve

This is another interesting curve to create. Besides, it is quite easy to work with. It is based on the following trigonometric equations:

The Python code is given below:

# 8. Example 3 - The Figure 8 Curve
t = np.linspace(0, 2 * np.pi, 40)
x = np.sin(t)
y = np.sin(t) * np.cos(t)

# plt.subplot(2,1,1)
plt.plot(x, y, 
         markersize = 10,
         linewidth = 15,
         color = 'green',


The output of the code is given below:


3. Working with Subplots

Subplots allow you to display two or more plots in a grid form with each curve in its own rectangular plot. The plt.subplot() function takes at least 3 inputs n, m and i and creates a figure with a n by m grid of subplots and then sets the ith subplot (counting across the rows) as the current plot (ie. current axes object).

Once, you call the subplot function, then the next plot following it is plotted on the particular subplot specified.

Let’s take and example

We’ll plot four different curves in 4 subplots. The equations of the curves is given as:

The Python code is given below;

t = np.linspace(0,4,200)
f1 = 1/2 + np.sin(2 * np.pi * t) / np.pi
f2 = f1  - np.sin(4 * np.pi * t) / 2 * np.pi
f3 = f2  + np.sin(6 * np.pi * t) / 3 * np.pi
f4 = f3  - np.sin(8 * np.pi * t) / 4 * np.pi

plt.plot(t, f1)
plt.title('N = {}'.format(1))

plt.plot(t, f2)
plt.title('N = {}'.format(2))

plt.title('N = {}'.format(3))

plt.plot(t, f4)
plt.title('N = {}'.format(4))



The output of the code is given below:



4. Working with Scatterplots

Scatterplots are used for a number of math applications. The code below shows a scatterplot. Most part of the code are explained as comments within the code.

# Working with Scatter Plot

# Set the number of dots in the plot
N = 200

# Create a random x and y cordinates sampled uniformly from 0 to 1
x = np.random.rand(N)
y = np.random.rand(N)

# Create a random array sampled uniformly from [20, 120]
# 'size' array is used below to set the size of each dot
size = 100 * np.random.rand(N) + 20

# Create a rondom 4-tuples sampled uniformly from [0 to 1]

# The colors array is used to set the color of each dot
colors = np.random.rand(N,4)

# Create a figure that is of size 12 by 5 and create a scatter plot
plt.figure(figsize=(12, 5))
plt.scatter(x, y, c=colors, s = size)
plt.title('Scatter Plot')


The output of the scatter plot is shown below


5. Working with Histograms

A histogram is a diagram made up of rectangles placed side by side. The area of the rectangles are proportional to the frequency of a variable while the width is equal to the class interval.

The code below displays a histogram.

# Working with Histograms
samples = np.random.randn(10000)
plt.hist(samples, bins=20, 
         color=(0.1, 0.8, 0.1)
plt.title('Random Samples - Normal Distribution')

#Plot a red line
x = np.linspace(-4, 4, 100)
y = 1/(2*np.pi)**0.5 * np.exp(-x**2/2)
plt.plot(x, y, 'r', alpha = 0.8)


The output of the histogram plot is given below


6. Creating a Bar Plot

A bar plot also called a bar graph or bar chart is used to present a categerical data with rectangular bars along with corresponding heights that is proportional to the values that they represent.

The code below creates a bar chart.

# Working With Bar Plots
months = range(1,13)
precipitation = [88.8,118.8,201.0,126.5,102.2,46.4,40.8,21.0,29.4,104.8,192.0,160.6],precipitation,edgecolor='red')

plt.yticks(range(0, 300, 50))
plt.grid(True, alpha=0.9, 
plt.title('Precipitation in Onitsha, 2015')
plt.ylabel('Total Precipitation (mm) ')


The output of the code is given below:

If you have come this far, you’ve done great. I recommend you do it over again with Part 1 so it becomes clearer.

Feel free to visit my Youtube channel for more video lessons

2 Comments on “Plotting Tutorial in Python with Matplolib.pyplot – Part 2”

Leave a Reply

Your email address will not be published. Required fields are marked *