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

ML#03 - Matplotlib 3

Uploaded by

Kiki Nhabinde
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ML#03 - Matplotlib 3

Uploaded by

Kiki Nhabinde
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

In [ ]: import matplotlib.

pyplot as plt
import numpy as np
import pandas as pd

In [2]: #1. Write a Python program to plot two or more lines on same plot with suitable legends

x1 =np.array ([10, 20, 30])


x2 =np.array ([10, 20, 30])
y1 = np.array([20, 40, 10])
y2= np.array ([40,10, 30]) plt.xlabel ("x-axis") plt.ylabel ("y-axis")

plt.plot(x1,y1, color= "purple") plt.plot( x2, y2, color = "green")


plt.legend(["Line 1", "Line 2"],loc = "upper right")

<matplotlib.legend.Legend at 0x2d814877a20>
Out[2]:

In #2. Write a Python program to plot two or more lines with legends, different widths and
[3]:
x1 =np.array ([10, 20, 30])
x2 =np.array ([10, 20, 30])
y1 = np.array([20, 40, 10])
y2= np.array ([40,10, 30]) plt.xlabel ("x-axis") plt.ylabel ("y-axis")

plt.plot(x1,y1, color= "blue", linewidth = 3) plt.plot( x2, y2, color = "red", linewidth =5)
plt.legend(["Line 1 width =3", "Line 2 width = 5"],loc = "upper right")

<matplotlib.legend.Legend at 0x2d81493c5f8>

Out[3]:
In [4]: # Write a Python programming to display a bar chart of the popularity of programming
Lan
x= ['Java', 'Python',' PHP', 'JavaScript', 'C#', 'C+
+'] y= [22.2, 17.6, 8.8, 8, 7.7, 6.7]
plt.bar( x,y, color="blue")
plt.xlabel(" Programming Languages")
plt.ylabel ("Popularity")
plt.title ("Popularity of programming languages worldwide, Oct 2017 compared to a year
a

plt.minorticks_on()
plt.grid(which='major', linestyle='-', linewidth='0.5', color='red')
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')

In [5]: #3. Write a Python program to draw a scatter plot comparing two subject marks
of #Mathematics and Science. Use marks of 10 students.

math_marks = [88, 92, 80, 89, 100, 80, 60, 100, 80, 34]
science_marks = [35, 79, 79, 48, 100, 88, 32, 45, 20, 30]
marks_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

plt.scatter(math_marks, marks_range, c ="red")


plt.scatter(science_marks, marks_range, c ="green")
plt.xlabel(" Marks Range")
plt.ylabel ("Marks
Scored") plt.title
("Scatter Plot")
plt.legend(["math_marks", "science_marks"],loc = "upper right")

plt.show()
In [13]: #4. Write a Python programming to create a pie chart of the popularity
of #programming Languages.

import matplotlib.pyplot as plt

# Data
programming_languages = ['Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C+
+'] Popularity = [22.2, 17.6, 8.8, 8, 7.7, 6.7]
explode = (0.1, 0, 0, 0, 0, 0) # Explode the second slice (Python)

# Create a pie chart with labels


plt.pie(Popularity, explode=explode, labels=programming_languages, autopct='%1.1f%%',
shadow=True, startangle=90)

plt.show()

In [ ]:

You might also like