Open In App

Matplotlib.axes.Axes.add_table() in Python

Last Updated : 21 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.add_table() Function

The Axes.add_table() function in axes module of matplotlib library is also used to add a Table to the axes’ tables and return the table.

Syntax: Axes.add_table(self, tab)

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

  • tab: This parameter is the table instances which is to be add.

Returns: This returns the following:

  • table : This method returns the created table.

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

Example 1:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import matplotlib.table as tbl
import numpy as np  
    
data = [[ 66, 17471, 58],
        [ 58, 13945, 164],
        [ 8952, 18, 81],
        [ 7858, 12368],
        [13, 159, 164, 80]]
    
val1 = ('Geek1', 'Geek2', 'Geek3', 'Geek4')
val2 = ['Month % d' % x for x in (5, 4, 3, 2, 1)]
val3 = np.arange(0, 2500, 500)
val4 = 1000
val5 = plt.cm.Greys(np.linspace(0, 0.5, len(val2)))
val6 = len(data)
val7 = np.arange(len(val1)) + 0.3
val8 = 0.4
val9 = np.zeros(len(val1))
    
lista = []
   
fig, ax = plt.subplots()
    
for row in range(val6):
      
    ax.bar(val7, data[row], val8, 
           bottom = val9,
           color = val5[row])
    val9 = val9 + data[row]
      
    lista.append([(x // 50) for x in val9])
       
table = tbl.table(ax, cellText = lista,
                      rowLabels = val2,
                      rowColours = val5,
                      colLabels = val1,
                      loc ='bottom')
ax.add_table(table)
  
plt.subplots_adjust(left = 0.2, bottom = 0.2)
  
ax.set_xticks([])
  
ax.set_title('matplotlib.axes.Axes.add_table() \
function Example', fontweight ="bold")
  
plt.grid()
plt.show()


Output:

Example 2:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import matplotlib.table as tbl
import numpy as np  
    
data = [[ 66, 17471, 58],
        [ 58, 13945, 164],
        [ 8952, 18, 81],
        [ 7858, 12368],
        [13, 159, 164, 80]]
    
val1 = ('Geek1', 'Geek2', 'Geek3', 'Geek4')
val2 = ['Month % d' % x for x in (5, 4, 3, 2, 1)]
val3 = np.arange(0, 2500, 500)
val4 = 1000
val5 = plt.cm.Greys(np.linspace(0, 0.5, len(val2)))
val6 = len(data)
val7 = np.arange(len(val1)) + 0.3
val8 = 0.4
val9 = np.zeros(len(val1))
    
lista = []
   
fig, ax = plt.subplots()
    
for row in range(val6):
      
    ax.bar(val7, data[row], val8, 
           bottom = val9,
           color = val5[row])
    val9 = val9 + data[row]
      
    lista.append([(x // 50) for x in val9])
       
table = tbl.table(ax, cellText = lista,
                      rowLabels = val2,
                      rowColours = val5,
                      colLabels = val1,
                      loc ='bottom')
ax.add_table(table)
  
plt.subplots_adjust(left = 0.2, bottom = 0.2)
  
ax.set_xticks([])
  
ax.set_title('matplotlib.axes.Axes.add_table() \
function Example', fontweight ="bold")
  
plt.grid()
plt.show()


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads