Udit Vashisht
Author: Udit Vashisht


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

  • Aug. 27, 2019, 12:18 p.m.
  • 5 minutes read
  • 15 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

Python unittest module - How to test your python code?
By Udit Vashisht | 3 weeks, 3 days ago

Python unittest module

Why to unit test your python source code?

All programmers want their code to be impeccable, but as the saying goes, to err is human, we make mistakes and leave bugs in our source code. Here is where the unit testing comes to our rescue. If you ...

Read More
How to host and schedule your python script on PythonAnywhere?
By Udit Vashisht | 9 months ago

In our last post here, we learned how to create a python script which will automatically delete your messages from gmail account based on a query.

The complete ready-to-use code can be found here.

Follow the steps in the above tutorial to download the ‘credentials.json’ file and ...

Read More
Matplotlib Tutorial in Python | Chapter 2 | Extracting Data from CSVs and plotting Bar Charts.
By Udit Vashisht | 1 month, 3 weeks ago

Matplotlib Tutorial in Python

Chapter 2 | Extracting Data from CSVs and plotting Bar Charts

In the last chapter, we learned to draw simple plots in Matplotlib and further customizing it.In this chapter we will be learning to extract data from external sources like CSV and creating ...

Read More
Search