Simple Plot in Python using Matplotlib - GeeksforGeeks

04 Jan, 2022
Matplotlib is a Python library that helps in visualizing and analyzing the data and helps in better understanding of the data with the help of graphical, pictorial visualizations that can be simulated using the matplotlib library. Matplotlib is a comprehensive library for static, animated and interactive visualizations.
Step 1: Open command manager (just type “cmd” in your windows start search bar)Step 2: Type the below command in the terminal.
cd Desktop
Step 3: Then type the following command.
pip install matplotlib
Python3
# importing the required module import matplotlib.pyplot as plt     # x axis values x = [1,2,3] # corresponding y axis values y = [2,4,1]     # plotting the points  plt.plot(x, y)     # naming the x axis plt.xlabel('x - axis') # naming the y axis plt.ylabel('y - axis')     # giving a title to my graph plt.title('My first graph!')     # function to show the plot plt.show() 
Output:
The code seems self-explanatory. Following steps were followed:
Define the x-axis and corresponding y-axis values as lists.
Plot them on canvas using .plot() function.
Give a name to x-axis and y-axis using .xlabel() and .ylabel() functions.
Give a title to your plot using .title() function.
Finally, to view your plot, we use .show() function.
Let’s have a look at some of the basic functions that are often used in matplotlib.
Note: Try removing the features added one by one and understand how does the output result changes
Example 1:
Python3
import matplotlib.pyplot as plt    a = [1, 2, 3, 4, 5]b = [0, 0.6, 0.2, 15, 10, 8, 16, 21]plt.plot(a)  # o is for circles and r is # for redplt.plot(b, "or")  plt.plot(list(range(0, 22, 3)))  # naming the x-axisplt.xlabel('Day ->')  # naming the y-axisplt.ylabel('Temp ->')  c = [4, 2, 6, 8, 3, 20, 13, 15]plt.plot(c, label = '4th Rep')  # get current axes commandax = plt.gca()  # get command over the individual# boundary line of the graph bodyax.spines['right'].set_visible(False)ax.spines['top'].set_visible(False)  # set the range or the bounds of # the left boundary line to fixed rangeax.spines['left'].set_bounds(-3, 40)  # set the interval by  which # the x-axis set the marksplt.xticks(list(range(-3, 10)))  # set the intervals by which y-axis# set the marksplt.yticks(list(range(-3, 20, 3)))  # legend denotes that what color # signifies whatax.legend(['1st Rep', '2nd Rep', '3rd Rep', '4th Rep'])  # annotate command helps to write# ON THE GRAPH any text xy denotes # the position on the graphplt.annotate('Temperature V / s Days', xy = (1.01, -2.15))  # gives a title to the Graphplt.title('All Features Discussed')  plt.show()
Output:
Example 2:
Python3
import matplotlib.pyplot as plt    a = [1, 2, 3, 4, 5]b = [0, 0.6, 0.2, 15, 10, 8, 16, 21]c = [4, 2, 6, 8, 3, 20, 13, 15]  # use fig whenever u want the # output in a new window also # specify the window size you# want ans to be displayedfig = plt.figure(figsize =(10, 10))  # creating multiple plots in a # single plotsub1 = plt.subplot(2, 2, 1)sub2 = plt.subplot(2, 2, 2)sub3 = plt.subplot(2, 2, 3)sub4 = plt.subplot(2, 2, 4)  sub1.plot(a, 'sb')  # sets how the display subplot # x axis values advances by 1# within the specified rangesub1.set_xticks(list(range(0, 10, 1)))sub1.set_title('1st Rep')  sub2.plot(b, 'or')  # sets how the display subplot x axis# values advances by 2 within the# specified rangesub2.set_xticks(list(range(0, 10, 2)))sub2.set_title('2nd Rep')  # can directly pass a list in the plot# function instead adding the referencesub3.plot(list(range(0, 22, 3)), 'vg')sub3.set_xticks(list(range(0, 10, 1)))sub3.set_title('3rd Rep')  sub4.plot(c, 'Dm')  # similarly we can set the ticks for # the y-axis range(start(inclusive),# end(exclusive), step)sub4.set_yticks(list(range(0, 24, 2)))sub4.set_title('4th Rep')  # without writing plt.show() no plot# will be visibleplt.show()
Output:
nnr223442
Python-matplotlib
Python
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Convert integer to string in Python
Convert string to integer in Python
Python infinity
How to set input type date in dd-mm-yyyy format using HTML ?
Matplotlib.pyplot.title() in Python