Plot a black-and-white binary map in matplotlib

前端 未结 2 1316
谎友^
谎友^ 2020-12-09 14:53

I\'m using python to simulate some automation models, and with the help of matplotlib I\'m producing plots like the one shown below.

2条回答
  •  执笔经年
    2020-12-09 15:39

    You can change the color map you are using via the cmap keyword. The color map 'Greys' provides the effect you want. You can find a list of available maps on the scipy website.

    import matplotlib.pyplot as plt
    import numpy as np
    
    np.random.seed(101)
    g = np.floor(np.random.random((100, 100)) + .5)
    
    plt.subplot(211)
    plt.imshow(g)
    plt.subplot(212)
    plt.imshow(g, cmap='Greys',  interpolation='nearest')
    plt.savefig('blkwht.png')
    
    plt.show()
    

    which results in:

    enter image description here

提交回复
热议问题