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

举报
千江有水千江月 发表于 2021/12/20 12:18:17 2021/12/20
【摘要】 环境信息ModelArtsCodeLabConda-python3matplotlib 2.1.0  代码示例import numpy as npimport matplotlib.pyplot as plt%matplotlib inline# [low,high)# 黑色是0# 白色是255# 为了Mnist数据集,而创建的28*28像素矩阵image = np.random.rand...

环境信息

  • ModelArts
    • CodeLab
      • Conda-python3
        • matplotlib 2.1.0

代码示例

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# [low,high)
# 黑色是0
# 白色是255
# 为了Mnist数据集,而创建的28*28像素矩阵
image = np.random.randint(low=0,high=256,size=(28,28))
image

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

......

备注

  1. 欢迎各位同学一起来交流学习心得^_^
  2. 在线课程、沙箱实验、认证、大赛、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。