MATPLOTLIB NOTES Pandas
MATPLOTLIB NOTES Pandas
1. Purpose of plotting - Data Visualization is the presentation of data in graphical format. It helps people
understand the significance of data by summarizing and presenting huge amount of data in a simple
and easy-to-understand format and helps communicate information clearly and effectively.
To plot any chart/graph in Python first we import:
import matplotlib.pyplot as plt
2. Anatomy and Customization of Plot/Chart –
Types of Plots/Charts
SN Description
1 Bar - Make a bar plot.
2 Barh - Make a horizontal bar plot.
3 Boxplot - Make a box and whisker plot.
4 Hist - Plot a histogram.
5 hist2d - Make a 2D histogram plot.
6 Pie - Plot a pie chart.
7 Plot - Plot lines and/or markers to the Axes.
8 Polar - Make a polar plot.
9 Scatter - Make a scatter plot of x vs y.
10 Stackplot - Draws a stacked area plot.
11 Stem - Create a stem plot.
12 Step - Make a step plot.
13 Quiver - Plot a 2-D field of arrows.
a) Figure – The area where pyplot draw/plot every chart is called Figure. The figure contains all
other elements of plot/chart.
Figure Functions
SN Description
1 Figtext - Add text to figure.
2 Figure - Creates a new figure.
3 Show - Display a figure.
4 Savefig - Save the current figure.
5 Close - Close a figure window.
b) Axes – The axes define the area on which actual plot will appear. There are two axes in a plot: 1)
X-axis, the horizontal axis 2) Y-axis, the vertical axis
Axes Functions
SN Description
1 Axes - Add axes to the figure.
2 Text - Add text to the axes.
3 Title - Set a title of the current axes.
4 Xlabel - Set the x axis label of the current axis.
5 Xlim - Get or set the x limits of the current axes.
6 Xscale - Set the scaling of the x-axis.
7 Xticks - Get or set the x-limits of the current tick locations and labels.
8 Ylabel - Set the y axis label of the current axis.
9 Ylim - Get or set the y-limits of the current axes.
10 Yscale - Set the scaling of the y-axis.
11 Yticks - Get or set the y-limits of the current tick locations and labels.
i. Axes label: To define the name for an axis using xlabel and ylabel.
Syntax: plt.xlabel('string_label_for_x_axis')
plt.ylabel('string_label_for_y_axis')
ii. Limits: To define the range of values and number of values marked on X-axis and Y-axis.
Syntax: plt.xlim(limit_begin_int, limit_end_int)
plt.ylim(limit_begin_int, limit_end_int)
Note: While setting up the limits for axes, one must keep in mind that only the data that falls
into the limits of X-axis and Y-axis will be plotted only, rest of the data will not be plotted.
iii. Tick_Marks: The individual points marked on X-axis and Y-axis. Python automatically
decides which data points will have ticks on the axes, however we can customize it using
xticks() and yticks().
Syntax: plt.xticks(tick_list_int,tick_labels_list_str)
plt.yticks(tick_list_int,tick_labels_list_str)
c) Title: To define the Title of the plot/chart which appears on the top.
Syntax: plt.title('Title_Str')
d) Legends: To define which colour identifies different data sets plotted in plot/chart. It shows in a
corner of the plot/chart. To plot legends for any kind of plot/chart below steps needs to be followed:
i. Step 1 – Specify the label for each plotting data set.
ii. Step 2 – Specify the legend with its location
Syntax: plt.legend(loc = location_code_int)
Location codes for legend
LOCATION CODE
upper right 1
upper left 2
lower left 3
lower right 4
e) Line colour: Colour code needs to be mentioned next to the data being plotted in plot() function.
Syntax: plt.plot(data1, data2, color_code_char)
Colour codes
COLOUR CODE
Blue 'b'
Green 'g'
Red 'r'
Cyan 'c'
Magenta 'm'
Yellow 'y'
Black 'k'
Blue 'b'
White 'w'
g) Marker: The data points plotted in plot/chart are called markers. Syntax to customize marker is
given below.
Syntax: plt.plot(data1, data2, marker = 'marker_code',
markersize = integer_value>, markeredgecolor = 'color_code')
Marker codes
Marker type CODE
Point marker '.'
Circle marker 'o'
X marker 'x'
Diamond marker 'D'
Hexagon marker 'H'
Square marker 's'
Plus marker '+'
h) Combining line colour with marker type: Line colour and Marker type can be combined as
(<Colour_Code><Maker_Type>).
Syntax: plt.plot(<data1>, <data2>, 'r+')
#r is line colour and + is marker type
Note: When markeredgecolour is not mentioned in plot() function it takes the same colour as line. If
line colour and marker type are mentioned combined and line style is not mentioned separately,
python will plot the markers only not the line making it Scatter plot.
Example: We shall now display a simple line plot using all kinds of customizations:
Line.1. import matplotlib.pyplot as plt
Line.2. import numpy as np
Line.3. arr1 = np.array([10, 25])
Line.4. arr2 = np.array([15, 40.5])
Line.5. arr3 = np.array([28, 2])
Line.6. arr4 = np.array([20, 0.5])
Line.7. arr5 = np.array([0, 1])
#Setting labels
Line.8. plt.xlabel('AREA OF BUSINESS')
Line.9. plt.ylabel('PROFIT IN BN$')
#Setting limits
Line.10. plt.xlim(0, 1)
Line.11. plt.ylim(5, 35)
#Setting ticks
Line.12. plt.xticks([0, 1], ['2019', '2020'])
Line.13. plt.yticks(np.arange(0, 45, 5))
#Setting Title
Line.14. plt.title('Business Trend')
#Plotting data & setting line color, legends labels, line
style, marker, marker size, marker edge color
Line.15. plt.plot(arr1, 'y', label='GENERAL STORE',
linestyle='solid', marker='h', markersize=10,
markeredgecolor='y')
Line.16. plt.plot(arr2, 'r', label='HOSPITALS', linestyle='dotted',
marker='*', markersize=15, markeredgecolor='m')
Line.17. plt.plot(arr3, 'b', label='HOTELS', linestyle='dashed',
marker='D', markersize=20, markeredgecolor='c')
#using combined line color and marker type
Line.18. plt.plot(arr4, 'gp', label='CONVEYANCE',
linestyle='dashdot', markersize=5, markeredgecolor='r')
#Setting Legends location
Line.19. plt.legend(loc=3)
Line.20. plt.show()
3. Basics of Simple Plotting – Matplotlib comes with a wide variety of plots. Plots helps to understand
trends, patterns, and to make correlations. Some of the commonly used plots are covered here.
a. Line Chart/Plot: Line plot is a type of chart that displays information as a series of data points
connected by straight line segments. A line plot is often the first plot of choice to visualize any time
series data.
Example 1: We shall now display a simple line plot of angle in radians vs. its sine value in
Matplotlib.
import matplotlib.pyplot as plt
import numpy as np
# Obtain the ndarray object of angles between 0 and 2π using the
arange() function from the NumPy library.
x = np.arange(0, 3.14*2, 0.05)
# Obtain sin values of all x in y using the sin() function from the
NumPy library.
y = np.sin(x)
# The values from two arrays are plotted using the plot() function.
plt.plot(x,y)
# You can set the plot title, and labels for x and y axes.
plt.xlabel("angle")
plt.ylabel("sine")
plt.title('sine wave')
plt.show() #The Plot viewer window is invoked by the show() function
Output:
b. Scatter Chart/Plot: It is similar to line chart, only difference is line chart connects all the plotted
markers with a line, scatter chart simply plots the markers/data points to show the trend in data.
scatter() function is used to plot Scatter Chart/Plot in python.
Syntax: plt.scatter(x,y,s,c,marker)
Where x & y are the data structures,
s is the Marker Size
c is the Marker colour
marker is type of marker
Example:
import matplotlib.pyplot as plt
girls_grades = [89, 90, 70, 89, 100, 80, 90, 100, 80, 34]
boys_grades = [30, 29, 49, 48, 100, 48, 38, 45, 20, 30]
grades_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
plt.scatter(grades_range, girls_grades, c='r', s=50, marker='*')
plt.scatter(grades_range, boys_grades, c='b', s=50, marker='p')
plt.show()
Output:
Note – Different colours and sizes can be specified for data points in Scatter() function using list
of colour codes or list of marker size of same length as data.
Example:
import matplotlib.pyplot as plt
girls_grades = [89, 90, 70, 89]
girl_code = ['G01', 'G02', 'G03', 'G04']
colorlist = ['r', 'g', 'm', 'k']
sizelist = [50, 20, 100, 30]
plt.scatter(grades_range, girls_grades, c=colorlist, s=sizelist,
marker='*')
plt.show()
Output:
c. Bar Chart/Plot: A bar chart or bar graph is a chart or graph that presents categorical data with
rectangular bars with heights or lengths proportional to the values that they represent. The bars
can be plotted vertically or horizontally. Using bar() function we can plot the Bar Chart/Plot.
Syntax:
matplotlib.pyplot.bar(x, y, width, color)
Where x & y are data sets
width is the width(s) of the bars default 0.8
color is the color code for data representation
Example 1: Following is a simple example of the Matplotlib bar plot. It shows the number of
students enrolled for various courses offered at an institute.
import matplotlib.pyplot as plt
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
plt.bar(langs,students)
plt.show()
Output:
Example 3: Plotting bar() chart with different thickness for each bar
import matplotlib.pyplot as plt
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
widths = [0.1, 0.2, 0.3, 0.4, 0.5]
plt.bar(langs,students,width=widhts) #Setting same length list of
widths for all bars in the Chart;
plt.show()
Output:
Example 5: Plotting bar() chart with different colours for each bar
import matplotlib.pyplot as plt
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
widths = [0.1, 0.2, 0.3, 0.4, 0.5]
colours = ['g', 'b', 'black', 'y', 'm']
plt.bar(langs,students,width=widths,color=colours)
#Setting same length list of widths and list of colours for all bars
in the Chart;
plt.show()
Output:
histtype = 'step'
histype = 'stepfilled'
Example 4 – Above plot with changed orientation in #main_line and replaced xticks to yticks
plt.hist(a, bins = [0,25,50,75,100], histtype = 'step', orientation
= 'horizontal')
plt.yticks([0,25,50,75,100])
Output: