matplotlib 3D scatterplot with marker color corresponding to RGB values

前端 未结 1 791
渐次进展
渐次进展 2020-12-18 00:29

I have loaded a picture into a numpy array using mahotas.

import mahotas
img = mahotas.imread(\'test.jpg\')

Each pixel in img

相关标签:
1条回答
  • 2020-12-18 00:58

    Yes, you can do this, but it needs to be done through a separate mechanism than the c argument. In a nutshell, use facecolors=rgb_array.


    First off, let me explain what's going on. The Collection that scatter returns has two "systems" (for lack of a better term) for setting colors.

    If you use the c argument, you're setting the colors through the ScalarMappable "system". This specifies that the colors should be controlled by applying a colormap to a single variable. (This is the set_array method of anything that inherits from ScalarMappable.)

    In addition to the ScalarMappable system, the colors of a collection can be set independently. In that case, you'd use the facecolors kwarg.


    As a quick example, these points will have randomly specified rgb colors:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x, y = np.random.random((2, 10))
    rgb = np.random.random((10, 3))
    
    fig, ax = plt.subplots()
    ax.scatter(x, y, s=200, facecolors=rgb)
    plt.show()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题