gpt4 book ai didi

python - 将颜色条添加到表 matplotlib

转载 作者:行者123 更新时间:2023-12-01 05:28:56 26 4
gpt4 key购买 nike

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

最佳答案

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

关于python - 将颜色条添加到表 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20754205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com