Data Science Matplotlib Library Data Visualization, ASSIGNMENT - 6, GO_STP_8113

SOLUTIONS :

  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]​​​​​


import matplotlib.pyplot as plt 
Days=[1,2,3,4,5,6,7,8]
Speed=[6062615856574663]
plt.plot(Days,Speed)
plt.xlabel('days')
plt.ylabel('car speed')
plt.title('Car Speed Measurement')
plt.show()


OUTPUT :




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.

#line style green dotted line

Days=[1,2,3,4,5,6,7,8]
Speed=[6062615856574663]
plt.plot(Days,Speed,"g:")
plt.xlabel('days')
plt.ylabel('car speed')
plt.title('Car Speed Measurement')
plt.show()

OUTPUT :



#marker shape like + 

Days=[1,2,3,4,5,6,7,8]
Speed=[6062615856574663]
plt.plot(Days,Speed,"g:",marker='+')
plt.xlabel('days')
plt.ylabel('car speed')
plt.title('Car Speed Measurement')
plt.show()

OUTPUT:




#change markersize 

Days=[1,2,3,4,5,6,7,8]
Speed=[6062615856574663]
plt.plot(Days,Speed,"c-",markersize=30,marker='*')
plt.xlabel('days')
plt.ylabel('car speed')
plt.title('Car Speed Measurement')
plt.show()

OUTPUT:





#marker face color

Days=[1,2,3,4,5,6,7,8]
Speed=[6062615856574663]
plt.plot(Days,Speed,"y-",marker='o',markerfacecolor='red')
plt.xlabel('days')
plt.ylabel('car speed')
plt.title('Car Speed Measurement')
plt.show()

OUTPUT:






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]

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.title('Max Avg Min Speeds of Car')
plt.plot(days,max_speed,color="red",label="Maximum Speed")
plt.plot(days,min_speed,color="blue",label="Minimum Speed")
plt.plot(days,avg_speed,color="yellow",label="Average Speed")
plt.xlabel('days')
plt.ylabel('speed')
plt.grid()
plt.legend()
plt.show()

OUTPUT:




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

import numpy as np
import matplotlib.pyplot as plot
x = np.arange(0100.1)
a = np.sin(x)
b = np.cos(x)
plt.plot(x,a,color='cyan',label='sin wave')
plt.plot(x,b,color='orange',label='cos wave')
plt.title('Sine & Cosine waves')
plt.legend()
plt.grid()
plt.show()


OUTPUT:





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.


Languages =['Python''SQL''Java''C++''JavaScript']
Popularity = [5639343429]
Security = [44 ,36 ,555042]
plt.bar(Languages,Popularity,color="blue",label='Popularity')
plt.bar(Languages,Security,color="dimgray",label='Security')
plt.legend()
plt.show()

OUTPUT:


plt.barh(Languages,Popularity,color="blue",label='Popularity',height=0.4)
plt.barh(Languages,Security,color="dimgray",label='Security',height=0.5)
plt.legend()

OUTPUT:




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


Marks = [61,86,42,46,73,95,65,78,53,92,55,69,70,49,72,86,64]
print('Below Average')
for i in Marks:
  if i>=40 and i<60:
    print(i)
print('Average')
for i in Marks:
  if i>=60 and i<=80:
    print(i)
print('Excellent')
for i in Marks:
  if i>=80 and i<=100:
    print(i) 
plt.hist(Marks)
plt.show()


OUTPUT:

Below Average 42 46 53 55 49 Average 61 73 65 78 69 70 72 64 Excellent 86 95 92 86


7.  Titanic Data Set Download Data

 Load the data file

 (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



df=pd.read_csv("Titanic.csv")
df.head()

sex = df.groupby(["Sex"])["Survived"].count()
print(sex)
plt.pie(sex)
plt.legend(['female','male'])
plt.show()

OUTPUT:
Sex female 462 male 851 Name: Survived, dtype: int64













Comments