Plotting of line graph from NumPy array
This tutorial will help you in plotting various line graphs from NumPy array in Python. To plot a line graph in Python we need to import two libraries of Python on our code space as follows.
from matplotlib import pyplot as plt import NumPy as np
In case your code space doesn’t have the two libraries, you can install them by using the syntax below:
-pip install matplotlib- -pip install NumPy-
Matplotlib is a library in Python which is particularly designed to plot various kinds of graphs like line graphs, histograms, bar graphs, and many more in Python programming. In this tutorial, we will focus on only plotting one type of graph which is the line graph.
One of the libraries in Python, works on problems related to arrays and it is also useful in working for mathematical operations like matrix, Fourier transforms, and linear algebra. A NumPy array is a grated framework of the same type of values that have positive tuples as their index.
Graph of a straight line
We can make a straight-line graph using the following syntax:
from matplotlib import pyplot as plt import numpy as np # Assigning a variable "list" to store the following list list=[1,2,3,4] # Converting that list to a NumPy array x=np.array(list) # Equation of a line y=2*x+1 #give a title to the graph plt.title("Straight line graph") #plot the graph plt.plot(x,y) # to make the graph visible plt.show()
Exponential graph using line graph
We can also plot a parabolic graph using a line graph by writing the following syntax:
from matplotlib import pyplot as plt import numpy as np # np. arange() method defines the range of the array, each element is at equidistance from one another. x=np.arange(1,21) # Equation is set y=x*x #give a title to the graph plt.title("Exponential graph") #plot the graph plt.plot(x,y) # to make the graph visible plt.show()
Leave a Reply