Open In App

Matplotlib.pyplot.table() function in Python

Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib.pyplot.table() is a subpart of matplotlib library in which a table is generated using the plotted graph for analysis. This method makes analysis easier and more efficient as tables give a precise detail than graphs. The matplotlib.pyplot.table creates tables that often hang beneath stacked bar charts to provide readers insight into the data generated by the above graph.

Syntax: matplotlib.pyplot.table(cellText=None, cellColours=None, cellLoc=’right’, colWidths=None,rowLabels=None, rowColours=None, rowLoc=’left’, colLabels=None, colColours=None, colLoc=’center’, loc=’bottom’, bbox=None, edges=’closed’, **kwargs) 
 

Example 1: Consider a graph analyzing the increase in price of crops in months. The following code is for a non linear graph.

Python3




# importing necessary packagess
import numpy as np
import matplotlib.pyplot as plt
 
 
# input data values
data = [[322862, 876296, 45261, 78237232451],
        [58230, 1131397804599308, 516044],
        [891358552, 15258, 497981, 603535],
        [2441573858, 150656, 1932369638],
        [139361, 831509, 43164, 738052269]]
 
# preparing values for graph
columns = ('Soya', 'Rice', 'Wheat', 'Bakri', 'Ragi')
rows = ['%d months' % x for x in (50, 35, 20, 10, 5)]
values = np.arange(0, 2500, 500)
value_increment = 1000
 
# Adding pastel shades to graph
colors = plt.cm.Oranges(np.linspace(22, 3, 12))
n_rows = len(data)
index = np.arange(len(columns)) + 0.3
bar_width = 0.4
 
# Initializing vertical-offset for the graph.
y_offset = np.zeros(len(columns))
 
# Plot bars and create text labels for the table
cell_text = []
 
for row in range(n_rows):
    plt.plot(index, data[row], bar_width, color=colors[row])
    y_offset = y_offset + data[row]
    cell_text.append(['%1.1f' % (x / 1000.0) for x in y_offset])
 
# Reverse colors and text labels to display table contents with
# color.
colors = colors[::-1]
cell_text.reverse()
 
# Add a table at the bottom
the_table = plt.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom')
 
# make space for the table:
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.ylabel("Price in Rs.{0}'s".format(value_increment))
plt.yticks(values * value_increment, ['%d' % val for val in values])
plt.xticks([])
plt.title('Cost price increase')
 
# plt.show()-display graph
# Create image. plt.savefig ignores figure edge and face color.
fig = plt.gcf()
plt.savefig('pyplot-table-original.png',
            bbox_inches='tight',
            dpi=150)


 
 

Output:

 

 

Example 2: Let’s consider the rise in price of milk of different brands in past years

 

Python3




# importing necessary packagess
import numpy as np
import matplotlib.pyplot as plt
 
 
# input data values
data = [[322862, 876296, 45261, 78237232451],
        [58230, 1131397804599308, 516044],
        [891358552, 15258, 497981, 603535],
        [2441573858, 150656, 1932369638],
        [139361, 831509, 43164, 738052269]]
 
# preparing values for graph
columns = ('Gokul', 'Kwality', 'Bakhri', 'Arun', 'Amul')
rows = ['%d months' % x for x in (50, 35, 20, 10, 5)]
values = np.arange(0, 2500, 500)
value_increment = 1000
 
# Adding pastel shades to graph
colors = plt.cm.Oranges(np.linspace(22, 3, 12))
n_rows = len(data)
index = np.arange(len(columns)) + 0.3
bar_width = 0.4
 
# Initializing vertical-offset for the graph.
y_offset = np.zeros(len(columns))
 
# Plot bars and create text labels for the table
cell_text = []
for row in range(n_rows):
    plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
    y_offset = y_offset + data[row]
    cell_text.append(['%1.1f' % (x / 1000.0) for x in y_offset])
 
# Reverse colors and text labels to display table contents with
# color.
colors = colors[::-1]
cell_text.reverse()
 
# Add a table at the bottom
the_table = plt.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom')
 
# make space for the table:
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.ylabel("Rise in Rs's".format(value_increment))
plt.yticks(values * value_increment, ['%d' % val for val in values])
plt.xticks([])
plt.title('Cost of Milk of diff. brands')
 
# plt.show()-display graph
# Create image. plt.savefig ignores figure edge and face color.
fig = plt.gcf()
plt.savefig('pyplot-table-original.png',
            bbox_inches='tight',
            dpi=150)


 
 

Output:

 

 



Last Updated : 09 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads