Create chart with matplotlib pyplot from numpy array

In this tutorial, we will learn how to display image chart from numpy array using pyplot from Matplotlib library, Matplotlib is another open source library in python that allow us to create graphical representation of any data, here we see how to display numpy array in form of image using pyplot. learn more about Python Matplotlib library

There are two important libraries from data visualization from Matplotlib, they are pyplot and image.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

Here first we learn how to use pyplot to display numpy array information, then we look how to import image inside chart and display with array data.

import numpy as np
from matplotlib import pyplot as plt

x = np.arange(1,10)
y = np.arange(11,20)

plt.title("Numpy Array with pyplot") 
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")

plt.plot(x,y)
plt.show()

In above code we have created two one-dimension array and then called plot method like plt.plot(x,y)

Here is the chart created from numpy array data, notice, how plot and show method us used.

mumpy array pyplot example

Multiple 1-D arrays in one chart

Now we see how to display multiple one-dimension arrays in one chart, you can think of three products details displayed for comparison.

Here we define three one-dimension array and then add each array to bar property using plt.bar(x, y, align = 'center')

import numpy as np
from matplotlib import pyplot as plt

x = [3,12,9,11]
y = [13,17,6,18]  
plt.bar(x, y, color = 'g', align = 'center') 

x2 = [13,9,11,14]
y2 = [4,15,8,16]
plt.bar(x2, y2, color = 'r', align = 'center')

x3 = [9,11,8,19]
y3 = [7,14,6,17]
plt.bar(x3, y3, color = 'b', align = 'center')

plt.title('multicolor bar graph: WebTrainingRoom') 
plt.ylabel('Y axis') 
plt.xlabel('X axis')  
plt.show()

mumpy array pyplot example

Above image is generated from numpy array using matplotlib pyplot library. We need to add two different arrays in x-axis and y-axis, you can add as many bar as you need using bar method plt.bar(x3, y3, color = 'b', align = 'center') then use pyplot show method to create the image in pop-window.

 
matplotlib pyplot library
Free Tutorials
matplotlib pyplot show method
Python NumPy examples | Join Python Course