MATPLOTLIB For Python
MATPLOTLIB For Python
Matplotlib
• Matplotlib is a low level graph plotting library in python that
serves as a visualization utility.
• Matplotlib was created by John D. Hunter.
• Matplotlib is open source and we can use it freely.
• Matplotlib is mostly written in python, a few segments are
written in C, Objective-C and Javascript for Platform
compatibility.
Matplotlib Pyplot
plt.plot(xpoints, ypoints)
plt.show()
Draw a line in a diagram from position (0,0) to position (6,250):
plt.plot(xpoints, ypoints)
plt.show()
Draw two points in the diagram, one at position (1, 3) and one
in position (8, 10):
• import matplotlib.pyplot as plt
import numpy as np
plt.plot(xpoints, ypoints)
plt.show()
Plotting without x-points:
plt.plot(ypoints)
plt.show()
• plt.legend()
• Example
• plt.legend([“blue”, “green”], bbox_to_anchor=(0.75, 1.15),
ncol=2)
Attributes
• The Following are some more attributes of function legend() :
• shadow: [None or bool] Whether to draw a shadow behind the
legend.It’s Default value is None.
• markerscale: [None or int or float] The relative size of legend
markers compared with the originally drawn ones.The Default is
None.
• numpoints: [None or int] The number of marker points in the legend
when creating a legend entry for a Line2D (line).The Default is None.
• fontsize: The font size of the legend.If the value is numeric the size
will be the absolute font size in points.
• facecolor: [None or “inherit” or color] The legend’s background
color.
• edgecolor: [None or “inherit” or color] The legend’s background
patch edge color.
Single legends
import numpy as np
import matplotlib.pyplot as plt
# X-axis values
x = [1, 2, 3, 4, 5]
# Y-axis values
y = [1, 4, 9, 16, 25]
# Function to plot
plt.plot(x, y)
# Y-axis values
y1 = [2, 3, 4.5]
# Y-axis values
y2 = [1, 1.5, 5]
# Function to plot
plt.plot(y1)
plt.plot(y2)
• import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
Create a Title for a Plot
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.grid()
plt.show()
Display Multiple Plots
• With the subplot() function you can draw multiple plots in one figure:
• import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()
• The subplot() function takes three arguments that describes
the layout of the figure.
• The layout is organized in rows and columns, which are
represented by the first and second argument.
• The third argument represents the index of the current plot.
plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is
the first plot.
plt.subplot(1, 2, 2)
the figure has 1 row, 2 columns, and this plot is
the second plot.
Matplotlib Scatter
• With Pyplot, you can use the scatter() function to draw a scatter
plot.
• The scatter() function plots one dot for each observation. It needs
two arrays of the same length, one for the values of the x-axis, and
one for values on the y-axis
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)
plt.show()
Draw two plots on the same
figure:
• import matplotlib.pyplot as plt
import numpy as np
plt.show()
Set your own color of the
markers:
• import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y, color = 'hotpink')
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y=
np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85
])
plt.scatter(x, y, color = '#88c999')
plt.show()
Matplotlib Bars
• With Pyplot, you can use the bar() function to draw bar
graphs:
plt.bar(x,y)
plt.show()
Bar chart
Horizontal Bars
plt.barh(x, y)
plt.show()
Bar Color
• Histogram
• A histogram is a graph showing frequency distributions.
• It is a graph showing the number of observations within each
given interval.
• Example: Say you ask for the height of 250 people, you might
end up with a histogram like this:
Create Histogram
import numpy as np
print(x)
plt.hist(x)
plt.show()
Pie Chart
• With Pyplot, you can use the pie() function to draw pie charts:
plt.pie(y)
plt.show()
Labels
• Labels
• Add labels to the pie chart with the label parameter.
• The label parameter must be an array with one label for each
wedge: