0% found this document useful (0 votes)
11 views8 pages

Assignment Week-03 (2403B05107)

The document outlines Python programming tasks using Matplotlib to create various types of plots, including a simple line graph, multiple lines on a graph, and multiple subplots. Each task specifies data sets, customization options, and expected outputs for the visualizations. The final result confirms the successful completion of the plotting exercises.

Uploaded by

vinaynaidu6872
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Assignment Week-03 (2403B05107)

The document outlines Python programming tasks using Matplotlib to create various types of plots, including a simple line graph, multiple lines on a graph, and multiple subplots. Each task specifies data sets, customization options, and expected outputs for the visualizations. The final result confirms the successful completion of the plotting exercises.

Uploaded by

vinaynaidu6872
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SR UNIVERSITY

SCHOOL OF COMPUTER SCIENCE AND ENGINEERING

Course- M-Tech Type- Core


Course Code- 24CS301PC501 Course Name- Fundamentals of Data Science
Year- 2024-2025 Date- 07th November
KOLA VINAY KUMAR
2024 Semester- Odd H.T.NO : 2403B05107

3A: Write a Python program using Matplotlib to plot a simple line graph with the following
specifications:
1. Define the x-axis values as [1, 2, 3].
2. Define the corresponding y-axis values as [2, 4, 1].
3. Plot the points and connect them with a line.
4. Label the x-axis as "x - axis" and the y-axis as "y - axis".
5. Set the title of the graph as "My first graph!".
6. Display the graph.

Expected Output:
A line graph with the specified x and y values, labeled axes, and a title.

PROGRAM :

# Importing the required module


import matplotlib.pyplot as plt

# x-axis values
x = [1, 2, 3]
# Corresponding y-axis values
y = [2, 4, 1]

# Plotting the points


plt.plot(x, y)

# Naming the x-axis


plt.xlabel('x - axis')
# Naming the y-axis
plt.ylabel('y - axis')

# Giving a title to the graph


plt.title('My first graph!')

# Function to show the plot


plt.show()
OUTPUT :
3B: Write a Python program using Matplotlib to plot mul ple lines on a graph with the following
these specifications:
1. Define two data sets:
o a = [1, 2, 3, 4, 5]
o b = [0, 0.6, 0.2, 15, 10, 8, 16, 21]
2. Plot:
o Line a as a regular line plot.
o Line b as a sca er plot with red circles.
o A third line with x-values from 0 to 21 with a step of 3.
3. Customize the plot:
o Label the x-axis as "Day ->" and the y-axis as "Temp ->".
o Add a fourth line c = [4, 2, 6, 8, 3, 20, 13, 15] with the label "4th Rep".
4. Customize the graph boundaries:
o Hide the top and right borders of the plot.
o Set the visible range of the le boundary from -3 to 40.
o Customize the ck marks on the x-axis to range from -3 to 10 and on the y-axis from -3 to
20 with intervals of 3.
5. Add a legend and annotate the graph with the text "Temperature V / s Days" at posi on (1.01, -
2.15).
6. Title the graph as "All Features Discussed" and display the plot.

Expected Output:

A plot showing all the specified lines, labels, customizations, title, legend, andannotation.

PROGRAM :

import matplotlib.pyplot as plt

# Data for plotting


a = [1, 2, 3, 4, 5]
b = [0, 0.6, 0.2, 15, 10, 8, 16, 21]

# Plot the list `a`


plt.plot(a)

# Plot the list `b` with red circles (using 'or')


plt.plot(b, "or")

# Plot a range of numbers from 0 to 21 with a step of 3


plt.plot(list(range(0, 22, 3)))

# Naming the x-axis


plt.xlabel('Day ->')
# Naming the y-axis
plt.ylabel('Temp ->')
# Plotting a new dataset `c` and labeling it as '4th Rep'
c = [4, 2, 6, 8, 3, 20, 13, 15]
plt.plot(c, label='4th Rep')

# Get current axes command


ax = plt.gca()

# Hide the right and top boundary lines of the graph


ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

# Set the range or bounds for the left boundary line


ax.spines['left'].set_bounds(-3, 40)

# Set intervals for the x-axis


plt.xticks(list(range(-3, 10)))

# Set intervals for the y-axis with a step of 3


plt.yticks(list(range(-3, 20, 3)))

# Add legend labels for the plots


ax.legend(['1st Rep', '2nd Rep', '3rd Rep', '4th Rep'])

# Annotate a text on the graph at specified coordinates


plt.annotate('Temperature V / s Days', xy=(1.01, -2.15))

# Title of the Graph


plt.title('All Features Discussed')

# Show the plot


plt.show()
OUTPUT :
3C : Write a Python program using Matplotlib to create multiple subplots within a single figure,
following these specifica ons:
1. Define four data sets:
o a = [1, 2, 3, 4, 5]
o b = [0, 0.6, 0.2, 15, 10, 8, 16, 21]
o c = [4, 2, 6, 8, 3, 20, 13, 15]
o A sequence generated by list(range(0, 22, 3))
2. Create a 2x2 grid of subplots within a figure of size (10, 10).
3. Plot each data set on a separate subplot:
o Subplot 1: Plot a with square blue markers ('sb'), set x-ticks to range from 0 to 9 with a
step of 1, and title as "1st Rep".
o Subplot 2: Plot b with red circle markers ('or'), set x-ticks to range from 0 to 9 with a step
of 2, and title as "2nd Rep".
o Subplot 3: Plot list(range(0, 22, 3)) with green triangle-down markers ('vg'), set x-ticks from
0 to 9 with a step of 1, and title as "3rd Rep".
o Subplot 4: Plot c with diamond magenta markers ('Dm'), set y-ticks from 0 to 23 with a
step of 2, and title as "4th Rep".
4. Display the plot.

Expected Output:
A 2x2 grid of subplots, each showing the specified data, custom markers, tick settings, and titles.

PROGRAM :

import matplotlib.pyplot as plt

# Data for plotting


a = [1, 2, 3, 4, 5]
b = [0, 0.6, 0.2, 15, 10, 8, 16, 21]
c = [4, 2, 6, 8, 3, 20, 13, 15]

# Use `fig` to open a new figure window and specify the window size
fig = plt.figure(figsize=(10, 10))

# Creating multiple subplots in a single figure


sub1 = plt.subplot(2, 2, 1)
sub2 = plt.subplot(2, 2, 2)
sub3 = plt.subplot(2, 2, 3)
sub4 = plt.subplot(2, 2, 4)

# Plotting data in each subplot


sub1.plot(a, 'sb') # 's' is for square markers, 'b' for blue
sub1.set_xticks(list(range(0, 10, 1))) # Set x-ticks for sub1
sub1.set_title('1st Rep')
sub2.plot(b, 'or') # 'o' is for circle markers, 'r' for red
sub2.set_xticks(list(range(0, 10, 2))) # Set x-ticks for sub2
sub2.set_title('2nd Rep')

sub3.plot(list(range(0, 22, 3)), 'vg') # 'v' is for triangle markers, 'g' for green
sub3.set_xticks(list(range(0, 10, 1))) # Set x-ticks for sub3
sub3.set_title('3rd Rep')

sub4.plot(c, 'Dm') # 'D' is for diamond markers, 'm' for magenta


sub4.set_yticks(list(range(0, 24, 2))) # Set y-ticks for sub4
sub4.set_title('4th Rep')

# Display the plot


plt.show()
OUTPUT :

Result:

Thus the basic plots using Matplotlib in Python program was successfully completed.

You might also like