Print

Tables in matplotlib

In this tutorial I will show you how to create Tables using Python and Matplotlib. In this tutorial I will show you how to create Radar charts using Python and Matplotlib. For more matplotlib charts, check out the gallery:

1 dataset 100 matplotlib visualizations
Python dataviz gallery, matplotlib viz gallery

Important notes:

1. This are my personal notes, so apologies if some explanations and notations are missing.

Tables in matplotlib

Click on the links to go to the specific tutorials:

  1. Colored table
  2. 96 of 100: Matrix chart
  3. 100 of 100: Tabular chart

Create a colored table in matplotlib

Import the libraries

from matplotlib import pyplot as plt
import numpy as np

Define the columns and cells



columns =["Number of \n Characters", "Numbers only", "Lowercase \nLetters","Upper and \nLowercase Letters", "Numbers, \nUpper and \nLowercase Letters", "Numbers, \nUpper and \nLowercase Letters, \nSymbols"]
columns

noc = list(range(1,16))
no = ["Instantly", "Instantly", "Instantly", "Instantly", "Instantly", "Instantly", "Instantly", "Instantly", "2 secs", "19 secs", "3 mins", "32 mins", "5 hours", "2 days", "3 weeks"]
lc =["Instantly", "Instantly", "Instantly", "Instantly", "Instantly", "10 secs", "4 mins", "2 hours", "2 days", "2 months", "4 years", "100 years", "3k years", "69k years", "2m years"]
ual = ["Instantly", "Instantly", "Instantly", "2 secs", "2 mins", "1 hour", "3 days", "5 months", "24 years", "1 k years", "64k years", "3m years", "173m years", "9bn years", "467bn years"]
nual =["Instantly", "Instantly", "Instantly", "7 secs", "7 mins", "7 hours", "3 weeks", "3 years", "200 years", "12k years", "750k years", "46m years", "3bn years", "179bn years", "11 tn years"]
nuals =["Instantly", "Instantly", "Instantly", "31 secs", "39 mins", "2 days", "5 months", "34 years", "3k years", "202k years", "16m years", "1 bn years", "92bn years", "7tn years", "438tn years"]

cells = np.column_stack((noc, no,lc,ual,nual,nuals))

#Lets create now the correct colored list
c_noc = np.repeat(['#858893'], [15])
c_no = np.repeat(['#7E539D','#BA2C36', ], [8, 7])
c_lc = np.repeat(['#7E539D','#BA2C36', '#D8862F', '#E8B532','#66AA56'], [5, 4, 1, 1, 4])
c_ual = np.repeat(['#7E539D','#BA2C36', '#D8862F', '#E8B532','#66AA56' ], [3, 4, 1, 1, 6])
c_nual = np.repeat(['#7E539D','#BA2C36', '#E8B532','#66AA56' ], [3, 4, 1, 7])
c_nuals = np.repeat(['#7E539D','#BA2C36', '#D8862F', '#E8B532','#66AA56' ], [3, 3, 1, 1, 7])

cell_col = np.column_stack((c_noc, c_no, c_lc, c_ual, c_nual, c_nuals))
cell_col 

Define the annotations

font= "Raleway"
def annotations(ax):
    # Set source text
    # Add in title and subtitle
    ax.text(x=0.1, y=1.1, 
            s="Time it takes to brute force a password",                                          #title of the plot
            fontname= font,
            transform=fig.transFigure,                                                      #set the coordinate system to 0-1 to pass the postition
            color= "#000000",
            ha='left', fontsize=14, weight='bold', alpha=.8)
    ax.text(x=0.1, y=1.06, 
            s="Time it would take a computer to crack your password with the following paramets.",                                    #Subtitle
            fontname= font,
            transform=fig.transFigure, 
            color= "#181818",
            ha='left', fontsize=8, alpha=.8)
    ax.text(x=0.1, y=-0.1, 
            s="""Data: Statista\nCoded by: Ruth Pozuelo Martinez - www.curbal.com\nDesign: Statista""", #footnote
             fontname= font,
            transform=fig.transFigure, 
            color="#858585", 
            ha='left', fontsize=6, alpha=.7)

Plot the chart

fig, ax = plt.subplots(1, 
                       figsize=(9,5), dpi=130,     #specify the size of the plot
                       facecolor="white")  #set a background color

tab = ax.table(cellText=cells,       #values on the rows
                    colLabels=columns,     #header values
                    colColours = ["#303546"] * 10,           #column header color
                    cellColours = cell_col ,
                    cellLoc="center",               #align the cell text to the left
                    rowLoc= "right",               #Column headers alignment
                    loc='center')                  #location of the figure

#Iterate through all the cells in the table to:
cellDict = tab.get_celld()
for i in range(0,len(columns)):
    cellDict[(0,i)].set_height(.08)                   #header height
    cellDict[(0,i)].get_text().set_color('white')     #header font color
    cellDict[(0,i)].set_edgecolor("#303546")
    for j in range(1,len(cells)+1):
        cellDict[(j,i)].set_height(.05)               #row height
        cellDict[(j,i)].get_text().set_color('white')   #cell font color
        cellDict[(j,i)].set_edgecolor("#303546")



# iterate through cells of a table
table_cells = tab.properties().get('child_artists')
print(table_cells)

ax.set_axis_off() #get rid of the axxes figure
tab.scale(1, 1.6) #add padding

#turn off the auto set text so you can set the font size
tab.auto_set_font_size(False)
tab.set_fontsize(8)

annotations(ax)
Was this helpful?

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents