使用 matplotlib 将色调渐变作为 y 轴上的颜色条
Hue gradient as colorbar on y axis with matplotlib
我有以下 Hue/Saturation 二维直方图。有两件事我想改变但不知道如何改变。
1) 是否可以用显示色调梯度的颜色条替换 y 轴值 (0-20)?以下代码构建了一个 numpy 数组梯度。
hue_gradient = np.linspace(0, 1)
hsv = np.ones(shape=(1, len(hue_gradient), 3), dtype=float)
hsv[:, :, 0] = hue_gradient
all_hues = hsv_to_rgb(hsv)
所以问题是如何将这个数组设置为颜色条并将其定位在图像左侧旁边。
2) 我想缩放图像右侧的颜色条,使其在顶部和底部不超过它?
我希望有人能帮助我。
编辑:
为了阐明我想在 y 轴(色调)而不是 0 到 20 的值上看到什么。
我有以下梯度。我用上面的代码生成它。我希望将此渐变可视化为颜色条,而不是色调轴的值 0 到 20。
基本上:我如何将 all_hues
(下面发布的渐变)设置为颜色条的数据并显示它并将颜色条移动到色相轴刻度的位置。
当前代码:
fig_synth = plt.figure("synth")
plt.imshow(synth_avrgHistogram, interpolation='nearest', vmin=0, vmax=vmax)
plt.colorbar()
plt.xlabel("Saturation")
plt.ylabel("Hue")
好吧,我不知道该怎么做...我能想到的最接近的事情是使用这些 matplotlib 示例中的 AxesGrid
:cmap and edge cbar.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
import numpy as np
def demo_right_cbar(fig):
"""
A grid of 2x1 images. Left will be the colorbar, right the image.
"""
grid = AxesGrid(fig, 121, # similar to subplot(122)
nrows_ncols=(1,2),
axes_pad=0.05,
cbar_location="right",
cbar_mode="edge",
cbar_size="7%",
cbar_pad="2%",
)
extent = (0,200,0,200)
Z = np.random.randint(0,200,(extent[1],extent[3]))
gradient = np.linspace(0, 20, 100)
gradient = np.vstack((gradient, gradient)).T
grid[0].imshow(gradient, aspect=100./7., extent=extent)
grid[1].set_ylabel("Hue")
grid[0].set_xticks([])
grid[0].set_ylabel("Hue")
im = grid[1].imshow(Z, extent=extent, interpolation="nearest", cmap=plt.get_cmap("summer"))
grid[1].set_xlabel("Saturation")
print(dir(grid[0]))
cax = grid.cbar_axes[0]
cax.colorbar(im)
cax.toggle_label(True)
cax.axis[cax.orientation].set_label('Foo')
fig = plt.figure()
demo_right_cbar(fig)
plt.show()
这是我能做的最好的了...你必须想办法在左边绘制你想要的颜色 "colorbar"。
我有以下 Hue/Saturation 二维直方图。有两件事我想改变但不知道如何改变。
1) 是否可以用显示色调梯度的颜色条替换 y 轴值 (0-20)?以下代码构建了一个 numpy 数组梯度。
hue_gradient = np.linspace(0, 1)
hsv = np.ones(shape=(1, len(hue_gradient), 3), dtype=float)
hsv[:, :, 0] = hue_gradient
all_hues = hsv_to_rgb(hsv)
所以问题是如何将这个数组设置为颜色条并将其定位在图像左侧旁边。
2) 我想缩放图像右侧的颜色条,使其在顶部和底部不超过它?
我希望有人能帮助我。
编辑:
为了阐明我想在 y 轴(色调)而不是 0 到 20 的值上看到什么。
我有以下梯度。我用上面的代码生成它。我希望将此渐变可视化为颜色条,而不是色调轴的值 0 到 20。
基本上:我如何将 all_hues
(下面发布的渐变)设置为颜色条的数据并显示它并将颜色条移动到色相轴刻度的位置。
当前代码:
fig_synth = plt.figure("synth")
plt.imshow(synth_avrgHistogram, interpolation='nearest', vmin=0, vmax=vmax)
plt.colorbar()
plt.xlabel("Saturation")
plt.ylabel("Hue")
好吧,我不知道该怎么做...我能想到的最接近的事情是使用这些 matplotlib 示例中的 AxesGrid
:cmap and edge cbar.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
import numpy as np
def demo_right_cbar(fig):
"""
A grid of 2x1 images. Left will be the colorbar, right the image.
"""
grid = AxesGrid(fig, 121, # similar to subplot(122)
nrows_ncols=(1,2),
axes_pad=0.05,
cbar_location="right",
cbar_mode="edge",
cbar_size="7%",
cbar_pad="2%",
)
extent = (0,200,0,200)
Z = np.random.randint(0,200,(extent[1],extent[3]))
gradient = np.linspace(0, 20, 100)
gradient = np.vstack((gradient, gradient)).T
grid[0].imshow(gradient, aspect=100./7., extent=extent)
grid[1].set_ylabel("Hue")
grid[0].set_xticks([])
grid[0].set_ylabel("Hue")
im = grid[1].imshow(Z, extent=extent, interpolation="nearest", cmap=plt.get_cmap("summer"))
grid[1].set_xlabel("Saturation")
print(dir(grid[0]))
cax = grid.cbar_axes[0]
cax.colorbar(im)
cax.toggle_label(True)
cax.axis[cax.orientation].set_label('Foo')
fig = plt.figure()
demo_right_cbar(fig)
plt.show()
这是我能做的最好的了...你必须想办法在左边绘制你想要的颜色 "colorbar"。