Ask Question
21 April, 16:56

Write a code that makes a 2D square array with 20*20 elements. Fill the array with random numbers initially. Then, "draw" a square in the array by adding a value of 4 to those array elements, and plot it with imshow. The square should be centered and have side lengths of 10.

+2
Answers (1)
  1. 21 April, 19:02
    0
    Answer explained below. Python language used

    Explanation:

    The size of the square is of size 10. So assuming zero indexing,

    vertical side1 (column Index = 5) : row index from 5 to 14

    vertical side1 (column Index = 14) : row index from 5 to 14

    horizontal side1 (row Index = 5) : column index from 5 to 14

    horizontal side2 (row Index = 14) : column index from 5 to 14

    Note:

    while indexing, data_new[a:b, c] denotes,

    row index: a to b-1

    column index: c

    Similarly data_new[a, b:c] denotes,

    row index: a

    column index: b to c-1

    Code:

    import matplotlib. pyplot as plt

    import numpy as np

    size_1 = 20

    size_2 = 20

    data_new = np. random. random ((size_1, size_2))

    data_new[5:15,5] = 4

    data_new[5:15,14] = 4

    data_new[5,5:15] = 4

    data_new[14,5:15] = 4

    fig = plt. figure (figsize = (10,10))

    plt. imshow (data_new)

    plt. colorbar ()

    plt. show ()
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a code that makes a 2D square array with 20*20 elements. Fill the array with random numbers initially. Then, "draw" a square in the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers