Plot Line Graph from NumPy Array in Python

Introduction:

In this tutorial, we are learning about Plot line graphs from NumPy array in Python. A line graph is a way to show the relationship between two sets of data. Its whole purpose is to provide change over time. To draw plots from NumPy arrays we can use matplotlib, the oldest and most widely used Python plotting library. Additionally, it can be easily combined with NumPy, making it easy to create graphs that represent patterns and trends in given data. Matplotlib is used with NumPy files to plot all kinds of plots. In Matplotlib, we use the special function pyplot(), which is used to plot two-dimensional data. There are various functions to perform plot line graphs from NumPy array in Python, which are given below -

  1. arange(start, end): This function is returns the evenly spaced values of the range [start, end].
  2. title(): This function is used to give a title to the graph. To the function, the title is passed as a parameter.
  3. xlabel(): This function sets the label name of the x axis. The name of the x axis is assigned as a parameter to the function.
  4. ylabel(): This function sets the label name of the Y axis. The name of the Y axis is passed as an argument to the function.
  5. plot(): This function plots the parameter values passed to it together.
  6. show(): This function shows all images to the console.

Program Code:

Here, we give a program code to plot a line graph from a NumPy array in Python. In this program, we will create a numpy array axis X with values ranging from 0 to 25 and then use the sin method to create the corresponding axis Y array. We will use the method plot() to draw a line graph and also update the graph with a title and labels for the x and y axes. The code is given below -

Output:

Now we compile and run the above program. After running this, we will find the line graph, which is given below -

Plot Line Graph from NumPy Array in Python

Program Code:

Here, we give a program code to plot a line graph from a NumPy array in Python. In this program, we use the plot() method for plotting multiple lines in Python. The approach of this program is given below -

  1. Firstly, import the numpy library and pyplot module from the matplotlib library with the name "np" and rename them to plt.
  2. Then initialize our row as data points using numpy arrays.
  3. After that, use the 'plot()' method to plot the x coordinate value against the y coordinate value.
  4. Then use 'title', 'legend', 'xlabel' and 'ylabel' to add some information about the plot.
  5. Using the "show()" method to view the results and exit.

The code is given below -

Output:

Now we compile and run the above program. After running this, we will find the line graph, which is given below -

Plot Line Graph from NumPy Array in Python

Program Code:

Another example is where we will use the same rules as the previous example to draw two lines instead of three lines. In this program, we use the plot() method for plotting multiple lines in Python. The code is given below -

Output:

Now we compile and run the above program. After running this, we will find the line graph, which is given below -

Plot Line Graph from NumPy Array in Python

Program Code:

Here, we give a program code to plot a line graph from a NumPy array in Python. In this program, we will create a numpy array axis X with values ranging from 1 to 13. We will use the method plot() to draw a line graph and also update the graph with a title and labels for the x and y axes. The code is given below -

Output:

Now we compile and run the above program. After running this, we will find the line graph, which is given below -

Plot Line Graph from NumPy Array in Python

Program Code:

Here, we give another program code to plot a line graph from the NumPy array in Python. The code is given below -

Output:

Now we compile and run the above program. After running this, we will find the line graph, which is given below -

Plot Line Graph from NumPy Array in Python

Conclusion:

In this tutorial, we are learning how to Plot line graphs from NumPy arrays in Python. We have given five examples that show how to plot line graphs from NumPy arrays using the plot() method, which is provided in the matplotlib library. The line graph is a way to visualize different patterns and sets of data.