
- Matplotlib - Home
- Matplotlib - Introduction
- Matplotlib - Vs Seaborn
- Matplotlib - Environment Setup
- Matplotlib - Anaconda distribution
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - Simple Plot
- Matplotlib - Saving Figures
- Matplotlib - Markers
- Matplotlib - Figures
- Matplotlib - Styles
- Matplotlib - Legends
- Matplotlib - Colors
- Matplotlib - Colormaps
- Matplotlib - Colormap Normalization
- Matplotlib - Choosing Colormaps
- Matplotlib - Colorbars
- Matplotlib - Working With Text
- Matplotlib - Text properties
- Matplotlib - Subplot Titles
- Matplotlib - Images
- Matplotlib - Image Masking
- Matplotlib - Annotations
- Matplotlib - Arrows
- Matplotlib - Fonts
- Matplotlib - Font Indexing
- Matplotlib - Font Properties
- Matplotlib - Scales
- Matplotlib - LaTeX
- Matplotlib - LaTeX Text Formatting in Annotations
- Matplotlib - PostScript
- Matplotlib - Mathematical Expressions
- Matplotlib - Animations
- Matplotlib - Celluloid Library
- Matplotlib - Blitting
- Matplotlib - Toolkits
- Matplotlib - Artists
- Matplotlib - Styling with Cycler
- Matplotlib - Paths
- Matplotlib - Path Effects
- Matplotlib - Transforms
- Matplotlib - Ticks and Tick Labels
- Matplotlib - Radian Ticks
- Matplotlib - Dateticks
- Matplotlib - Tick Formatters
- Matplotlib - Tick Locators
- Matplotlib - Basic Units
- Matplotlib - Autoscaling
- Matplotlib - Reverse Axes
- Matplotlib - Logarithmic Axes
- Matplotlib - Symlog
- Matplotlib - Unit Handling
- Matplotlib - Ellipse with Units
- Matplotlib - Spines
- Matplotlib - Axis Ranges
- Matplotlib - Axis Scales
- Matplotlib - Axis Ticks
- Matplotlib - Formatting Axes
- Matplotlib - Axes Class
- Matplotlib - Twin Axes
- Matplotlib - Figure Class
- Matplotlib - Multiplots
- Matplotlib - Grids
- Matplotlib - Object-oriented Interface
- Matplotlib - PyLab module
- Matplotlib - Subplots() Function
- Matplotlib - Subplot2grid() Function
- Matplotlib - Anchored Artists
- Matplotlib - Manual Contour
- Matplotlib - Coords Report
- Matplotlib - AGG filter
- Matplotlib - Ribbon Box
- Matplotlib - Fill Spiral
- Matplotlib - Findobj Demo
- Matplotlib - Hyperlinks
- Matplotlib - Image Thumbnail
- Matplotlib - Plotting with Keywords
- Matplotlib - Create Logo
- Matplotlib - Multipage PDF
- Matplotlib - Multiprocessing
- Matplotlib - Print Stdout
- Matplotlib - Compound Path
- Matplotlib - Sankey Class
- Matplotlib - MRI with EEG
- Matplotlib - Stylesheets
- Matplotlib - Background Colors
- Matplotlib - Basemap
- Matplotlib - Event Handling
- Matplotlib - Close Event
- Matplotlib - Mouse Move
- Matplotlib - Click Events
- Matplotlib - Scroll Event
- Matplotlib - Keypress Event
- Matplotlib - Pick Event
- Matplotlib - Looking Glass
- Matplotlib - Path Editor
- Matplotlib - Poly Editor
- Matplotlib - Timers
- Matplotlib - Viewlims
- Matplotlib - Zoom Window
- Matplotlib Widgets
- Matplotlib - Cursor Widget
- Matplotlib - Annotated Cursor
- Matplotlib - Buttons Widget
- Matplotlib - Check Buttons
- Matplotlib - Lasso Selector
- Matplotlib - Menu Widget
- Matplotlib - Mouse Cursor
- Matplotlib - Multicursor
- Matplotlib - Polygon Selector
- Matplotlib - Radio Buttons
- Matplotlib - RangeSlider
- Matplotlib - Rectangle Selector
- Matplotlib - Ellipse Selector
- Matplotlib - Slider Widget
- Matplotlib - Span Selector
- Matplotlib - Textbox
- Matplotlib Plotting
- Matplotlib - Line Plots
- Matplotlib - Area Plots
- Matplotlib - Bar Graphs
- Matplotlib - Histogram
- Matplotlib - Pie Chart
- Matplotlib - Scatter Plot
- Matplotlib - Box Plot
- Matplotlib - Arrow Demo
- Matplotlib - Fancy Boxes
- Matplotlib - Zorder Demo
- Matplotlib - Hatch Demo
- Matplotlib - Mmh Donuts
- Matplotlib - Ellipse Demo
- Matplotlib - Bezier Curve
- Matplotlib - Bubble Plots
- Matplotlib - Stacked Plots
- Matplotlib - Table Charts
- Matplotlib - Polar Charts
- Matplotlib - Hexagonal bin Plots
- Matplotlib - Violin Plot
- Matplotlib - Event Plot
- Matplotlib - Heatmap
- Matplotlib - Stairs Plots
- Matplotlib - Errorbar
- Matplotlib - Hinton Diagram
- Matplotlib - Contour Plot
- Matplotlib - Wireframe Plots
- Matplotlib - Surface Plots
- Matplotlib - Triangulations
- Matplotlib - Stream plot
- Matplotlib - Ishikawa Diagram
- Matplotlib - 3D Plotting
- Matplotlib - 3D Lines
- Matplotlib - 3D Scatter Plots
- Matplotlib - 3D Contour Plot
- Matplotlib - 3D Bar Plots
- Matplotlib - 3D Wireframe Plot
- Matplotlib - 3D Surface Plot
- Matplotlib - 3D Vignettes
- Matplotlib - 3D Volumes
- Matplotlib - 3D Voxels
- Matplotlib - Time Plots and Signals
- Matplotlib - Filled Plots
- Matplotlib - Step Plots
- Matplotlib - XKCD Style
- Matplotlib - Quiver Plot
- Matplotlib - Stem Plots
- Matplotlib - Visualizing Vectors
- Matplotlib - Audio Visualization
- Matplotlib - Audio Processing
- Matplotlib Useful Resources
- Matplotlib - Quick Guide
- Matplotlib - Cheatsheet
- Matplotlib - Useful Resources
- Matplotlib - Discussion
Matplotlib - Area Plots
An area plot is like a line graph, but instead of just showing the lines, it fills the space below them with colors. It is a type of data visualization that displays quantitative data using shaded areas to represent different categories or groups over a continuous range.
Imagine you have data that changes over time, like sales for different products. An area plot is a way to show how much each product contributes to the total sales at each point in time. It uses shaded areas to represent each product, and when you stack them together, you get a visual picture of how they add up over time.

Area Plots in Matplotlib
In Matplotlib, we can create an area plot using the fill_between() function or the stackplot() function. These functions allows us to customize colors, transparency, and labels to enhance the visual representation of the data.
The fill_between() Function
The fill_between() function shades the area between two specified curves. It is commonly used to highlight specific areas of interest in a plot.
Syntax
Following is the syntax of fill_between() function in Matplotlib −
fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, **kwargs)
Where,
- x is the x-coordinates of the data points.
- y1 is the y-coordinates of the first curve.
- y2 is the y-coordinates of the second curve or the baseline. If not specified, it defaults to 0, creating a fill between the curve and the x-axis.
- where is an optional condition to limit the filling. If provided, only the regions where the condition is True will be filled.
- If interpolate is True, the filled area will be interpolated. If False (default), steps are used to create stairs-like filling.
- If step is not None, it defines the step type to use for interpolation (plotting). Possible values are 'pre', 'post', or 'mid'.
- **kwargs is the additional keyword arguments that control the appearance of the filled region, such as color, alpha, label, etc.
Example
In the following example, we are filling the area between the curves y=x2 and y=x with a sky-blue color, and using the alpha parameter to control the transparency of the filled region. We are then passing label argument to add a legend to the plot −
import matplotlib.pyplot as plt import numpy as np # Generate sample data x = np.linspace(0, 5, 100) y1 = x**2 y2 = x # Fill the region between y1 and y2 with a sky-blue color and set transparency plt.fill_between(x, y1, y2, color='skyblue', alpha=0.4, label='Filled Area') # Plot the curves for y=x^2 and y=x plt.plot(x, y1, label='y=x^2') plt.plot(x, y2, label='y=x') # Add labels to the axes plt.xlabel('X-axis') plt.ylabel('Y-axis') # Add a title to the plot plt.title('Fill Between Example') # Add a legend to identify the filled area and the curves plt.legend() # Show the plot plt.show()
Output
Following is the output of the above code −

The stackplot() Function
The stackplot() function is used to create a stacked area plots. It allows you to represent the cumulative contribution of multiple datasets or categories to a whole over a continuous range. Each category is represented by a filled area, and the combined areas give an overview of the overall trend.
Syntax
Following is the syntax of stackplot() function in Matplotlib −
stackplot(x, *args, labels=(), colors=None, baseline='zero', alpha=1, **kwargs)
Where,
- x is the x-coordinates of the data points.
- *args is the variable-length argument list of y-coordinates for each dataset or category to be stacked.
- labels is a tuple or list of labels for each dataset, used for creating a legend.
- colors is a tuple or list of colors for each dataset. If not specified, Matplotlib will automatically choose colors.
- baseline specifies whether stacking is relative to 'zero' (default) or 'sym' (symmetric around zero).
- alpha specifies the transparency of the filled areas.
- **kwargs is the additional keyword arguments that control the appearance of the filled region, such as color, alpha, label, etc.
Example
In here, we are using the stackplot() function to create a visual representation of three datasets 'y1', 'y2' and'y3' stacked on top of each other. The legend helps identify each dataset in the plot and the transparency effect enhances the visualization of the filled areas −
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) y3 = np.exp(-0.1 * x) * np.sin(x) # Create a stacked area plot plt.stackplot(x, y1, y2, y3, labels=['Sin(x)', 'Cos(x)', 'Exp(-0.1*x)*Sin(x)'], alpha=0.7) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Stacked Area Plot Example') plt.legend() plt.show()
Output
After executing the above code, we get the following output −

Gradient-Filled Area Plot
A gradient-filled area plot is like coloring the space under a curve with a smooth transition of colors. It helps emphasize the shape of the curve and how it changes.
Example
In the following example, we are creating a smooth curve representing the sine function using numpy. We are then using the fill_between() function to fill the area under the curve with a gradient, starting from the x-axis up to the curve −
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 10, 100) y = np.sin(x) plt.fill_between(x, y, color='purple', interpolate=True) plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Gradient-Filled Area Plot') plt.show()
Output
The resulting plot shows a gradual color transition under the curve −

Percentage Stacked Area Plot
A percentage stacked area plot refers to visualizing different parts of a whole over time, where each part is shown as a percentage contribution to the total. It helps to understand how the proportions of various components change relative to the entire set.
Example
In here, we are representing the percentage contribution of various energy sources (such as coal, natural gas, renewable energy, and nuclear) to the overall energy consumption in a country over several years −
import matplotlib.pyplot as plt years = [2010, 2012, 2014, 2016, 2018] # Percentage contribution of coal to total energy consumption coal = [40, 35, 30, 25, 20] # Percentage contribution of natural gas natural_gas = [30, 25, 20, 18, 15] # Percentage contribution of renewable energy renewable_energy = [15, 20, 25, 30, 35] # Percentage contribution of nuclear energy nuclear = [15, 20, 25, 27, 30] plt.stackplot(years, coal, natural_gas, renewable_energy, nuclear, labels=['Coal', 'Natural Gas', 'Renewable Energy', 'Nuclear'], colors=['#1f78b4', '#33a02c', '#a6cee3', '#fdbf6f'], alpha=0.7, baseline='zero') plt.xlabel('Years') plt.ylabel('Percentage of Total Energy Consumption') plt.title('Energy Source Composition Over Time') plt.legend(loc='upper left') plt.show()
Output
The output obtained is as shown below −

Area Plot with Annotations
An area plot with annotations allows you to add textual or graphical elements to the plot, highlighting specific points or regions on it. It provides additional information about significant data points or regions. These annotations can include labels, arrows, or other markers to draw attention to specific area of interest.
Example
In here, we are determining monthly sales data with a light blue filled area, representing the sales trend. We are adding annotations to highlight significant points on the plot, such as a sales peak and the month with the lowest sales. The annotate() function is used to position text and arrows at specific locations −
import numpy as np import matplotlib.pyplot as plt # Simulating monthly sales data months = np.arange(1, 13) sales = np.array([10, 15, 12, 18, 25, 30, 28, 35, 32, 28, 20, 15]) # Create an area plot plt.fill_between(months, sales, color='lightblue', alpha=0.7, label='Monthly Sales') # Add annotations for significant points plt.annotate('Sales Peak', xy=(5, 30), xytext=(6, 32), arrowprops=dict(facecolor='black', shrink=0.05), fontsize=8, ha='center') plt.annotate('Lowest Sales', xy=(11, 15), xytext=(10, 18), arrowprops=dict(facecolor='black', shrink=0.05), fontsize=8, ha='center') # Add labels and title plt.xlabel('Months') plt.ylabel('Sales (in units)') plt.title('Monthly Sales Trend with Annotations') # Show the plot plt.legend() plt.show()
Output
The output obtained is as shown below −
