how to plot a linear equation in matplotlib 1

how to plot a linear equation in matplotlib

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,100)
y = 2*x+1
plt.plot(x, y, '-r', label='y=2x+1')
plt.title('Graph of y=2x+1')
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
plt.legend(loc='upper left')
plt.grid()
plt.show()

Here is what the above code is Doing:
1. We are creating a variable x and assigning it to a numpy array of 100 evenly spaced numbers between -5 and 5.
2. We are creating a variable y and assigning it to the equation 2x+1.
3. We are using the plot function to plot the x and y variables.
4. We are using the title function to give the plot a title.
5. We are using the xlabel and ylabel functions to give the x and y axes labels.
6. We are using the legend function to add a legend to the plot.
7. We are using the grid function to add a grid to the plot.
8. We are using the show function to display the plot.

Similar Posts