The Wayback Machine - https://web.archive.org/web/20240721215453/https://www.geeksforgeeks.org/matplotlib-pyplot-table-function-in-python/
Open In App

Matplotlib.pyplot.table() function in Python

Last Updated : 09 Feb, 2023
Improve
Suggest changes
Post a comment
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:

 

Image

 

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:

 

Image

 



Similar Reads

Matplotlib.pyplot.figimage() function in Python
Matplotlib is a widely used library in Python for plotting various graphs, as it provides very efficient ways and easy to understand methods for complex plots also. pyplot is a collection of command style functions that make matplotlib work like MATLAB. figimage() function matplotlib.pyplot.figimage() is a method for adding a non-resampled image to
2 min read
Matplotlib.pyplot.hexbin() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc. Matplotlib.pyplot.hexbin() function The hexbi
2 min read
Matplotlib.pyplot.suptitle() function in Python
Matplotlib is a library in Python and it is a mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.suptitle() Function The suptitle() function in pyplot module of the matplotlib library is used to add a title to the figure. Syntax: matplotlib.pyp
3 min read
Matplotlib.pyplot.xscale() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc. matplotlib.pyplot.xscale() function The xscal
2 min read
Matplotlib.pyplot.setp() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc. Matplotlib.pyplot.setp() function The setp()
2 min read
matplotlib.pyplot.semilogy() function in Python
Matplotlib is the most popular and Python-ready package that is used for visualizing the data. We use matplotlib for plotting high-quality charts, graphs, and figures. matplotlib.pyplot.semilogy() Function The matplotlib.pyplot.semilogy() function in pyplot module of matplotlib library is used to make a plot with log scaling on the y axis. Syntax:
2 min read
Matplotlib.pyplot.figlegend() function in Python
Matplotlib is a Python library used for creating, animations, and editing graphs, plots, and figures using Pyplot. Matplotlib.pyplot has many functions defined in it to use, as per the preference and requirement of the user demands. matplotlib.pyplot.figlegend() function This is used to place a legend on the figure. A legend in Matplotlib is simila
3 min read
matplotlib.pyplot.step() function in Python
The step() function designs the plot such that, it has a horizontal baseline to which the data points will be connected by vertical lines. This kind of plot is used to analyze at which points the change in Y-axis value has occurred exactly with respect to X-axis. This is very useful in discrete analysis. The step plotting can be combined with any o
4 min read
Matplotlib.pyplot.pcolor() function in Python
Matplotlib is the well-known Python package used in data visualization. Numpy is the numerical mathematics extension of Matplotlib. Matplotlib is capable of producing high-quality graphs, charts, and figures. Matplotlib produces object-oriented API for embedding plots into projects using GUI toolkits like Tkinter, wxPython, or Qt. John D. Hunter wa
2 min read
Matplotlib.pyplot.text() function in Python
This function is used to add a text to the axes at location x, y in data coordinates. Syntax: matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)parameters Descriptionx, y:floatThe position to place the text. By default, this is in data coordinates. The coordinate system can be changed using the transform parameter.s :strThe text.fontdict : di
1 min read
Practice Tags :
three90RightbarBannerImg