gpt4 book ai didi

python - 使用 matplotlib 将色相渐变作为 y 轴上的颜色条

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:11 25 4
gpt4 key购买 nike

我有以下色相/饱和度二维直方图。有两件事我想改变但不知道如何改变。 Hue/saturation 2D Histogram

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 (下面发布的渐变)设置为颜色条的数据并显示它并将颜色条移动到色相轴刻度的位置。

Hue Gradient

当前代码:

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:cmapedge 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()

这是我能做的最好的事情......你必须找到一种方法来在左侧“颜色栏”中绘制你想要的颜色。

关于python - 使用 matplotlib 将色相渐变作为 y 轴上的颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37834881/

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