# Data Science Matplotlib Library Data Visualization Question Exercise Practice Solutions

0 like 0 dislike
5.2k views

edited

## This assignment is for visualization using Python Matplotlib Library

1. Load the necessary package for plotting using pyplot from matplotlib. Example - Days(x-axis) represents 8 days and Speed represents a car’s speed. Plot a Basic line plot between days and car speed, put x axis label as days and y axis label as car speed and put title Car Speed Measurement.

Days=[1,2,3,4,5,6,7,8]

Speed=[60,62,61,58,56,57,46,63]​​​​​

2. Now to above car data apply some string formats  like line style example green dotted line, marker shape like +, change markersize, markerface color etc.

3. Plot Axes Labels, Chart title, Legend, Grid in Car minimum, Maximum and average speed in 8 days.

days=[1,2,3,4,5,6,7,8]

max_speed=[80,91,92,88,77,79,76,75]

min_speed=[42,43,40,42,33,36,34,35]

avg_speed=[46,58,57,56,40,42,41,36]

4. Plotting a basic sine graph by adding more features. Adding Multiple plots by Superimposition like cosine wave.

5. Plot Simple bar chart showing popularity of Programming Languages.

Languages =['Python', 'SQL', 'Java', 'C++', 'JavaScript']

Popularity = [56, 39, 34, 34, 29]

Security = [44 ,36 ,55, 50, 42]

Plot Multiple Bars showing Popularity and Security of major Programming Languages. Also Create Horizontal bar chart using barh function.

6.  Plot Histogram, We have a sample data of Students marks of various Students, we will try to plot number of Students by marks range and try to figure out how many Students are average, below-average and Excellent.

Marks = [ 61,86,42,46,73,95,65,78,53,92,55,69,70,49,72,86,64]

Histogram showing Below Average, Average and Execellent distribution

40-60: Below Average

60-80: Average

80-100: Excellent

(i) Create a pie chart presenting the male/female proportion

(ii) Create a scatterplot with the Fare paid and the Age, differ the plot color by gender

0 like 0 dislike
by (342 points)
selected

1. Question

import matplotlib.pyplot as plt # Import Required Package

Days = [1,2,3,4,5,6,7,8]

Speed = [60,62,61,58,56,57,46,63]

plt.plot(Days, Speed, color = 'blue') # For Create a Line Plot

plt.xlabel('Days')

plt.ylabel('Car Speed')

plt.title('Car Speed Measurement')

plt.show()

2. Question

Days = [1,2,3,4,5,6,7,8]

Speed = [60,62,61,58,56,57,46,63]

plt.plot(Days, Speed, color = 'blue', marker = 'o', ms = 20, mec = 'red', mfc = 'green', linestyle = 'dotted', linewidth = 3.5)

plt.xlabel('Days') # ms for marker size, mfc for marker face color, mec for border color

plt.ylabel('Car Speed')

plt.title('Car Speed Measurement')

plt.show()

3. Question

days = [1,2,3,4,5,6,7,8]

max_speed = [80,91,92,88,77,79,76,75]

min_speed = [42,43,40,42,33,36,34,35]

avg_speed = [46,58,57,56,40,42,41,36]

plt.figure(figsize=(8,6))

plt.subplot(2,1,1)

plt.plot(Days, max_speed, color = 'red', marker = '*', ms = 10, mec = 'red', mfc = 'orange', label = 'Maximum Speed')

plt.xlabel('Days')

plt.ylabel('Maximum Speed')

plt.title('Car Speed Measurement')

plt.legend()

plt.grid()

plt.show()

plt.figure(figsize=(8,6))

plt.subplot(2,1,2)

plt.plot(Days, min_speed, color = 'green', marker = 'o', ms = 10, mec = 'black', mfc = 'blue', label = 'Minimum Speed')

plt.xlabel('Days')

plt.ylabel('Minimum Speed')

plt.title('Car Speed Measurement')

plt.legend()

plt.grid()

plt.show()

plt.figure(figsize=(8,6))

plt.subplot(2,1,1)

plt.plot(Days, avg_speed, color = 'yellow', marker = '^', ms = 10, mec = 'red', mfc = 'orange', label = 'Average Speed')

plt.xlabel('Days')

plt.ylabel('Maximum Speed')

plt.title('Car Speed Measurement')

plt.legend()

plt.grid()

plt.show()

4. Question

x = np.arange(0,4*np.pi,0.1)

y = np.sin(x)

z = np.cos(x)

plt.plot(x,y, color = 'green', marker='o')

plt.plot(x,z, color = 'blue', marker = '*')

plt.xlabel('Sin and Cos Value From 0 to 4pi')

plt.ylabel('Sin(x) and Cos(x)')

plt.title('Sin And Cosine Wave')

plt.legend(['sin(X)','Cos(x)'])

plt.show()

5. Question

Languages =['Python''SQL''Java''C++''JavaScript']

Popularity = [5639343429]

Security = [44 ,36 ,555042]

plt.subplot(2,1,1)

plt.bar(Languages, Popularity, width=0.5, color = 'orange', align='center')

plt.xlabel('Languages')

plt.ylabel('Popularity')

plt.legend(['Popularity'])

plt.show()

plt.subplot(2,1,2)

plt.bar(Languages, Security, width=0.5, color = 'red', align='center')

plt.xlabel('Languages')

plt.ylabel('Security')

plt.legend(['Security'])

plt.show()

Horizontal Bar

Languages =['Python''SQL''Java''C++''JavaScript']

Popularity = [5639343429]

Security = [44 ,36 ,555042]

plt.subplot(2,1,1)

plt.barh(Languages, Popularity, color = 'g', align='center')

plt.xlabel('Languages')

plt.ylabel('Popularity')

plt.legend(['Popularity'])

plt.show()

plt.subplot(2,1,2)

plt.barh(Languages, Security, color = 'red', align='center')

plt.xlabel('Languages')

plt.ylabel('Security')

plt.legend(['Security'])

plt.show()

0 like 0 dislike
by (278 points)
0 like 0 dislike
by (342 points)

6. Question

Marks = np.array([61,86,42,46,73,95,65,78,53,92,55,69,70,49,72,86,64])

below_avg = Marks[np.logical_and(Marks >= 40, Marks < 60)]

avg_marks = Marks[np.logical_and(Marks >= 60, Marks < 80)]

exe_marks = Marks[np.logical_and(Marks >= 80, Marks <= 100)]

plt.hist(Marks)

plt.show()

print('Below Average Students Are : ',below_avg.size)

print('Average Students Are : ',avg_marks.size)

print('Execelent Students Are : ',exe_marks.size)

7. Question

import pandas as pd

df['Sex']

x = df.Sex.value_counts()

x

plt.pie(x, autopct='%.0f%%')

plt.legend(['Male','Female'])

plt.title('Male And Female Proportion')

plt.show()

male = df[df['Sex']=='male']

male.Sex.value_counts()

female = df[df['Sex']=='female']

female.Sex.value_counts()

plt.scatter(male.Fare, male.Age, label = 'Male', marker = '*')

plt.scatter(female.Fare, female.Age, label = 'Female', marker = '^')

plt.legend()

plt.xlabel('Fare')

plt.ylabel('Age')

plt.grid()

plt.show()

### Best Online Learning Opportunities

UDEMY::  Attend All Udemy Courses in Just INR 450[Coupon]