使用matplotlib将2个表对象绘制为子图

时间:2018-03-05 12:38:10

标签: python matplotlib

我在列表中有2个matplotlib表对象,我试图将每个表绘制为子图。到目前为止,Stack Exchange上的所有答案似乎都与子图绘制或绘制单个表格有关。

以下代码仅生成我想要绘制的第二个表,而不是第一个。

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()

所以我遵循各种教程的建议并提出以下建议:

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()

但是当此代码调用add_subplot方法时,会产生以下错误。

  

TypeError:int()参数必须是字符串,类字节对象或数字,而不是'表'。

如何将每个表绘制为子图?

1 个答案:

答案 0 :(得分:1)

您正在将表格实例保存在列表中,然后尝试使用需要数字列表的plt.plot来绘制它们。

可能是创建子图,然后使用面向对象的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()

给出了:

enter image description here