Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting 2 table objects as subplots using matplotlib

I have 2 matplotlib table objects in a list, and I'm trying to plot each table as a subplot. So far all the answers on Stack Exchange appear to be concerned with either subplotting figures, or plotting single tables.

The following code produces only the second table I want to plot, but not the first.

import matplotlib.pyplot as plt
import numpy as np

list_of_tables = []
a = np.empty((16,16))

for i in range(0, 2):
    a.fill(i)
    the_table = plt.table(
        cellText=a,
        loc='center',
    )
    list_of_tables.append(the_table)

plt.show()

So I followed advice from various tutorials and came up with the following:

import matplotlib.pyplot as plt
import numpy as np

list_of_tables = []
a = np.empty((16,16))

for i in range(0, 2):
    a.fill(i)
    the_table = plt.table(
        cellText=a,
        loc='center',
    )
    list_of_tables.append(the_table)

fig = plt.figure()
ax1 = fig.add_subplot(list_of_tables[0])
ax2 = fig.add_subplot(list_of_tables[1])
ax1.plot(list(of_tables[0])
ax2.plot(list_of_tables[1])

plt.show()

But when this code calls the add_subplot method, the following error is produced.

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Table'.

How can I plot each table as a subplot?

like image 246
user156060 Avatar asked Oct 16 '25 20:10

user156060


1 Answers

You are saving the tables instances in a list and then trying to plot them using plt.plot which expects a list of numbers.

A possibility would be to create the subplots, then use the object-oriented API in order to plot the table to a specific axes:

import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(1, 2)

a = np.empty((16, 16))

for i in range(0, 2):
    a.fill(i)
    the_table = axes[i].table(
        cellText=a,
        loc='center',
    )
    axes[i].axis("off")  

plt.show()

Which gives:

enter image description here

like image 64
DavidG Avatar answered Oct 19 '25 11:10

DavidG