Chapter 2 - part 1 - Line charts & Bar charts
Chapter 2 - part 1 - Line charts & Bar charts
Data Visualization
Data visualization is the presentation of data in a pictorial or
graphical format.
1
7/10/2024
Matplotlib
Matplotlib is a most popular, high quality, 2D plotting
library for the Python programming language and its
numerical mathematics extension is NumPy.
Installing Matplotlib
To install matplotlib:
pip install matplotlib
2
7/10/2024
What is pyplot ?
pyplot is a collection of methods within the matplot
library (of python) which allows user to construct 2D
plots easily and interactively.
Import pyplot
To make use of pyplot methods, you need to
import it by issuing following command:
import matplotlib.pyplot
or
import matplotlib.pyplot as py
3
7/10/2024
4
7/10/2024
TYPES OF CHARTS
1 4
LINE CHART HISTOGRAM
2 5
BAR CHART BOX CHART
3 6
PIE CHART SCATTER PLOT
1. Line Chart
A Line Graph or Chart is a simple
graph that shows the result in the
form of lines.
It displays information as a series
of data points called markers
connected by straight lines
It represents trends over a time.
Line chart can be created using
the plot() function in the pyplot
module.
5
7/10/2024
plot() function
It is a function in the pyplot module.
Used to create line charts.
Syntax:
plt.plot ( x, y, color, marker type, line width, linestyle,
marker, marker size,
marker edge color)
x is the data on the horizontal axis (X-axis)
y is the data on the vertical axis (Y-axis)
Remaining parameters help us to change the marker style
and the line style.
6
7/10/2024
7
7/10/2024
Pan axis with left button /Zoom any section in the plotted figure with right button
Save the figure to the hard disk (Save as dialog box pops up)
8
7/10/2024
9
7/10/2024
10
7/10/2024
Values of linestyle
linestyle Abbreviation Eg:
p.plot(x, y, linestyle = ‘solid‘ ) or
solid - p.plot(x, y, linestyle = '-‘ )
Values of color
Color Code Color Code
Red ‘r’ Green ‘g’
Blue ‘b’ Yellow ‘y’
Megenta ‘m’ Cyan ‘c’
Black ‘k’ White ‘w’
color is a parameter of plot() function to which a color or it’s code can be assigned and
colour of the line changes.
Eg: p.plot (x, y, color = ‘m’) or
p.plot (x, y, color='magenta‘)
Default value of color is ‘b’ or ‘blue’
11
7/10/2024
Value of linewidth
Line thickness increases according to the value of the
linewidth
You can change the line width as,
linewidth = <width> in points
Width should be a positive integer , i.e., a number >0
No line is displayed if width is zero.
12
7/10/2024
13
7/10/2024
Example: subplot()
Here, subplot() function divides the
figure in such a way that there are 2
rows and 1 column. Chart 1 is placed
in the 1st subplot and Chart 2 in the
2nd subplot. There is no space
between the 2 subplots.
14
7/10/2024
Example: subplots_adjust()
Here, hspace parameter of
subplots_adjust() function is the
amount of height reserved for space
between two subplots
Example: subplot()
Here, subplot() function divides the
figure in such a way that there are 2
columns and 1 row. Chart 1 is placed
in the 1st subplot and Chart 2 in the
2nd subplot. There is no space
between the 2 subplots
15
7/10/2024
Example: subplots_adjust()
Here, wspace parameter of
subplots_adjust() function is the
amount of width reserved for space
between the two subplots
Example: arange()
• arange() function of numpy library
is a tool for creating numeric
sequences in python.
Syntax:
np.arange(start, stop, step)
16
7/10/2024
17
7/10/2024
18
7/10/2024
19
7/10/2024
20
7/10/2024
Program to draw a line chart to show the population of India vs Pakistan from1960 – 2010
21
7/10/2024
Q. 39 :: ANSWER
22
7/10/2024
Q. 40 :: ANSWER
23
7/10/2024
24
7/10/2024
25
7/10/2024
ANSWER
26
7/10/2024
ANSWER
27
7/10/2024
Q. 34 :: ANSWER
OR
28
7/10/2024
Q. 8 :: ANSWER
OR
29
7/10/2024
Q. 35 :: ANSWER
30
7/10/2024
Write a program to draw a line with suitable label in the X-axis and
Y-axis, and a title.
Q. 37 :: ANSWER
OR
31
7/10/2024
Write a program to draw a line with suitable label in the X-axis and
Y-axis, and a title.
Q. 38 :: ANSWER
32
7/10/2024
Q. 1 :: ANSWER
33
7/10/2024
HW :PG 3.64
QN:2 to 5
2.What is matplotlib?
3. What do you mean by pip?
4. How do we update pip?
5. Why do we update pip?
Homework
Answers
34
7/10/2024
3. pip is the standard package manager for Python. It allows you to install and
manage additional packages that are not part of the Python standard library.
5. if you’re using an old version of pip, installing the latest version of a Python
package might fail—or install in a slower, more complex way.
35
7/10/2024
Pros
• Work well in showing trends chronologically.
• Clearly display relationships with continuous periodical data.
• Visualize data changes at a glance.
Cons
• Matches best with periodical data.
• Mess the chart if many categories are compared in one line chart.
2. BAR CHART
36
7/10/2024
2. BAR CHART
A bar chart is a 2D data visualization made up of
rectangular bars each for a specific category with its
length representing the value of that category.
Hence, bar chart or bar graph is a chart or graph that
presents categorical data with rectangular bars.
The heights / lengths of bars are proportional to the values
that they represent.
It is useful to compare a given numeric value on different
categories.
2. BAR CHART
It can also be used with 2 data series.
The bars can be plotted vertically or horizontally.
We can configure the width, alignment, color, ticks, limits
and label of the chart.
X-axis will be a range with the same quantity as that of the
items.
For e.g.: If there are 4 items, there will be 4 bars and
hence X-axis will be a range of 4 values].
37
7/10/2024
38
7/10/2024
39
7/10/2024
40
7/10/2024
41
7/10/2024
42
7/10/2024
43
7/10/2024
44
7/10/2024
45
7/10/2024
46
7/10/2024
Home Work
47
7/10/2024
48
7/10/2024
49
7/10/2024
SAVING FIGURE
matplotlib.pyplot.savefig(“filenamewithpath”)
For example:
plt.savefig(“pchart.png”)
• Or you can click the save button on the GUI panel
• The file will be saved in the python folder.
50