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

Python Lab Programs 5

The document contains several Python programming examples for data visualization using matplotlib. It includes creating pie charts, scatter plots, line plots, and reading data from a CSV file for plotting. Each example is accompanied by sample data and code snippets demonstrating how to generate the respective plots.

Uploaded by

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

Python Lab Programs 5

The document contains several Python programming examples for data visualization using matplotlib. It includes creating pie charts, scatter plots, line plots, and reading data from a CSV file for plotting. Each example is accompanied by sample data and code snippets demonstrating how to generate the respective plots.

Uploaded by

Anu S
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Write a Python programming to create a pie chart of the popularity


of programming Languages.
Sample data:
Programming languages: Java, Python, PHP, JavaScript, C#, C++
Popularity: 22.2, 17.6, 8.8, 8, 7.7, 6.7

import [Link] as plt


# Data to plot
languages = 'Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C++'
popuratity = [22.2, 17.6, 8.8, 8, 7.7, 6.7]
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
"#8c564b"]
# explode 1st slice
explode = (0.1, 0, 0, 0,0,0)
# Plot
[Link](popuratity, explode=explode, labels=languages, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
[Link]('equal')
[Link]()

[Link] a Python program to draw a scatter plot with empty circles


taking a random distribution in X and Y and plotted against each
other.
import [Link] as plt
import numpy as np
x = [Link](50)
y = [Link](50)
[Link](x, y, s=70, facecolors='none', edgecolors='g')
[Link]("X")
[Link]("Y")
[Link]()

[Link] a Python program to draw a scatter plot using random


distributions to generate balls of different sizes.

import math
import random
import [Link] as plt
# create random data
no_of_balls = 25
x = [[Link]() for i in range(no_of_balls)]
y = [[Link](0.5, 0.25) for i in range(no_of_balls)]
colors = [[Link](1, 4) for i in range(no_of_balls)]
areas = [[Link] * [Link](5, 15)**2 for i in
range(no_of_balls)]
# draw the plot
[Link]()
[Link](x, y, s=areas, c=colors, alpha=0.85)
[Link]([0.0, 1.0, 0.0, 1.0])
[Link]("X")
[Link]("Y")
[Link]()

[Link] a Python program to draw a scatter plot comparing two


subject marks of Mathematics and Science. Use marks of 10 students.
Test Data:
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]

import [Link] as plt


import pandas as pd
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]
[Link](marks_range, math_marks, label='Math marks', color='r')
[Link](marks_range, science_marks, label='Science marks',
color='g')
[Link]('Scatter Plot')
[Link]('Marks Range')
[Link]('Marks Scored')
[Link]()
[Link]()

5. Write a Python program to draw a line using given axis values


taken from a text file, with suitable label in the x axis, y axis and a
title.
Test Data:
[Link]
12
24
31

import [Link] as plt


with open("[Link]") as f:
data = [Link]()
data = [Link]('\n')
x = [[Link](' ')[0] for row in data]
y = [[Link](' ')[1] for row in data]
[Link](x, y)
# Set the x axis label of the current axis.
[Link]('x - axis')
# Set the y axis label of the current axis.
[Link]('y - axis')
# Set a title
[Link]('Sample graph!')
# Display a figure.
[Link]()
[Link] a Python program to plot two or more lines with
different styles.
import [Link] as plt
# line 1 points
x1 = [10,20,30]
y1 = [20,40,10]
# line 2 points
x2 = [10,20,30]
y2 = [40,10,30]
# Set the x axis label of the current axis.
[Link]('x - axis')
# Set the y axis label of the current axis.
[Link]('y - axis')
# Plot lines and/or markers to the Axes.
[Link](x1,y1, color='blue', linewidth = 3, label = 'line1-
dotted',linestyle='dotted')
[Link](x2,y2, color='red', linewidth = 5, label = 'line2-dashed',
linestyle='dashed')
# Set a title
[Link]("Plot with two or more lines with different styles")
# show a legend on the plot
[Link]()
# function to show the plot
[Link]()
[Link] a Python programming to create a pie chart of gold medal
achievements of five most successful countries in 2016 Summer
Olympics. Read the data from a csv file.

Sample data:
[Link]
country,gold_medal
United States,46
Great Britain,27
China,26
Russia,19
Germany,17

import [Link] as plt


import pandas as pd
df = pd.read_csv('[Link]')
country_data = df["country"]
medal_data = df["gold_medal"]
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"]
explode = (0.1, 0, 0, 0, 0)
[Link](medal_data, labels=country_data, explode=explode,
colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
[Link]("Gold medal achievements of five most successful\
n"+"countries in 2016 Summer Olympics")
[Link]()

You might also like