4 min readJul 4, 2021


Matplotlib is one of the most popular Python packages used for data visualization.

It is a cross-platform library for making 2D plots from data in arrays.

Matplotlib is written in Python and makes use of NumPy, the numerical mathematics extension of Python.

It provides an object-oriented API that helps in embedding plots in applications using Python GUI toolkits such as PyQt, WxPythonotTkinter.

It can be used in Python and IPython shells, Jupyter notebook and web application servers also.

Anatomy of a figure

This figure shows the name of several matplotlib elements composing a figure

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator, MultipleLocator
np.random.seed(19680801)X = np.linspace(0.5, 3.5, 100)
Y1 = 3+np.cos(X)
Y2 = 1+np.cos(1+X/0.75)/2
Y3 = np.random.uniform(Y1, Y2, len(X))
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1, aspect=1)
def minor_tick(x, pos):
if not x % 1.0:
return ""
return f"{x:.2f}"
# FuncFormatter is created and used automatically
ax.set_xlim(0, 4)
ax.set_ylim(0, 4)
ax.tick_params(which='major', width=1.0)
ax.tick_params(which='major', length=10)
ax.tick_params(which='minor', width=1.0, labelsize=10)
ax.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25')
ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10)ax.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label="Blue signal", zorder=10)
ax.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label="Red signal")
ax.plot(X, Y3, linewidth=0,
marker='o', markerfacecolor='w', markeredgecolor='k')
ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom')
ax.set_xlabel("X axis label")
ax.set_ylabel("Y axis label")
def circle(x, y, radius=0.15):
from matplotlib.patches import Circle
from matplotlib.patheffects import withStroke
circle = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=1,
edgecolor='black', facecolor=(0, 0, 0, .0125),
path_effects=[withStroke(linewidth=5, foreground='w')])
def text(x, y, text):
ax.text(x, y, text, backgroundcolor="white",
ha='center', va='top', weight='bold', color='blue')
# Minor tick
circle(0.50, -0.10)
text(0.50, -0.32, "Minor tick label")
# Major tick
circle(-0.03, 4.00)
text(0.03, 3.80, "Major tick")
# Minor tick
circle(0.00, 3.50)
text(0.00, 3.30, "Minor tick")
# Major tick label
circle(-0.15, 3.00)
text(-0.15, 2.80, "Major tick label")
# X Label
circle(1.80, -0.27)
text(1.80, -0.45, "X axis label")
# Y Label
circle(-0.27, 1.80)
text(-0.27, 1.6, "Y axis label")
# Title
circle(1.60, 4.13)
text(1.60, 3.93, "Title")
# Blue plot
circle(1.75, 2.80)
text(1.75, 2.60, "Line\n(line plot)")
# Red plot
circle(1.20, 0.60)
text(1.20, 0.40, "Line\n(line plot)")
# Scatter plot
circle(3.20, 1.75)
text(3.20, 1.55, "Markers\n(scatter plot)")
# Grid
circle(3.00, 3.00)
text(3.00, 2.80, "Grid")
# Legend
circle(3.70, 3.80)
text(3.70, 3.60, "Legend")
# Axes
circle(0.5, 0.5)
text(0.5, 0.3, "Axes")
# Figure
circle(-0.3, 0.65)
text(-0.3, 0.45, "Figure")
color = 'blue'
ax.annotate('Spines', xy=(4.0, 0.35), xytext=(3.3, 0.5),
weight='bold', color=color,
ax.annotate('', xy=(3.15, 0.0), xytext=(3.45, 0.45),
weight='bold', color=color,
ax.text(4.0, -0.4, "Made with",
fontsize=10, ha="right", color='.5')

Matplotlib Table in Python With Examples

Hello programmers, today, we will learn about the implementation of Matplotlib tables in Python. The matplotlib.pyplot.table() method is used to create or add a table to axes in python programs. It generates a table used as an extension to a stacked bar chart. Before we move on with various examples and formatting of tables, let me just brief you about the syntax and return type of the Matplotlib table function.

Matplotlib Table in Python is a particular function that allows you to plot a table. So far, there are multiple plotting techniques such as aggregate bars, aggregate line charts, and other ways. By using matplotlib.pyplot.table(), we can add a table to Axes. This table is then plotted with columns as an x-axis and values as the y-axis. Let’s have a look at the syntax and full explanation of code –

Syntax of Matplotlib Table:

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)

Specifying one of cellText or cellColours as a parameter to the matplotlib table function is mandatory. These parameters must be 2D lists, in which the outer lists define the rows and the inner list define the column values of each row. Any other dimensional data will not be accepted by the function.

The table can optionally have row and column headers. rowLabels, rowColours, rowLoc, and colLabels, colColours, colLoc respectively are optional parameters for the function. **kwargs are optional parameters to adjust for the size of the 2-d list passed as a parameter.


  • cellText: The texts to place into the table cells.
  • cellColours: The background colors of the cells.
  • cellLoc: The alignment of the text within the cells. (default: ‘right’)
  • colWidths (optional): The column widths in units of the axes.
  • rowLabels (optional): The text of the row header cells.
  • rowColours (optional): The colors of the row header cells.
  • rowLoc (optional): The text alignment of the row header cells. (default: ‘left’)
  • colLabels (optional): The text of the column header cells.
  • colColours (optional): The colors of the column header cells.
  • colLoc (optional): The text alignment of the column header cells.(default: ‘left’)
  • Loc (optional): This parameter is the position of the cell with respect to ax.
  • bbox (optional): This parameter is the bounding box to draw the table into.
  • edges (optional): This parameter is the cell edges to be drawn with a line.
  • **kwargs: Used to control table properties.