Creating Tables in Matplotlib

import matplotlib.pyplot as plt
import numpy as np

# Sample data
columns = ('A', 'B', 'C', 'D')
rows = ['Row1', 'Row2', 'Row3']
data = [[123, 211, 232, 121],
        [231, 321, 323, 131],
        [111, 223, 338, 201]]

fig, ax = plt.subplots()

# Hide axes
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.set_frame_on(False)

# Create the table
table = plt.table(cellText=data, colLabels=columns, rowLabels=rows, loc='center')

# Auto scale the table layout
table.scale(1, 1.5)
plt.title('Basic Table in Matplotlib')

plt.show()
import matplotlib.pyplot as plt

# Sample data
columns = ('A', 'B', 'C', 'D')
rows = ['Row1', 'Row2', 'Row3']
data = [[123, 211, 232, 121],
        [231, 321, 323, 131],
        [111, 223, 338, 201]]

fig, ax = plt.subplots()

# Hide axes
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.set_frame_on(False)

# Create the table
table = plt.table(cellText=data, colLabels=columns, rowLabels=rows, loc='center', cellLoc='center')

# Customizations
table.auto_set_font_size(False)
table.set_fontsize(12)
table.scale(1.2, 1.2)

# Coloring cells
cell_dict = table.get_celld()
for i in range(len(data) + 1):
    for j in range(len(columns)):
        if i == 0:  # Header cell
            cell_dict[(i, j)].set_facecolor('#40466e')
            cell_dict[(i, j)].set_text_props(color='w')
        else:
            cell_dict[(i, j)].set_facecolor('#f2f4f7')

plt.title('Custom Table Appearance')

plt.show()
import matplotlib.pyplot as plt
import numpy as np

# Sample data
columns = ('A', 'B', 'C', 'D')
rows = ['Row1', 'Row2', 'Row3']
data = [[123, 211, 232, 121],
        [231, 321, 323, 131],
        [111, 223, 338, 201]]

fig, ax = plt.subplots()

# Hide axes
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.set_frame_on(False)

# Create the table
table = plt.table(cellText=data, colLabels=columns, rowLabels=rows, loc='center')

# Customizations
for (i, j), cell in table.get_celld().items():
    cell.set_fontsize(12)
    if i == 0 or j == -1:
        cell.set_text_props(weight='bold', color='white')
        cell.set_facecolor('#40466e')
    else:
        if data[i-1][j] > 200:
            cell.set_facecolor('#ff9999')
        else:
            cell.set_facecolor('#99ff99')

table.scale(1.2, 1.2)

plt.title('Advanced Table with Highlighted Cells')

plt.show()
import numpy as np 
import matplotlib.pyplot as plt 
  
# Energy consumption data for 4 quarters: Electricity and Gas consumption (in kWh)
consumption_data = [
    [250, 300, 320, 280],  # Electricity consumption
    [210, 110, 80, 180]   # Gas consumption
]

quarters = ['Q1', 'Q2', 'Q3', 'Q4']
rows = ['Electricity', 'Gas']
columns = quarters
colors = plt.get_cmap('tab10').colors

cell_text = []
for row in range(len(consumption_data)):
    plt.plot(columns, consumption_data[row], color=colors[row], label=rows[row])
    cell_text.append([x for x in consumption_data[row]])

table = plt.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom')

plt.ylabel("Energy Consumption (kWh)")
plt.xticks([])
plt.title('Monthly Energy Consumption Over 5 Years')
plt.show()