【问题标题】:Adding color bar to a table matplotlib将颜色条添加到表 matplotlib
【发布时间】:2014-01-12 07:14:13
【问题描述】:

我有以下代码用于在 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(),但这给了我一个画布错误。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 2020-11-13
      • 2022-12-12
      • 2016-09-16
      相关资源
      最近更新 更多