如何将颜色条添加到 subplot2grid
How to add a colorbar to subplot2grid
这应该很简单,但由于某种原因我无法让它工作:
def plot_image(images, heatmaps):
plt.figure(0)
for i, (image, map) in enumerate(zip(images, heatmaps)):
a = plt.subplot2grid((2,4), (0,i))
a.imshow(image)
a = plt.subplot2grid((2,4), (1,i))
a.imshow(map)
plt.colorbar(a, fraction=0.046, pad=0.04)
plt.show()
colorbar 行中的值取自 here,但我得到:
AttributeError: 'AxesSubplot' object has no attribute 'autoscale_None'
我正在绘制 2 x 4 图像网格,我想在每个图像的右侧显示垂直颜色条,或者可能仅在网格中最右边的图像旁边显示。
plt.colorbar
需要一个图像作为它的第一个参数(或者通常是一个 ScalarMappable),而不是一个轴。
plt.colorbar(im, ax=ax, ...)
因此您的示例应为:
import numpy as np
import matplotlib.pyplot as plt
def plot_image(images, heatmaps):
fig = plt.figure(0)
for i, (image, map) in enumerate(zip(images, heatmaps)):
ax = plt.subplot2grid((2,4), (0,i))
im = ax.imshow(image)
ax2 = plt.subplot2grid((2,4), (1,i))
im2 = ax2.imshow(map)
fig.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
fig.colorbar(im2, ax=ax2, fraction=0.046, pad=0.04)
plt.show()
a = [np.random.rand(5,5) for i in range(4)]
b = [np.random.rand(5,5) for i in range(4)]
plot_image(a,b)
这应该很简单,但由于某种原因我无法让它工作:
def plot_image(images, heatmaps):
plt.figure(0)
for i, (image, map) in enumerate(zip(images, heatmaps)):
a = plt.subplot2grid((2,4), (0,i))
a.imshow(image)
a = plt.subplot2grid((2,4), (1,i))
a.imshow(map)
plt.colorbar(a, fraction=0.046, pad=0.04)
plt.show()
colorbar 行中的值取自 here,但我得到:
AttributeError: 'AxesSubplot' object has no attribute 'autoscale_None'
我正在绘制 2 x 4 图像网格,我想在每个图像的右侧显示垂直颜色条,或者可能仅在网格中最右边的图像旁边显示。
plt.colorbar
需要一个图像作为它的第一个参数(或者通常是一个 ScalarMappable),而不是一个轴。
plt.colorbar(im, ax=ax, ...)
因此您的示例应为:
import numpy as np
import matplotlib.pyplot as plt
def plot_image(images, heatmaps):
fig = plt.figure(0)
for i, (image, map) in enumerate(zip(images, heatmaps)):
ax = plt.subplot2grid((2,4), (0,i))
im = ax.imshow(image)
ax2 = plt.subplot2grid((2,4), (1,i))
im2 = ax2.imshow(map)
fig.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
fig.colorbar(im2, ax=ax2, fraction=0.046, pad=0.04)
plt.show()
a = [np.random.rand(5,5) for i in range(4)]
b = [np.random.rand(5,5) for i in range(4)]
plot_image(a,b)