Matplotlib Simple Line Plot

The visualization of the single line function that is y=f(x) is simplest among all. Let us take a look at creating simple plots of this type. In this tutorial, we will cover the following simple line plots:

  1. Straight Line for y=f(x) type
  2. Graph for Sine function
  3. Creating a single figure with Multiple Lines (Sine and Cose function)
  4. Curved Line

Now the first and foremost step is to set up the notebook for plotting and importing those functions that we will use:

1. Importing Matplotlib

To import Matplotlib you just need to write the following command:

import matplotlib.pyplot as plt

where we will import matplotlib with an alias plt for the ease.

2. Importing Numpy

As we are going to plot numbers; so in order to plot numbers, we need an array. In the Numpy module of Python, there are many functions for creating array. So we will also import Numpy in our code. For ease, we will import Numpy with an alias np.

The command for the same is given below:

import numpy as np

Graph for y=f(x) type in Matplotlib

Let us see the code snippet for the simplest equation:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1, 1, 50)
y = 2*x + 1

plt.plot(x, y)

In the above code example, plot(x, y) function is mainly used to draw a straight line. The output for this code snippet is given below:

Matplotlin Straight Line Plot

Graph for a Sine wave in Matplotlib

Now we are going to show you the visualization of Sine wave using matplotlib. The code snippet for the same is given below:

Explanation of the code

Let us take a look at the brief explanation of the code:

  1. The first step is to import the matplotlib.pyplot with an alias of plt.
  2. The next step is to import Numpy with an alias of np in order to use arrays and functions related to it.
  3. Then we had imported the Math module for the mathematical calculations required in the visualization.
  4. Now the ndarray object of angles between 0 and 2(pie) is obtained using the arange() function from the NumPy library.
  5. In order to plot the values from the two arrays, we have used the plot() function.
  6. In the next two lines after it, we have set the x and y label of the graph.
  7. After that in the next line, we have set the title for the graph with the help of title() function.
  8. In order to display the graph we had used the show() function.

Creating a single figure with Multiple Lines

If you want to create a single figure containing multiple lines then you just simply need to call the plot function multiple times.

The code for the same is given below:

Creating Graph with curved Lines

To plot any line plot() method is used in Matplotlib. It is not necessary that it should be a straight line always, as we just saw in the example above, how we can plot a wave. We can also plot a curved line.

Let us take an example of a simple curve in Matplotlib:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1, 1, 50)
y = 2**x + 1

plt.plot(x, y)


The output for the same is given below:

Curved line in matplotlib

In this tutorial, we have covered how to plot a straight line, to plot a curved linesingle sine wave and we had also covered plotting of multiple lines.

This is all about plotting simple functions in Matplotlib. Now in our further tutorials, we will cover more magical things with matplotlib. We will dive into some more details about how to control the appearance of the axes and lines.

Follow Us On