Installing Matplotlib
python -m pip install matplotlibConcepts Behind Matplotlib
The two main concepts that you have to understand before creating a matplotlib chart or graph are
Figure
Axes
Here Figure is the whole main window or the canvas which contain your graphs/plots.
While Axes is the actual plotting area where our graphs are drawn or plotted.
Please note that Axes is different from Axis (x or y).

A single Matplotlib Figure can have multiple Axes as shown below.
No Values have been plotted to the axes.
import matplotlib.pyplot as plt
figure,axes = plt.subplots(nrows=2,ncols=2) # this will create
# 4 axes shown below
plt.show()
You can also have multiple Figures with multiple Axes as shown below.
import matplotlib.pyplot as plt
figure1,axes1 = plt.subplots(nrows=1,ncols=2) #create Figure1 and its axes
figure2,axes2 = plt.subplots(nrows=2,ncols=3) #create Figure2 and its axes
plt.show()

Creating a Simple Line Chart using Matplotlib
Now we will create a simple Line Chart using the Matplotlib Python Library.
The main steps needed to create a line plot or chart is shown below.
Create a list of x and y values which are needed to draw the line
Create a Figure and Axes Object using the subplots() method
Now Plot the x and y values on the plotting area (axes) using .plot() method
call .show() method to show the plots on the screen
You can find the code for doing that below.
#Simple Line chart
import matplotlib.pyplot as plt
x= [1, 2,3,4, 5,6, 7,8, 9,10,11,12,13,14,15] #list of values on x axis
y= [-5,0,5,10,0,20,0,12,7,-1,3, 8,2,0,-16] #list of values on y axis
figure,axes = plt.subplots() # creates a drawing area for your graph
# returns a figure and an axes object
# tuple expansion
axes.plot(x,y,'-o') #plot x,y values on axes object.
axes.set_title('Name of the Axes') #set the name of the Axes
axes.grid(True) #show grid lines
figure.canvas.manager.set_window_title('Name of the Window') # Setting the name of the Window/Figure
plt.show()
On running the Code it will create the below window.

Tags
- Log in to post comments