Matplotlib
Matplotlib
Introduction to Matplotlib
Installation :
Windows, Linux and macOS distributions have matplotlib and most of its
dependencies as wheel packages. Run the following command to
install matplotlib package :
python -mpip install -U matplotlib
• Open File > Settings > Project from the PyCharm menu.
• Select your current project.
• Click the Python Interpreter tab within your project tab.
• Click the small + symbol to add a new library to the project.
• Now type in the library to be installed, in your
example "matplotlib" without quotes, and click Install Package.
• Wait for the installation to terminate and close all popup windows.
• Line plot
• Histogram
• Bar Chart
• Scatter plot
• Pie charts
• Boxplot
import matplotlib.pyplot
Line Plots-
A line plot is used to see the relationship between the x and y-axis.
Matplotlib Plotting
Plotting x and y points
Example 1:
# To draw a line
import numpy as np
plt.plot(xpoints, ypoints)
plt.show()
Output:
Example 2:
import numpy as np
ypoints = np.array([100,200])
plt.plot(xpoints, ypoints)
plt.show()
Output:
Example 3:
#Draw a line from points (10,20), (50,40), (20,60),(70,80)
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([10, 50, 20, 70])
ypoints = np.array([20, 40, 60, 80])
plt.plot(xpoints, ypoints)
plt.show()
Output:
Example 4:
#Making a line only with y-axis points
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([5,10,15,20,25])
Output:
To plot only the markers, you can use shortcut string notation parameter 'o',
which means 'rings'.
Example:
import numpy as np
plt.show()
Output:
Matplotlib Markers
You can use the keyword argument marker to emphasize each point with a
specified marker (vertex).
Example 1:
# Denoting markers with “+” symbol
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([5,10,15,20,25])
plt.plot(xpoints, marker='+')
plt.show()
Output:
Example 2:
# Denoting marker with “o” symbol
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([5,10,15,20,25])
plt.plot(xpoints, marker='o')
plt.show()
Output:
Example 4:
# marker|line|color
# Draw a line with marker “o”, dotted style “:” and red color “r”
import numpy as np
plt.plot(ypoints, 'o:r')
plt.show()
Example 5:
# Increasing marker size
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([10, 20, 30, 40])
plt.plot(ypoints, 'o:r',ms=20)
plt.show()
Output:
Example 6:
# Marker face color as blue
import matplotlib.pyplot as plt
import numpy as np
plt.plot(ypoints, 'o:r',ms=20,mfc='b')
plt.show()
Output:
Marker reference
Marker
'o' Circle
'*' Star
'.' Point
',' Pixel
'x' X
'X' X (filled)
'+' Plus
's' Square
'D' Diamond
'p' Pentagon
'H' Hexagon
'h' Hexagon
'^' Triangle Up
'<' Triangle Left
'2' Tri Up
'|' Vline
'_' Hline
Line Reference
Line Syntax
Color Reference
Color Syntax
'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
A graph without title and labels is incomplete and one can not interpret
anything from that graph. We can include title and labels as follows.
Example:
import numpy as np
x = np.array([25,30,15,63,25,57,17,83,52,45,37])
y = np.array([85,63,78,89,90,95,75,48,63,55,82])
plt.plot(x, y)
plt.title("Health Index")
plt.xlabel("Average Age")
plt.ylabel("Average Weight")
plt.show()
Output:
Bar plot
Mainly barplot is used to show the relationship between the numeric and
categoric values. In a bar chart, we have one axis representing a particular
category of the columns and another axis representing the values or count of
the specific category. Barcharts are plotted both vertically and horizontally and
are plotted using the following line of code:
plt.bar(x,height,width,bottom,align)
bottom: It’s optional. It is a y-coordinate of the bar its default value is None
Example 1:
#vertical plot
y = [25,50,75,80,85]
plt.bar(x,y)
plt.show()
Output:
Example 2:
#Horizontal Plot
y = [25,50,75,80,85]
plt.barh(x,y)
plt.show()
Output:
Example 3:
y = [25,50,75,80,85]
plt.bar(x,y,color="green")
plt.show()
Output:
Example 4:
from matplotlib import pyplot as plt
y = [25,50,75,80,85]
plt.bar(x,y,width=2)
plt.show()
Output:
Matplotlib Adding Grid Lines
With Pyplot, you can use the grid() function to add grid lines to the plot.
Example:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([10,20,30,40,50,60])
y = np.array([40,50,60,70,80,90])
plt.title("Student Performance")
plt.xlabel("Average Age")
plt.ylabel("Average Marks")
plt.plot(x, y)
plt.grid()
plt.show()
Output:
Note:
Matplotlib Subplot
With the subplot() function you can draw multiple plots in one figure.
Example:
#plot 1:
x = np.array([2, 6, 8, 3])
y = np.array([3, 8, 6, 5])
plt.subplot(2, 1, 1)
# figure has 2 rows, 1 column, and this is figure number 1
plt.plot(x,y)
#plot 2:
x = np.array([3, 1, 11, 13])
y = np.array([7, 5, 13, 11])
plt.subplot(2, 1, 2)
# figure has 2 rows, 1 column, and this is figure number 2
plt.plot(x,y)
plt.show()
Output:
Matplotlib Scatter
Scatter plots are used to show the relationships between the variables and use
the dots for the plotting or it used to show the relationship between two
numeric variables.
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:
Example:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,7,2,9,4])
y = np.array([9,8,7,1,3,7,4,1,6])
plt.scatter(x, y)
plt.show()
Output:
Histogram
Example:
import matplotlib.pyplot as plt
import numpy as np
print(x)
plt.hist(x)
plt.show()
Output:
A pie chart (or circular chart ) is used to show the percentage of the whole.
Hence it is used when we want to compare the individual categories with the
whole.
Example 1:
import matplotlib.pyplot as plt
import numpy as np
plt.pie(y)
plt.show()
Output:
Example 2:
# adding labels to pie chart
import matplotlib.pyplot as plt
import numpy as np
Output:
Example 3:
#explode is used for wedges to stand out. Each value represents how far from
the center each wedge is displayed.
import numpy as np
plt.show()
Output:
Example 4:
import numpy as np
plt.pie(y)
plt.show()
Output: