Matplotlib 1
Matplotlib 1
Line plot :
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
# Function to plot
plt.plot(x,y)
Bar plot :
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
Histogram :
# Y-axis values
y = [10, 5, 8, 4, 2]
Scatter Plot :
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 2]
Pyplot in Matplotlib
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in
Python. Matplotlib can be used in Python scripts, the Python and IPython shell, web
application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc.
Parameters: This function accepts parameters that enables us to set axes scales and format
the graphs. These parameters are mentioned below :-
plot(x, y): plot x and y using default line style and color.
plot.axis([xmin, xmax, ymin, ymax]): scales the x-axis and y-axis from minimum to
maximum values
plot.(x, y, color=’green’, marker=’o’, linestyle=’dashed’, linewidth=2, markersize=12):
x and y co-ordinates are marked using circular markers of size 12 and green color line with
— style of width 2
plot.xlabel(‘X-axis’): names x-axis
plot.ylabel(‘Y-axis’): names y-axis
plot(x, y, label = ‘Sample line ‘) plotted Sample Line will be displayed as a legend
plt.legend()
plt.show()
plt.xlabel('Years')
plt.ylabel('Power consumption in kWh')
plt.legend()
plt.show()
Axes class
Axes is the most basic and flexible unit for creating sub-plots. Axes allow placement of plots at any
location in the figure. A given figure can contain many axes, but a given axes object can only be in
one figure. The axes contain two axis objects 2D as well as, three-axis objects in the case of 3D.
Let’s look at some basic functions of this class.
axes() function
axes() function creates axes object with argument, where argument is a list of 4 elements [left,
bottom, width, height]. Let us now take a brief look to understand the axes() function.
add_axes() function
Alternatively, you can also add the axes object to the figure by calling the add_axes() method. It
returns the axes object and adds axes at position [left, bottom, width, height] where all quantities are
in fractions of figure width and height.
Syntax :
add_axes([left, bottom, width, height])
ax.legend() function
Adding legend to the plot figure can be done by calling the legend() function of the axes class. It
consists of three arguments.
Syntax :
ax.legend(handles, labels, loc)
ax.plot() function
plot() function of the axes class plots the values of one array versus another as line or marker.
Syntax : plt.plot(X, Y, ‘CLM’)
Parameters:
X is x-axis.
Y is y-axis.
‘CLM’ stands for Color, Line and Marker.
Example: The following example shows the graph of sine and cosine functions.
import matplotlib.pyplot as plt
import numpy as np
ax.set_title("Trigonometric Functions")
plt.show()
# importing library
import matplotlib.pyplot as plt
# Creating 2 subplots
fig, ax = plt.subplots(2)
# Accessing each axes object to plot the data through returned array
ax[0].plot(x, y)
ax[1].plot(x, z)
# importing library
import matplotlib.pyplot as plt
import numpy as np
Steps Needed
Import Libraries
Create/ Load data
Make subplot
Plot subplot
Set title to subplots.
Example 1: (Using set_title() method)
We use matplotlib.axes._axes.Axes.set_title(label) method to set title (string label) for the current
subplot Axes.
# importing packages
import numpy as np
import matplotlib.pyplot as plt
# create data
x=np.array([1, 2, 3, 4, 5])
# making subplots
fig, ax = plt.subplots(2, 2)
# set spacing
fig.tight_layout()
plt.show()
# importing packages
import numpy as np
import matplotlib.pyplot as plt
# create data
x=np.array([1, 2, 3, 4, 5])
# making subplots
fig, ax = plt.subplots(2, 2)
# set spacing
fig.tight_layout()
plt.show()
# importing packages
import numpy as np
import matplotlib.pyplot as plt
# create data
x=np.array([1, 2, 3, 4, 5])
# making subplots
fig, ax = plt.subplots(2, 2)
for i in range(4):
# subplots
plt.subplot(2, 2, i+1)
# plotting (x,y)
plt.plot(x, y[i])
# importing packages
import numpy as np
import matplotlib.pyplot as plt
# create data
x=np.array([1, 2, 3, 4, 5])
# making subplots
fig, ax = plt.subplots(2, 2)
for i in range(4):
# subplots
plt.subplot(2, 2, i+1)
# plotting (x,y)
plt.plot(x, y[i])
# set the title to subplots
plt.gca().title.set_text(title[i])
# set spacing
fig.tight_layout()
plt.show()