2013-12-24 72 views
1

我有以下代碼用於在matplotlib中製作表格圖。將顏色條添加到表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()但這給了我一個畫布錯誤。

回答

2

您可以通過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() 

enter image description here