【问题标题】:RGB cube of an image图像的 RGB 立方体
【发布时间】:2013-05-14 19:58:38
【问题描述】:

我正在尝试在 python 中使用 matplotlib 创建图像的 RGB 立方体。我有一个 python 列表,其中包含[(2,152,255),(0,0,0)...] 格式的所有像素的 RGB。我用散点函数绘制所有点,但我不知道如何用各自的 RGB 颜色绘制每个 RGB 点。

我尝试过类似ax.scatter(paleta[0],paleta[1],paleta[2],c = RGBlist) 的操作,但该函数需要一个 RGBa 值...

我期待这样的事情:

代码:

paleta=zip(*RGBlist) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(paleta[0],paleta[1],paleta[2]) ax.grid(False) ax.set_title('grid on') plt.savefig('Images\\RGBcube.png')

【问题讨论】:

    标签: python matplotlib plot rgb cube


    【解决方案1】:

    尝试将 RGB 值缩放到 [0,1] 范围:

    ax.scatter(paleta[0],paleta[1],paleta[2], c=[(r[0] / 255., r[1] / 255., r[2] / 255.) for r in RGBlist])

    这适用于以下示例:

    import random
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import axes3d, Axes3D
    
    RGBlist = [(random.randint(0,255), random.randint(0,255), random.randint(0,255)) for i in range(100)]
    paleta=zip(*RGBlist)
    fig = plt.figure()
    ax = Axes3D(fig)
    ax.scatter(paleta[0],paleta[1],paleta[2], c=[(r[0] / 255., r[1] / 255., r[2] / 255.) for r in RGBlist])
    ax.grid(False)
    ax.set_title('grid on')
    plt.savefig('blah.png')
    

    给出输出:

    【讨论】:

    • 这就是我的意思!谢谢!
    【解决方案2】:

    scatter函数中设置color属性:

    color - matplotlib 颜色参数或 rgba 元组序列

    【讨论】:

      猜你喜欢
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多