Visualize (plot) a NumPy array in Python using Seaborn
Hello programmers, in this tutorial we will see how to visualize a NumPy array in Python using the inbuilt visualization module seaborn.
NumPy is a module built-in Python that is mainly used for scientific computing. Seaborn is a visualization module in Python which is based on matplotlib. It helps in building high-level graphs with more informative statistical graphics.
Install the three modules using the following commands in your command prompt.
pip install numpy pip install matplotlib pip install seaborn
Create a NumPy array
We create a numpy array using the ‘np.array()’ method. We can have a single dimension of the array specified or even multiple dimensions.
For example, we create a numpy array of single dimensions as follows:
#Importing necesary libraries import numpy as np #Create single dimension numpy array function def createSingDimArray(): singDimNpAry = np.array([1,2,3,4,5]) print(f'The type of the array is: {type(singDimNpAry)} and the shape of the array is: {singDimNpAry.shape}.') #driver if __name__ == "__main__": createSingDimArray() #call the function
Output
The type of the array is: <class 'numpy.ndarray'> and the shape of the array is: (5,).
Now, let us create a numpy array of 2 dimensions as follows:
#Importing necesary libraries import numpy as np #Create 2D numpy array function def createMultDimArray(): multDimNpAry = np.array([[1,2,3,4,5], [6,7,8,9,10]]) print(f'The type of the array is: {type(multDimNpAry)} and the shape of the array is: {multDimNpAry.shape}.') #driver if __name__ == "__main__": createMultDimArray() #call the function
Output
The type of the array is: <class 'numpy.ndarray'> and the shape of the array is: (2, 5).
Plotting the NumPy array in Seaborn
We will first create a numpy array and then visualize it using the seaborn library.
npArray = np.random.randint(low = 0, high = 10, size = (3,3)) df = pd.DataFrame(npArray) print(df)
Output
0 1 2 0 2 3 3 1 4 3 7 2 2 8 0
Explanation
We create a numpy array with random values from 0 to 10 with the size of the 2D matrix as 3×3. Then we generate a data frame using the pandas library and print out the data frame to better understand the structure of the data.
We will use a heat map to visualize the randomly generated numpy array created above.
sns.heatmap(npArray, annot = True) plt.title('Heatmap visualization of a random generated numpy array.') plt.show()
Output
The heatmap is depicted.
Explanation
We use seaborn as ‘sns’ and use the inbuilt heatmap method of it. We pass the data and the annotation inside the method as parameters and create the graph for the following.
Let us generate another numpy array and visualize using a bar plot.
npArray = np.array([1,2,3,4,5,6,7,8,9]) npArray2 = np.array([0,1,4,2,3,1,6,4,2])
Visualize the plot
sns.barplot(npArray, npArray2) plt.title('Heatmap visualization of a random generated numpy array.') plt.show()
Output
The bar plot is depicted.
Visualize the above numpy array using a scatter plot.
sns.scatterplot(npArray, npArray2) plt.title('Heatmap visualization of a random generated numpy array.') plt.show()
Output
The scatter plot is depicted.
Visualize the above numpy array using a histogram.
sns.displot(npArray, npArray2) plt.title('Heatmap visualization of a random generated numpy array.') plt.show()
Output
The histogram is depicted.
Explanation
The above-used method is ‘displot()’ which stands for distribution plot. It helps in visualizing mainly the univariate and the bivariate variables using histograms.
Leave a Reply