MATPLOT LIBRARY:
⮚ It’s a python popular library for creating Static, Animated and interactive data
visualization.
⮚ It was created by John D hunter in 2003 and it is free and open source.
⮚ TO INSTALL MATPLOT:
Linux command: Pip install matplotlib
Import [Link] as plt
1. import
● A Python keyword
● Used to bring an external module (library) into your program so you can use its
functions and classes.
2. matplotlib
● The name of a Python library
● It is mainly used for data visualization (creating graphs and charts like line plots, bar
charts, histograms, etc.).
[Link]
● A sub-module of matplotlib
● Provides plotting functions to create line graph, bar chart, pie chart, histogram etc.
● Used to create and control figures, axes, labels, titles, legends, etc.
4. plt
● An alias (short name) for [Link]
● Makes the code shorter and easier to write
MARKERS in Matplotlib: Markers are symbols used to represent data points
on a graph.
Common Marker Types
Mark
Meaning
er
'o' Circle
'.' Point
's' Square
'^' Triangle (up)
'v' Triangle
Mark
Meaning
er
(down)
'+' Plus
'x' Cross
'D' Diamond
'*' Star
Marker Example:
import [Link] as plt
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
[Link](x, y, marker='o')
[Link]("Line Graph with Markers")
[Link]()
COLORS in Matplotlib: Colors are used to make graphs more readable and
attractive.
Common Color Codes
Co
Color
de
'r
'
Red
'g
'
Green
'b
'
Blue
'y
'
Yellow
'k
'
Black
'c
'
Cyan
'm Magen
Co
Color
de
' ta
Color Example
[Link](x, y, color='r')
MARKERS + COLORS (Combined)
import [Link] as plt
x = [1, 2, 3, 4]
y = [5, 7, 9, 11]
[Link](x, y, marker='s', color='g')
[Link]("Markers and Colors Example")
[Link]()
Simple Line Graph Example (Python Program):
import [Link] as plt
# Data for the graph
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a line graph
[Link](x, y)
# Add labels and title
[Link]("X Axis")
[Link]("Y Axis")
[Link]("Simple Line Graph")
# Display the graph
[Link]()
📝 Explanation
● x → values on the X-axis
● y → values on the Y-axis
● [Link](x, y) → draws the line graph
● [Link]() → labels the X-axis
● [Link]() → labels the Y-axis
● [Link]() → gives a title to the graph
● [Link]() → displays the graph
Write a Python program to plot a bar chart using the Matplotlib library. Your program
should:
● 1. Import the necessary libraries.
● 2. Create a list of categories and their corresponding values.
● 3. Plot a bar chart with appropriate labels and a title.
SOLUTION:
# 1. Import necessary libraries
import [Link] as plt
# 2. Create categories and their corresponding values
categories = ['Maths', 'Science', 'English', 'Computer']
values = [75, 85, 65, 90]
# 3. Plot the bar chart
[Link](categories, values)
# Add labels and title
[Link]('Subjects')
[Link]('Marks')
[Link]('Student Marks in Different Subjects')
# Display the chart
[Link]()
What will be the output of the code given below:
my_list = [10, 20, 30, 40, 50];
print(“The number is “,my_list[-4]) ?
ANSWER: 20
Write a Python program that performs the following operations on a list:
1. Create a list: [2, 4, 10, 14, 17]
2. Add 19 to the list.
3. Insert 22 at index 2.
4. Delete the element 10.
5. .Find the index of 4.
6. Print the final list.
SOLUTION:
# 1. Create a list
lst = [2, 4, 10, 14, 17]
# 2. Add 19 to the list
[Link](19)
# 3. Insert 22 at index 2
[Link](2, 22)
# 4. Delete the element 10
[Link](10)
# 5. Find the index of 4
index_of_4 = [Link](4)
print("Index of 4:", index_of_4)
# 6. Print the final list
print("Final List:", lst)
Write a Python program that performs the following operations on a string:
1. Create a string with the value "Artificial Intelligence".
2. Convert the entire string to uppercase and print the result.
3. Find and print the position of the substring "Intelligence" within the string.
4. Replace the substring "Artificial" with "Machine" and print the new string.
5. Check if the string starts with "Machine" and print the result (True/False).
6. Count and print the number of occurrences of the letter 'i' in the string.
SOLUTION:
. # 1. Create a string
s = "Artificial Intelligence"
# 2. Convert the string to uppercase
print("Uppercase:", [Link]())
# 3. Find the position of the substring "Intelligence"
position = [Link]("Intelligence")
print("Position of 'Intelligence':", position)
# 4. Replace "Artificial" with "Machine"
new_string = [Link]("Artificial", "Machine")
print("Replaced String:", new_string)
# 5. Check if the string starts with "Machine"
print("Starts with 'Machine':", new_string.startswith("Machine"))
# 6. Count the number of occurrences of the letter 'i'
count_i = [Link]('i')
print("Number of occurrences of 'i':", count_i)
Output:
● Uppercase: ARTIFICIAL INTELLIGENCE
● Position of "Intelligence": 11
● Replaced String: Machine Intelligence
● Starts with "Machine": True
● Occurrences of 'i': 5