matplotlib基础 plt 显示随机生成的灰度值(0-255)矩阵

  •        Python : 3.8.11
  •    matplotlib : 3.3.4
  •          OS : Ubuntu Kylin 20.04
  •       Conda : 4.10.1
  •    jupyter lab : 3.1.4

代码示例

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
复制代码
# [low,high)
# 黑色是0
# 白色是255
# 补充:MNIST数据集正好与之相反
image = np.random.randint(low=0,high=256,size=(28,28))
image

array([[195, 182, 114,  92, 182, 180, 169,  54, 238,  68, 166,   1, 162,
        159, 103, 149, 122, 153,  51, 244,  66,  26, 194,   3,  90,  57,
        186, 174],
       [212, 109, 154,  41, 149,  27, 141, 114, 191, 166, 127,  66,  52,
        120,   8, 156, 160, 151, 145, 105,  37, 120, 241, 233,  28,  48,
        133,  99],
       [ 58, 167, 169, 213,  77, 129,  30,  67,  46, 175, 185, 146,  96,
         73,  92, 126, 243, 154,  83, 174,  59, 222,  32, 236, 173,  79,
          6, 237],
       [185,  42,  64, 250, 146, 140,  23,   1, 163, 196,  76,  43, 129,
         84, 119,  94,  69, 134, 147,  87, 241, 244,  33,   6,   9, 135,
        195, 173],
       [215,  51, 162, 218,  79,  51, 197, 133, 174, 151,  44, 118, 134,
         21, 160, 147, 147, 167, 141,   5, 128,  90, 188,  53, 164,   3,
        152, 159],
       [158,  26, 120,  12, 167,  68,  72, 156, 136,  97,  39, 141,  11,
        179,  38, 140,   9, 179,   6,  20, 156, 177, 107, 131,  99,  36,
        216, 216],
       [231,  73,  18, 209, 155,  96,  81, 240, 210, 153,  41, 162, 166,
        147, 230,  53,  67, 153,  12, 100, 151, 192, 111, 232,  52, 250,
         88,  70],
       [167, 201,  39,  37, 131, 216,  44, 215, 119,  71, 118, 163, 233,
         38, 177,  50, 237,  50, 200, 169,  32, 137,  96, 165,  68, 226,
        253, 200],
       [115, 135,  36, 200,  90,  43,  96, 253, 234, 199, 146, 177,  85,
        162,  37,  95,  45, 148, 188, 135, 151, 197,  24, 198, 171, 143,
        107, 184],
       [  1, 213,  79, 125, 173,  94,  50,  79, 105,  65,  17,  29,  91,
        222,  36,  40, 108,  88, 161, 118, 226, 174,  44,  53, 111, 238,
        203, 179],
       [230, 140, 237,  73,  48,  71, 130,   4, 220, 153, 187, 198, 250,
        111,  18, 171,  57, 128,  46, 100,  64,  98,  47,  60, 197,   9,
        104, 101],
       [ 26,  18, 130, 210,  49,  38, 150,  21, 248,  65,  16, 152, 173,
         76, 131,   3,  36,  85, 194, 214,  92, 168, 105, 194,  61,  88,
        147, 153],
       [ 87,  24, 215, 115, 244, 236,  33,  12, 218, 189, 157, 158,  42,
        164,  22, 200,  19,  73,  91,  70,  80, 114, 155,   0, 121, 249,
        106,  51],
       [151, 162, 123,  68, 107, 248, 237, 187,  47, 168, 193, 175,  84,
        164, 168, 142, 234, 207, 201, 222, 133,  60, 100, 222, 113,  37,
        253,   5],
       [  9,  13,  96, 226, 240,  99,   3, 132,  88, 138, 174, 255, 209,
         38,  99,  20, 128, 228, 148, 226,  86, 195,  58, 184, 142,  30,
        115, 216],
       [ 15, 163,   1, 211,  13, 211, 209,  24, 193, 218, 245, 132, 235,
        247,   2, 227,  25,   7, 212, 139,  51,  99, 249,  62,  26, 247,
        218, 182],
       [198, 122,   5, 117,  50, 137, 148,  48,  82, 235, 119, 252,  43,
        144, 138, 216, 248, 148, 124,  86, 144,  35,  27,  14, 144,   1,
        222, 106],
       [255, 144, 105, 122, 215, 154, 182, 209, 203,  49,  55,  43, 153,
        115, 188,  73, 207,   4,  82,  46,  91,   9, 215,  15, 238, 120,
         23, 158],
       [ 81, 236, 124,  56, 221,  19, 169, 230,  82, 248, 109, 199,  55,
         34,   2, 158, 173,  34,  25,  95, 208, 201,  21, 235,  70,  48,
        238,   2],
       [217, 133,  10,  57,  19, 148, 242, 151, 157,  84, 196, 111, 251,
        155, 158,  68,  27, 131,  33, 254,  70,  34,  50, 171, 235, 137,
         15,  79],
       [102, 230, 120,  23, 211, 160,  37,  51, 221, 252,  41, 207, 112,
        106, 135, 193, 133, 165,  43, 253,  21, 149, 145, 218,  80, 126,
         60, 224],
       [230, 123,  24,  90,  86,  37,  85, 155,  57, 216,  15,  56, 146,
         89,  17, 197, 161,  52, 176, 114, 198, 216, 129, 143, 174,   5,
         49, 184],
       [ 28, 205, 153, 105, 237,  52, 151,  33,  74,  22, 215,  83,  51,
        106,  81,  18,   6, 143,   1, 186, 121, 100, 101, 240, 106, 205,
         25,  54],
       [ 26,  76, 136,  86,  45, 159, 157, 108, 189, 112, 123, 209, 189,
        247, 221, 214, 124, 117, 152, 234, 204, 166,  42, 137, 154,  25,
        194, 144],
       [123, 123, 182, 111,  41, 196, 119,  91,  34,  63, 123,  98, 143,
        231, 234, 248,  29, 107,  42,  63,  26, 196, 232, 166,  23, 244,
        216,  45],
       [108, 189, 247,  78, 166, 188, 239, 194,  63, 239, 147, 211,  91,
        159,  29,  55,  50, 254,  37,  51,  18, 128, 156,   3, 125,  90,
        145,  75],
       [177, 217, 104, 209, 169, 231,  92,  16,  10, 248,  22,  34,  13,
        127, 115, 152, 113, 241, 117, 224,  68,  74,  42, 126,  73, 161,
        203,   6],
       [ 50, 115, 172,   9, 112, 161, 156,  30, 114, 229,   4, 170, 219,
        198, 125,  25, 112, 194,  87, 100,  67, 212,  60,  85, 165, 156,
        140,  44]])
复制代码
plt.imshow(image)
复制代码

plt-0-255-1.png

# cmap="Greys" 灰度
plt.imshow(image,cmap="Greys")
复制代码

plt-0-255-2.png

源码学习

help(np.random.randint)

elp on built-in function randint:

randint(...) method of numpy.random.mtrand.RandomState instance
    randint(low, high=None, size=None, dtype=int)
    
    Return random integers from `low` (inclusive) to `high` (exclusive).
    
    Return random integers from the "discrete uniform" distribution of
    the specified dtype in the "half-open" interval [`low`, `high`). If
    `high` is None (the default), then results are from [0, `low`).
    
    .. note::
        New code should use the ``integers`` method of a ``default_rng()``
        instance instead; please see the :ref:`random-quick-start`.

......
复制代码

学习推荐


Python具有开源、跨平台、解释型、交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。

猜你喜欢

转载自juejin.im/post/7018083378696028197
plt