Furthermore, the imshow() technique is well-known for displaying graphs in Matlab software. A two-dimensional array can display grayscale graphics, whereas a three-dimensional array can display colorful visuals. We will depict either colored or dark graphics with Matplotlib. Let’s look at several methods of using a NumPy array to draw the graphs and represent them with the help of the imshow() function.
Use the imshow() Method:
By using the imshow() method, we will be able to show graphics. In Matplotlib, we can adjust the dimension of imshow without extending the graphic.
import numpy as np
plt.rcParams["figure.figsize"] = [10.50, 6.0]
plt.rcParams["figure.autolayout"] = True
d = np.random.rand(8, 8)
plt.imshow(d, origin='upper', extent=[-5, 5, -2, 2], aspect=4)
Before starting the code, we import two libraries. The matplotlib.pyplot library is responsible for the graphical functions and plotting methods. On the other hand, the NumPy library is utilized to handle different numeric values.
Similarly, we call the autolayout() function to set the spacing among the subplots. Here, we assign a “true” value to this function. We created a new variable, so we stored the random data sets with 8×8 dimensions in this variable by using the rand() method of the NumPy library.
Further, we utilize a two-dimensional normal grid to show the data as a visual. We employ the imshow() function to draw the graph without extending the graph. Here, we can state the origin of the graph.
In addition, to convert the illustration of buffering pixel dimensions to a data domain Cartesian coordinates, we have provided the “extend” argument of imshow. We specify the image’s screen resolution with a number like “aspect=4”. It will prevent the portrait from distorting. The aspect of the graph is set to 1 by default. In the end, we represent the graph using the plt.show() method.
Draw Different Graphs Having a Unique Color Scheme:
The Imshow() method in matplotlib draws a figure by a 2-d NumPy array. Every attribute of the array would be represented by a block in the illustration. The data of the appropriate attribute and the color pattern utilized by the imshow() function define the shade of every block.
import numpy as np
n = 6
m = np.reshape(np.linspace(0,1,n**2), (n,n))
cmap = 'gray',
plt.title('Graph 1', y=0.3, fontsize=20)
plt.imshow(m, cmap = 'viridis', interpolation='nearest')
plt.title('Graph 2', y=0.3, fontsize=20)
plt.imshow(m, cmap = 'viridis', interpolation='bicubic')
plt.title('Graph 3', y=0.3, fontsize=20)
Here, we have to introduce matplotlib.pyplot and NumPy libraries that allow us to draw different graphs and execute some numeric functions. Next, we take a variable with “n” that represents the number of rows and columns in the subplots.
Further, we declare a new variable used to store the NumPy array. We apply figsize() to specify the size of the subplots. In this situation, we have to plot three dissimilar graphs. Now, to draw the first graph, we apply the subplot() function. And the imshow() method is called to draw the array. This function holds three parameters. The “cmap” is given as a parameter to this function which is being used to define the color of the blocks. The third parameter, “interpolation”, is utilized to mix the colors of the block, but the nearest colors will not be mixed with them.
Now, we employ the plt.ticks() method to the x and y axes, respectively. This is used to set the range of the number of ticks on both axes. In addition, the method plt.title() is applied to define the label of the graph and font size of the label.
Now, we will draw a second graph using the identical data points of the x and y axes. But here, we draw the graph with the different color schemes. For the second graph, we again call plt.subplot() function. The method plt.imshow() is utilized to update this function’s “cmap” parameter.
Here, we employ the plt.ticks() function to define the range of ticks. We also set the title of the second graph and its font size. Now it’s time to map the third graph. This graph uses the same array as above, but it is drawn by blending the colors as shown in the figure. The functions plt.subplots(), imshow() and plt.ticks() are now declared for this graph.
In the end, the title of this graph is also set by using the plt.title() method. We display the graphs with the help of the show() method.
Draw a Chessboard:
We will create a chessboard having only two shades. So we will utilize the NumPy library to make an array containing two integers, 0 and 1. In this step, 1 represents a bright hue, and 0 represents a dark or dull shade. Let’s draw a 10×10 matrix chessboard with the help of the imshow() function.
import matplotlib.pyplot as plt
First, we integrate the NumPy and Matplotlib libraries to perform graphic and mathematical methods. Now, we declare an array by the use of the NumPy library. This array is used to create a 10×10 matrix containing two numbers. These two numbers represent the dark color block and bright tint block.
Next, we call the print() statement to print the array. In addition, the plt.imshow() function is defined to draw the graph. Here, we set the origin of the plot using the “origin” parameter.
In this artifact, we discussed using the imshow() function. The purpose of using the imshow() method is to display the graphic object. We also utilize the multiple arguments of the imshow function to execute various operations on the graph. The “origin” argument of the imshow() method is being utilized to modify the graph’s origin. We hope you found this article helpful. Check the other Linux Hint articles for tips and tutorials.