Skip to main content
TopMiniSite

Back to all posts

How to Fill the Area Under A Curve In Matplotlib?

Published on
5 min read
How to Fill the Area Under A Curve In Matplotlib? image

Best Graphing Tools to Buy in November 2025

1 Mr. Pen Geometry Set with 6 Inch Swing Arm Protractor, Divider, Set Squares, Ruler, Compasses and Protractor, 15 Piece Set

Mr. Pen Geometry Set with 6 Inch Swing Arm Protractor, Divider, Set Squares, Ruler, Compasses and Protractor, 15 Piece Set

  • COMPREHENSIVE 15-PIECE SET FOR ALL GEOMETRY NEEDS & SKILL LEVELS.

  • MADE IN USA, DESIGNED BY MATH EXPERTS FOR RELIABLE QUALITY.

  • CONVENIENT POUCH FOR EASY STORAGE AND PORTABILITY ON-THE-GO.

BUY & SAVE
$11.99
Mr. Pen Geometry Set with 6 Inch Swing Arm Protractor, Divider, Set Squares, Ruler, Compasses and Protractor, 15 Piece Set
2 Nicpro 21PCS Professional Drafting Tools & Geometry Set with Case, Architect Compass & Protractor Set, Metal Pencils, Pens, Scale Ruler Metal Ruler, 5 Drawing Templates for Interior House Plan Design

Nicpro 21PCS Professional Drafting Tools & Geometry Set with Case, Architect Compass & Protractor Set, Metal Pencils, Pens, Scale Ruler Metal Ruler, 5 Drawing Templates for Interior House Plan Design

  • COMPLETE SET: EVERYTHING NEEDED FOR DRAFTING IN ONE CONVENIENT PACKAGE.
  • DURABLE TEMPLATES: FLEXIBLE DESIGNS FOR PRECISE PATTERNS ON ANY SURFACE.
  • EASY STORAGE: STURDY CASE KEEPS TOOLS ORGANIZED AND PORTABLE.
BUY & SAVE
$25.99 $28.99
Save 10%
Nicpro 21PCS Professional Drafting Tools & Geometry Set with Case, Architect Compass & Protractor Set, Metal Pencils, Pens, Scale Ruler Metal Ruler, 5 Drawing Templates for Interior House Plan Design
3 Mr. Pen- Professional Geometry Set, 15 pcs, Geometry Kit for Artists and Students, Geometry Set, Metal Rulers and Compasses, Drawing Tools, Drafting Supplies, Drafting Set, Drafting Tools and Kits

Mr. Pen- Professional Geometry Set, 15 pcs, Geometry Kit for Artists and Students, Geometry Set, Metal Rulers and Compasses, Drawing Tools, Drafting Supplies, Drafting Set, Drafting Tools and Kits

  • COMPLETE GEOMETRY SET: ALL TOOLS NEEDED IN ONE CONVENIENT PACKAGE.
  • DURABLE CASE: PROTECT TOOLS AND KEEP THEM ORGANIZED ON-THE-GO.
  • IDEAL FOR ALL: PERFECT FOR STUDENTS, TEACHERS, AND CREATIVE PROFESSIONALS.
BUY & SAVE
$19.85
Mr. Pen- Professional Geometry Set, 15 pcs, Geometry Kit for Artists and Students, Geometry Set, Metal Rulers and Compasses, Drawing Tools, Drafting Supplies, Drafting Set, Drafting Tools and Kits
4 Mr. Pen- 6 Inch, 2 Pack, Pocket Size Ruler, Small Architectural Scale

Mr. Pen- 6 Inch, 2 Pack, Pocket Size Ruler, Small Architectural Scale

  • COMPACT DESIGN: PORTABLE POCKET-SIZE RULERS PERFECT FOR ON-THE-GO USE.

  • PRECISION ENGINEERING: LASER-CUT PRINTS ENSURE LASTING ACCURACY AND DURABILITY.

  • VERSATILE USE: IDEAL FOR ARCHITECTS, DRAFTERS, AND STUDENTS ALIKE!

BUY & SAVE
$6.95
Mr. Pen- 6 Inch, 2 Pack, Pocket Size Ruler, Small Architectural Scale
5 STAEDTLER 10-Piece Math Set with Metal Compass, 6" Ruler & Triangle Squares, 45° & 30/60° Set Squares, Alphabet Stencil - Measuring Tools

STAEDTLER 10-Piece Math Set with Metal Compass, 6" Ruler & Triangle Squares, 45° & 30/60° Set Squares, Alphabet Stencil - Measuring Tools

  • ALL-IN-ONE GEOMETRY SET FOR PRECISE, ORGANIZED DRAFTING ESSENTIALS.

  • STRONG MATERIALS ENSURE DURABILITY AND ACCURACY FOR REPEATED USE.

  • COMPACT DESIGN WITH CLEAR MARKINGS AIDS STUDENTS IN MASTERING SKILLS.

BUY & SAVE
$7.79
STAEDTLER 10-Piece Math Set with Metal Compass, 6" Ruler & Triangle Squares, 45° & 30/60° Set Squares, Alphabet Stencil - Measuring Tools
6 Nicpro 20PCS Professional Geometry Set with Case, Drafting Tools with Protractor and Compass, Metal Rulers, Triangles, Pens, Pencils, Drawing Supplies, Drafting Kit for Architect Engineer Students

Nicpro 20PCS Professional Geometry Set with Case, Drafting Tools with Protractor and Compass, Metal Rulers, Triangles, Pens, Pencils, Drawing Supplies, Drafting Kit for Architect Engineer Students

  • COMPLETE SET: ALL ESSENTIALS FOR PRECISION DRAWING IN ONE BOX.
  • HIGH-QUALITY COMPASSES: DURABLE, ADJUSTABLE FOR PRECISE CIRCLE CREATION.
  • VERSATILE TOOLS: IDEAL FOR STUDENTS & PROFESSIONALS IN VARIOUS FIELDS.
BUY & SAVE
$21.99
Nicpro 20PCS Professional Geometry Set with Case, Drafting Tools with Protractor and Compass, Metal Rulers, Triangles, Pens, Pencils, Drawing Supplies, Drafting Kit for Architect Engineer Students
7 Akuoly Math Compasses Back-to-School Starter Geometry Kit with 2 Drafting Compasses Protractor Set Squares Ruler and More for Students Maths Study Homework, Blue Set with Carry Case

Akuoly Math Compasses Back-to-School Starter Geometry Kit with 2 Drafting Compasses Protractor Set Squares Ruler and More for Students Maths Study Homework, Blue Set with Carry Case

  • COMPLETE GEOMETRY SET: COMPASSES, PROTRACTOR, SQUARES, AND RULER INCLUDED.
  • PORTABLE CARRY CASE: EASY TO STORE AND TRANSPORT FOR ON-THE-GO LEARNING.
  • IDEAL FOR STUDENTS: PERFECT FOR ALL SCHOOL-LEVEL GEOMETRY TASKS AND PROJECTS.
BUY & SAVE
$8.99
Akuoly Math Compasses Back-to-School Starter Geometry Kit with 2 Drafting Compasses Protractor Set Squares Ruler and More for Students Maths Study Homework, Blue Set with Carry Case
8 Tenare 1 Set Plastic Clear Math Set Includes Protractor Triangle Rulers 12 Inch 6 Inch Straight Geometry Math Transparent Ruler Measuring Tool for School Office Home Supplies(Clear)

Tenare 1 Set Plastic Clear Math Set Includes Protractor Triangle Rulers 12 Inch 6 Inch Straight Geometry Math Transparent Ruler Measuring Tool for School Office Home Supplies(Clear)

  • ALL-IN-ONE MATH TOOLSET: 6 ESSENTIAL PIECES FOR EVERY NEED.
  • ACCURATE MEASUREMENTS: SCALES PRECISE TO 1 DEGREE FOR EASY USE.
  • DURABLE DESIGN: STURDY, LIGHTWEIGHT PLASTIC FOR LONG-LASTING USE.
BUY & SAVE
$5.98 $7.95
Save 25%
Tenare 1 Set Plastic Clear Math Set Includes Protractor Triangle Rulers 12 Inch 6 Inch Straight Geometry Math Transparent Ruler Measuring Tool for School Office Home Supplies(Clear)
9 Angrox Geometric Drawings Templates Measuring Geometry Rulers 15 Pcs with 1 Pack File Bag for Design School Studying Office Building…

Angrox Geometric Drawings Templates Measuring Geometry Rulers 15 Pcs with 1 Pack File Bag for Design School Studying Office Building…

  • COMPLETE SET: INCLUDES 11 TEMPLATES, TOOLS & STORAGE FOR CONVENIENCE.
  • PRECISION MEASUREMENTS: METRIC RULER WITH 1MM ACCURACY FOR EXACTNESS.
  • VERSATILE USE: IDEAL FOR STUDENTS, ARTISTS, ENGINEERS, AND CONTRACTORS.
BUY & SAVE
$15.99 $18.99
Save 16%
Angrox Geometric Drawings Templates Measuring Geometry Rulers 15 Pcs with 1 Pack File Bag for Design School Studying Office Building…
10 Nicpro Carpenter Pencil with Sharpener, Mechanical Pencils Set with 26 Refills, Deep Hole Marker for Construction, Heavy Duty Woodworking Tools for Architect (Black, Red) - With Case

Nicpro Carpenter Pencil with Sharpener, Mechanical Pencils Set with 26 Refills, Deep Hole Marker for Construction, Heavy Duty Woodworking Tools for Architect (Black, Red) - With Case

  • COMPLETE CARPENTER TOOLSET: 2 PENCILS, 26 REFILLS, SHARPENER & BOX.
  • DEEP HOLE ACCESS: ELONGATED TIP EASILY MARKS TIGHT SPACES EFFORTLESSLY.
  • DURABLE & VERSATILE: STRONG 2.8 MM LEAD MARKS ON WOOD, METAL, & MORE.
BUY & SAVE
$8.99 $13.99
Save 36%
Nicpro Carpenter Pencil with Sharpener, Mechanical Pencils Set with 26 Refills, Deep Hole Marker for Construction, Heavy Duty Woodworking Tools for Architect (Black, Red) - With Case
+
ONE MORE?

To fill the area under a curve in Matplotlib, you can follow these steps:

  1. Import the necessary libraries:

import numpy as np import matplotlib.pyplot as plt

  1. Create the x and y data points for the curve:

x = np.linspace(0, 2*np.pi, 100) y = np.sin(x)

  1. Plot the curve using plt.plot():

plt.plot(x, y)

  1. Fill the area under the curve using plt.fill_between():

plt.fill_between(x, y, color='skyblue', alpha=0.4)

The color parameter specifies the color of the filled area, and the alpha parameter controls the opacity.

  1. Customize other aspects of the plot such as labels, title, etc.:

plt.xlabel('x') plt.ylabel('y') plt.title('Area under a curve')

  1. Display the plot:

plt.show()

Following these steps will allow you to create a plot with the area under the curve filled.

What is the significance of the alpha parameter in fill_between?

The alpha parameter in the fill_between function in Python is used to control the transparency of the filled area between two lines or curves.

Setting the alpha parameter allows you to adjust the visibility or opacity of the filled area. This can be useful when you want to highlight certain areas or overlap multiple filled areas without completely obscuring the underlying lines or curves.

The alpha value ranges from 0 to 1, where 0 represents complete transparency (i.e., the filled area becomes invisible) and 1 represents complete opacity (i.e., the filled area is fully visible). Setting values between 0 and 1 allows you to control the level of transparency and find the desired balance between visibility and clarity.

By adjusting the alpha parameter, you can enhance the visual presentation of data and create more visually appealing and informative plots.

What is the syntax to import the Matplotlib library?

The syntax to import the Matplotlib library in Python is as follows:

import matplotlib.pyplot as plt

Here, matplotlib is the library name, pyplot is a sub-library used for creating plots, and plt is an alias or nickname assigned to the pyplot module for convenience.

What is the purpose of the interpolate parameter in fill_between?

The interpolate parameter in the fill_between function is used to specify whether to interpolate the data between given points to create a smooth polygon for filling or to use straight lines to connect the points.

When interpolate is set to True (default), the function will interpolate the data to create a smooth curve between the given points. This is useful when the data points are not evenly spaced or if the desired fill area is not well-defined by the given points.

On the other hand, when interpolate is set to False, the function will use straight lines to connect the given points. This can be useful when the given points already form a well-defined polygon for filling or if a more angular fill area is desired.

In summary, the interpolate parameter allows the user to control the interpolation method used to create the fill area in the fill_between function.

How to fill the area between two curves using fill_between?

To fill the area between two curves using the fill_between function in Python, you can follow these steps:

  1. Import the necessary libraries:

import matplotlib.pyplot as plt import numpy as np

  1. Define the x-values for your data points along with the corresponding y-values for each curve. You can use the np.linspace() function to generate evenly spaced x-values and create corresponding y-values for each curve using some mathematical functions. For example:

x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x)

  1. Create a new figure and axis using the plt.subplots() function:

fig, ax = plt.subplots()

  1. Plot the two curves using the ax.plot() function:

ax.plot(x, y1, label='Curve 1') ax.plot(x, y2, label='Curve 2')

  1. Use the ax.fill_between() function to fill the area between the two curves:

ax.fill_between(x, y1, y2, color='gray', alpha=0.5)

Here, x is the common x-axis values, y1 and y2 are the corresponding y-axis values for the two curves. color specifies the color of the fill, and alpha determines the transparency of the fill.

  1. Finally, add a legend and show the plot:

ax.legend() plt.show()

With these steps, you should be able to fill the area between two curves using fill_between.

How to create a stacked area plot using fill_between?

To create a stacked area plot using fill_between in Python, you can follow these steps:

  1. Import the required libraries:

import matplotlib.pyplot as plt import numpy as np

  1. Define the data for the X-axis (common for all stacked areas) and the Y-axis values for each stacked area:

x = np.arange(0, 10, 0.1) # X-axis values y1 = np.sin(x) # Y-axis values for stacked area 1 y2 = np.cos(x) # Y-axis values for stacked area 2 y3 = np.exp(-x / 2) # Y-axis values for stacked area 3

  1. Calculate the cumulative sum of each Y-axis array to create the stacked area plot:

y1_stack = np.cumsum(y1) y2_stack = np.cumsum(y2) y3_stack = np.cumsum(y3)

  1. Create the stacked area plot using fill_between:

plt.fill_between(x, 0, y1_stack, alpha=0.5, label='Area 1') plt.fill_between(x, y1_stack, y2_stack, alpha=0.5, label='Area 2') plt.fill_between(x, y2_stack, y3_stack, alpha=0.5, label='Area 3')

  1. Add labels, title, and legend to the plot:

plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Stacked Area Plot') plt.legend()

  1. Show the plot:

plt.show()

Putting it all together, here's the complete code to create a stacked area plot using fill_between:

import matplotlib.pyplot as plt import numpy as np

x = np.arange(0, 10, 0.1) # X-axis values y1 = np.sin(x) # Y-axis values for stacked area 1 y2 = np.cos(x) # Y-axis values for stacked area 2 y3 = np.exp(-x / 2) # Y-axis values for stacked area 3

y1_stack = np.cumsum(y1) y2_stack = np.cumsum(y2) y3_stack = np.cumsum(y3)

plt.fill_between(x, 0, y1_stack, alpha=0.5, label='Area 1') plt.fill_between(x, y1_stack, y2_stack, alpha=0.5, label='Area 2') plt.fill_between(x, y2_stack, y3_stack, alpha=0.5, label='Area 3')

plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Stacked Area Plot') plt.legend() plt.show()

This code will produce a stacked area plot showing different areas in different colors, with the X-axis representing the independent variable and the Y-axis representing the cumulative sum of the values.