【问题标题】:Clipping input data to the valid range for imshow with RGB data使用 RGB 数据将输入数据剪切到 imshow 的有效范围
【发布时间】:2021-02-28 02:39:27
【问题描述】:

完整错误消息:使用 RGB 数据将输入数据剪切到 imshow 的有效范围(浮点数为 [0..1] 或整数为 [0..255])

我正在尝试从文本文档中的一系列 RGB 值中可视化一个色带。但是,当我使用下面的函数时,会出现上述错误,并且色带看起来像这样。

我已经在这里尝试了许多解决方案,但我仍然无法摆脱“剪辑输入”消息。

这是绘制色带的代码:

def plot_colors(col_list, col_order, ratio = 10): 
    assert len(col_list) == len(col_order)
    img = np.zeros((ratio, len(col_list), 3))
    for i in range(0, len(col_list)):
        img[:, i, :] = col_list[col_order[i]]
    fig, axes = plt.subplots(1, figsize=(10,6)) # figsize=(width,height) handles window dimensions
    axes.imshow(img, interpolation='nearest')
    axes.axis('off')
    plt.show()

这里是调用代码的地方:

clustered_colours = data_scaled.sort_values(by=['Cluster']) #sorts colours by cluster

kmeans_cluster = clustered_colours[['Red','Green','Blue']].to_numpy() #Converts DataFrame to numpy array

order = list(range(ncolors)) #order from 0 to ncolours 

plot_colors(kmeans_cluster, order)

【问题讨论】:

    标签: python numpy matplotlib jupyter-notebook


    【解决方案1】:

    Matplotlib 预计 imshow 的数据如果是浮点数则在 [0,1] 范围内,如果是整数则在 [0,255] 范围内。

    首先,检查type(img),如果是浮点类型,则必须转换[0,1]范围内的值,如果是整数,则为[0,255]

    img = img/np.amax(img) # if float
    img = np.array(img/np.amax(img)*255, np.int32) # if int
    

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2020-09-29
      • 2022-01-25
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 2016-11-19
      • 2016-03-19
      • 2019-06-18
      相关资源
      最近更新 更多