0% found this document useful (0 votes)
78 views

MATPLOTLIB NOTES Pandas

plt.plot(x,y) plots the sine wave with default settings. To customize: 1. Set labels, title, limits using plt.xlabel, plt.ylabel, plt.title, plt.xlim, plt.ylim 2. Customize line color, style, markers using plt.plot(,color, linestyle, marker) 3. Add legend with plt.legend(loc) 4. Show plot with plt.show() This document discusses various data visualization techniques in Python using Matplotlib library. It explains the purpose of data visualization and basic plotting components like figure, axes, titles, labels and customization options for legends, colors, styles and markers.

Uploaded by

AISHI SHARMA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

MATPLOTLIB NOTES Pandas

plt.plot(x,y) plots the sine wave with default settings. To customize: 1. Set labels, title, limits using plt.xlabel, plt.ylabel, plt.title, plt.xlim, plt.ylim 2. Customize line color, style, markers using plt.plot(,color, linestyle, marker) 3. Add legend with plt.legend(loc) 4. Show plot with plt.show() This document discusses various data visualization techniques in Python using Matplotlib library. It explains the purpose of data visualization and basic plotting components like figure, axes, titles, labels and customization options for legends, colors, styles and markers.

Uploaded by

AISHI SHARMA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CHAPTER – 6 – DATA VISUALIZATION

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'

f) Line Style: Line style can be mentioned in plot function.


Syntax: plt.plot(data1, data2, linestyle='line_style_code_str')
Line Styles codes
Line Type CODE
-------------------------------------- solid
……………………………………... dotted
-------------------------------------------- dashed
--.--.--.--.--.--.--.--.--.--.--.-- dashdot

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:

Example 2: Plotting above plot with customization:


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.
#D=Diamond, r=red
plt.plot(x,y,'r',marker='D',markersize=4,markeredgecolor='yellow',li
nestyle='dashed')
OR
plt.plot(x,y,'rD',markersize=4,markeredgecolor='yellow',linestyle='dashed')
# 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:

Example 3: Plotting multiple data sets.


import numpy as np
import matplotlib.pyplot as plt
set1 = np.arange(1, 11, 1)
set2 = np.sin(set1)
set3 = np.cos(set1)
set4 = np.tan(set1)
plt.plot(set1, set2, 'rp', linestyle='solid')
plt.plot(set1, set3, 'gh', linestyle='dashed')
plt.plot(set1, set4, 'bd', linestyle='dotted')
plt.show()
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 2: Plotting bar() chart with different bar thickness


import matplotlib.pyplot as plt
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
plt.bar(langs,students,width=0.8) #Width in bar() function can be in
the range of 0 to 1; default width is 0.8
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 4: Plotting bar() chart with different colour of bars


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=widths,color='g') #Setting same length
list of widths and colour 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:

Example 6: Plotting multiple data sets using bar() function


import numpy as np
import matplotlib.pyplot as plt
stud_codes = ['stud1', 'stud2', 'stud3', 'stud4', 'stud5']
java_score = [35, 45, 40, 42, 29]
python_score = [28, 29, 30, 31, 40]
html_score = [41, 25, 36, 39, 50]
#Generating numbers for X points
X = np.arange(5)
#Setting first data set at X+0.00 position with 0.25 width
plt.bar(X+0.00, java_score, color='g', width=0.25)
#Setting first data set at X+0.00+0.25 position with 0.25 width
plt.bar(X+0.25, python_score, color='r', width=0.25)
#Setting first data set at X+0.00+0.25+0.25 position with 0.25 width
plt.bar(X+0.50, html_score, color='b', width=0.25)
plt.show()
Output:
d. Horizontal Bar Chart/Plot: barh() function is used in place of bar() function to create the bar chart
horizontally.
Syntax: matplotlib.pyplot.barh(x, y, width, color)
Where x & y are data sets
width is thickness or list of thickness of bars
colour is colour or list of colours of bars
Example: Plotting Horizontal Bar Chart with multiple bars:
import numpy as np
import matplotlib.pyplot as plt
java_score = [35, 45, 40, 42, 29]
python_score = [28, 29, 30, 31, 40]
html_score = [41, 25, 36, 39, 50]
#Generating numbers for X points
X = np.arange(5)
width = 0.2
#Setting first data set at X+0.00 position with 0.25 width
plt.barh(X+0.0, java_score, width, color='g')
#Setting first data set at X+0.00+0.25 position with 0.25 width
plt.barh(X+0.2, python_score, width, color='r')
#Setting first data set at X+0.00+0.25+0.25 position with 0.25 width
plt.barh(X+0.4, html_score, width, color='b')
plt.show()
Output:

e. Histogram: A histogram is an accurate representation of the distribution of numerical data. It is


an estimate of the probability distribution of a continuous variable. It is like a Bar graph, however
there is no gap between the bars. The matplotlib.pyplot.hist() function plots a histogram.
The following table lists down the parameters for a histogram –
x array or sequence of arrays
bins integer or sequence or 'auto', optional
optional parameters
cumulative If True, then a histogram is computed where each bin gives the counts in that bin
plus all bins for smaller values.
histtype The type of histogram to draw. Default is 'bar'
 'bar' is a traditional bar-type histogram. If multiple data are given the bars are
arranged side by side.
 'barstacked' is a bar-type histogram where multiple data are stacked on top
of each other.
 'step' generates a lineplot that is by default unfilled.
 'stepfilled' generates a lineplot that is by default filled.
Example 1 - Following example plots a histogram of marks obtained by students in a class. Four
bins, 0-25, 26-50, 51-75, and 76-100 are defined. The Histogram shows number of students falling
in this range.
import numpy as np
import matplotlib.pyplot as plt
a = np.array([22, 87, 5, 43, 56, 73, 55, 54, 11, 20, 51, 5, 79, 31,
27])
plt.hist(a, bins = [0,25,50,75,100]) #main_line
plt.xticks([0,25,50,75,100])
plt.show()
Output:
Example 2 – Above plot with cumulative True in #main_line
plt.hist(a, bins = [0,25,50,75,100], cumulative=True)
Output –

Example 3 – Above plot using different histtypes in #main_line


plt.hist(a, bins = [0,25,50,75,100], histtype = 'bar' | 'step' |
'stepfilled')
histtype = 'bar'

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:

Example 5 – Plotting barstacked histogram using multiple data


import numpy as np
import matplotlib.pyplot as plt
a = np.array([22, 87, 5, 43, 56, 73, 55, 54, 11, 20, 51, 5, 79, 31,
27])
b = np.array([79, 31, 27, 22, 87, 73, 55, 54, 11, 20, 5, 43, 56, 51,
5])
plt.hist( [ a , b] , bins = [ 0, 25, 50, 75, 100 ], histtype =
'barstacked') #main_line
plt.xticks([0,25,50,75,100])
plt.show()
Output:

You might also like