Udit Vashisht
Author: Udit Vashisht


Filling Area on Line Plots | Matplotlib Tutorial in Python | Chapter 5

  • 5 minutes read
  • 98 Views
Filling Area on Line Plots | Matplotlib Tutorial in Python | Chapter 5

Matplotlib Tutorial in Python

Filling Area on Line Plots | Chapter 5

Matplotlib Line plots are a good way to represent your data. But sometimes you need more than that to make your data self-explaining. In such a case, filling the area between or under a Maplotlib Line Plot comes into action.

Matplotlib Tutorials in Python - Creating a Simple Line Plot with filled Area in Matplotlib

We will start with creating a simple Matplotlib Line Plot created by plt.plot() and then fill the entire area under it using plt.fill_between().

# matplotlib_area_plots.py

import matplotlib.pyplot as plt

ages = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21]

total_population = [27877307, 24280683, 25258169, 25899454, 24592293, 21217467, 27958147, 20859088, 28882735, 19978972]

plt.plot(ages, total_population)
plt.xlabel("Age")
plt.ylabel("Total Population")
plt.title("Age-wise population of India")
plt.tight_layout()
plt.fill_between(ages, total_population)
plt.show()

matplotlib_tutorial_filling_area__.png

You can also, fill upto a certain area/value and also change the opacity of the fill by passing the value of alpha to plt.fill_between(). We will fill the area in correspondence to the population of 25000000 and above.

plt.fill_between(ages, total_population, 25000000, alpha=0.30)

matplotlib_tutorial_filling_area_.png

Here you can easily see, which age group has population above 25000000 and which has below it.

Customizing Filling under line charts in Matplotlib Python

To understand it better, we will again bring in the example of Cricket . Let us say, India is given a target of 89 runs in ten overs, meaning that the required run-rate is 8.9 runs per over. Now we will plot the run-rate of India at the end of each over as a line chart and then fill the area under and above the required run-rate using plt.fill_between() and will also color code it.

# matplotlib_area_plots.py

import matplotlib.pyplot as plt

overs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
run_rate = [6, 7.5, 8.3, 8, 9.2, 8.5, 9.4, 8.7, 8.8, 8.9]

plt.plot(overs, run_rate)
plt.xlabel("Overs")
plt.ylabel("Run Rate")
plt.title("Over-wise run rate  of India")
plt.tight_layout()
plt.fill_between(overs, run_rate, 8.9, alpha=0.30)
plt.show()

matplotlib_tutorial_filling_area_!_.png

So, above we have plotted the run-rate as a Matplotlib Line Plot and then filled it as per the required run-rate. Now to give it better visualisation, we need to color the area where the run-rate was above the required run-rate as green and the area where the run-rate was below the required run-rate as red. This will give us a clear picture of how the team has performed in every over. For this we will be using numpy and changing the run-rate and required run-rate to arrays using np.array() so that we can check the condition using where.

# matplotlib_area_plots.py

import matplotlib.pyplot as plt
import numpy as np

overs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
run_rate = [6, 7.5, 8.3, 8, 9.2, 8.5, 9.4, 8.7, 8.8, 8.9]

required_run_rate = 8.9
z1 = np.array(run_rate)
z2 = np.array([required_run_rate] * 10)

plt.plot(overs, run_rate)
plt.xlabel("Overs")
plt.ylabel("Run Rate")
plt.title("Over-wise run rate  of India")
plt.tight_layout()

plt.fill_between(overs, run_rate, required_run_rate,
                 where=(z1 < z2),
                 alpha=0.30, color='red', interpolate=True)
plt.fill_between(overs, run_rate, 8.9,
                 where=(z1 >= z2),
                 alpha=0.30, color='green', interpolate=True)

plt.show()

Passing labels to the fill areas

matplotlib_tutorial_filling_area_!!_.png

We can also pass the labels in plt.fill_between() and then call plt.legend() to show the labels .

# matplotlib_area_plots.py

plt.fill_between(overs, run_rate, required_run_rate,
                 where=(z1 < z2),
                 alpha=0.30, color='red', interpolate=True, label='Below RRR')
plt.fill_between(overs, run_rate, 8.9,
                 where=(z1 >= z2),
                 alpha=0.30, color='green', interpolate=True, label='Above RRR')
plt.legend()

matplotlib_tutorial_filling_are!_.png

Table of Contents of Matplotlib Tutorials in Python

Matplotlib Tutorial in Python | Chapter 1 | Introduction

Matplotlib Tutorial in Python | Chapter 2 | Extracting Data from CSVs and plotting Bar Charts

Pie Charts in Python | Matplotlib Tutorial in Python | Chapter 3

Matplotlib Stack Plots/Bars | Matplotlib Tutorial in Python | Chapter 4

Filling Area on Line Plots | Matplotlib Tutorial in Python | Chapter 5

Python Histograms | Matplotlib Tutorial in Python | Chapter 6

Scatter Plotting in Python | Matplotlib Tutorial | Chapter 7

Plot Time Series in Python | Matplotlib Tutorial | Chapter 8

Python Realtime Plotting | Matplotlib Tutorial | Chapter 9

Matplotlib Subplot in Python | Matplotlib Tutorial | Chapter 10

Python Candlestick Chart | Matplotlib Tutorial | Chapter 11

If you have liked our tutorial, there are various ways to support us, the easiest is to share this post. You can also follow us on facebook, twitter and youtube.

In case of any query, you can leave the comment below.

In the next chapter, we will learn about drawing Histograms in Matplotlib.

If you want to support our work. You can do it using Patreon.



Related Posts

Plot Time Series in Python | Matplotlib Tutorial | Chapter 8
By Udit Vashisht

Plot Time Series in Python using Matplotlib

Plot Time Series in Python | Chapter 8

In this tutorial we will learn to plot time series data in Python using Matplotlib. We will also be using pandas dataframe to plot time series data in python from ...

Read More
Python Candlestick Chart | Matplotlib Tutorial | Chapter 11
By Udit Vashisht

Python Candlestick Chart in Matplotlib

Python Candlestick Chart | Chapter 11

A candlestick chart or Japanese candlestick chart is a financial chart used to depict the price movement of securities, derivatives etc. in financial market. In this tutorial we will use Python to plot Candlestick Chart.

A Candlestick Chart ...

Read More
Chapter 3 - Executing a Python Script
By Udit Vashisht

In our previous chapter, we have strongly recommended you to ditch IDLE and use some code-editor and terminal(MacOS/Linux) or Powershell(Windows) to write and execute your python script. In this chapter we will tell you how to do that.

First and foremost for the purpose of this tutorial, create a new ...

Read More
Search