我有以下代码用于在matplotlib中制作表图。
fig = plt.figure(figsize=(15,8))
ax = fig.add_subplot(111, frameon=True, xticks = [], yticks = [])
tb = plt.table(cellText = cells[:30], rowLabels = range(30), colLabels = range(30), loc = 'center',cellColours = plt.cm.hot(normal(cells[:30])))
ax.add_table(tb)
plt.show()
plt是pyplot对象
我想为我使用的颜色图添加一个颜色条。
我尝试这样做,fig.colorbar()
但这给了我一个画布错误。
您可以通过创建虚拟图像imshow
并将其隐藏:
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(8,4))
ax = fig.add_subplot(111, frameon=True, xticks = [], yticks = [])
cells = np.random.randint(0, 100, (10, 10))
img = plt.imshow(cells, cmap="hot")
plt.colorbar()
img.set_visible(False)
tb = plt.table(cellText = cells,
rowLabels = range(10),
colLabels = range(10),
loc = 'center',
cellColours = img.to_rgba(cells))
ax.add_table(tb)
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句