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:
- Straight Line for
- Graph for Sine function
- Creating a single figure with Multiple Lines (Sine and Cose function)
- 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
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) print(x) y = 2*x + 1 plt.plot(x, y) plt.show()
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:
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:
- The first step is to import the
matplotlib.pyplotwith an alias of plt.
- The next step is to import Numpy with an alias of np in order to use arrays and functions related to it.
- Then we had imported the Math module for the mathematical calculations required in the visualization.
- Now the ndarray object of angles between 0 and 2(pie) is obtained using the
arange()function from the NumPy library.
- In order to plot the values from the two arrays, we have used the
- In the next two lines after it, we have set the x and y label of the graph.
- After that in the next line, we have set the title for the graph with the help of
- In order to display the graph we had used the
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) plt.show()
The output for the same is given below:
In this tutorial, we have covered how to plot a straight line, to plot a curved line, single 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.