Skip to content
Related Articles

Related Articles

matplotlib.axes.Axes.stackplot() in Python

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 13 Apr, 2020

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.stackplot() Function

The Axes.stackplot() function in axes module of matplotlib library is used to create a stacked area plo.

Syntax: Axes.stackplot(axes, x, *args, labels=(), colors=None, baseline=’zero’, data=None, **kwargs)

Parameters: This method accept the following parameters that are described below:

  • x: This parameter is the sequence of x coordinates.
  • y: This parameter is the sequence of y coordinates.
  • baseline: This parameter is the base line{‘zero’, ‘sym’, ‘wiggle’, ‘weighted_wiggle’}.
  • colors: This parameter is the list or tuple of colors.
  • label: This parameter is the label to assign to each data series.

Returns: This returns the following:

  • list: This returns the list of PolyCollection instances, one for each element in the stacked area plot.

Below examples illustrate the matplotlib.axes.Axes.stackplot() function in matplotlib.axes:

Example-1:




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
  
x = [1, 2, 3, 4, 5]
y1 = [1, 1, 2, 3, 5]
y2 = [0, 4, 2, 6, 8]
y3 = [1, 3, 5, 7, 9]
  
y = np.vstack([y1, y2, y3])
  
labels = ["Geeks1 ", "Geeks2", "Geeks3"]
  
fig, ax = plt.subplots()
ax.stackplot(x, y1, y2, y3, 
             labels = labels)
  
ax.legend(loc ='upper left')
  
ax.set_title('matplotlib.axes.Axes.stackplot Example')
plt.show()

Output:

Example-2:




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
  
  
def GFG(n, m):
      
    def geeks(a):
        x = 1 / (.1 + np.random.random())
        y = 2 * np.random.random() - .5
        z = 10 / (.1 + np.random.random())
          
        for i in range(m):
            w = (i / m - y) * z
            a[i] += x * np.exp(-w * w)
              
    a = np.zeros((m, n))
      
    for i in range(n):
        for j in range(5):
            geeks(a[:, i])
              
    return a
  
  
test = GFG(3, 100)
  
fig, ax = plt.subplots()
ax.stackplot(range(100), test.T,
             baseline ='wiggle')
  
ax.set_title('matplotlib.axes.Axes.stackplot Example')
plt.show()


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!