Use matplotlib to plot 2 table objects as subgraphs
I
have 2 matplotlib table objects in the list and I’m trying to plot each table as a subgraph. So far, all the answers on Stack Exchange seem to be related to plotting subgraphs or plotting individual tables.
The following code only generates the second table I want to plot, 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 the suggestions of various tutorials and made the following suggestions:
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()
However, when this code calls the add_subplot method, the following error will be generated.
TypeError: int() argument must be a string, a bytes-like object or a number, not ‘Table’.
How do I plot each table as a subgraph?
Solution
You save table instances in a list and then try to plot them using plt.plot
, which requires a list of numbers.
One possibility is to create a subgraph and then plot the table to a specific axis using the object-oriented API:
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()
Give: