How to plot a graph in matplotlib for beginners

How to plot a graph in matplotlib for beginners


Code:
import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [8,12,10,3]
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Graph')
plt.plot(x,y)
#plt.show()    # you will add this line only when you are not using notebook

Lets try to understand how this code works
  • First we will import matplotlib.pyplot as plt so that we can call our graph plotting function by using plt
  • After we will define our list of x and y make sure no. of elements in both the list is same else you will see error
  • xlabel, ylabel, title are called to give the name of x axis , y axis and graph title respectively
  • Now we will show the graph by simply typing plt.plot(x,y)
    Note if you are not using notebook then you also need to add plt.show() at the end of code so that you can see graph in pop up windows by default notebook does not need this



Post a Comment

0 Comments