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

Chapter 6.1 Data Visualization MatPlotlib

The document contains multiple Python programs that utilize the pandas and matplotlib libraries to visualize data from an IBM dataset. It includes various types of charts such as line charts, pie charts, scatter plots, bar charts, stacked bar charts, and area plots to represent employee data and job satisfaction levels. Each program reads the dataset, processes the data, and displays the corresponding visualizations.

Uploaded by

aventador3366
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)
2 views12 pages

Chapter 6.1 Data Visualization MatPlotlib

The document contains multiple Python programs that utilize the pandas and matplotlib libraries to visualize data from an IBM dataset. It includes various types of charts such as line charts, pie charts, scatter plots, bar charts, stacked bar charts, and area plots to represent employee data and job satisfaction levels. Each program reads the dataset, processes the data, and displays the corresponding visualizations.

Uploaded by

aventador3366
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

Prog1LineChart

#Reading dataset
import pandas as pd
ibmdata = pd.read_csv("[Link]")
#Importing necessary libraries
import [Link] as plt

#Creating and displaying a line chart


[Link](figsize=(20, 10))
[Link](ibmdata['HourlyRate'])
[Link]("Line chart for hourly rate of employees of HR
department")
[Link]()

OUTPUT
Prog2MultipleLines
#Reading dataset
import pandas as pd
ibmdata = pd.read_csv("[Link]")
#Program to display multiple lines with different shapes
and colors
import [Link] as plt

#Defining four lists


perf_a = list(range(1, 11))
perf_b = list(range(5, 55, 5))
perf_c = list(range(10, 110, 10))
perf_d = list(range(20, 210, 20))

print("First list:", perf_a)


print("Second list:", perf_b)
print("Third list:", perf_c)
print("Fourth list:", perf_d)

#Creating a chart with different colors and effects for


different data
[Link](perf_a, perf_a, 'g^',
perf_a, perf_b, 'bs',
perf_a, perf_c, 'b--',
perf_a, perf_d, 'mo')

#Setting limits of x-axis


[Link](0, 11)
[Link](0, 220)

#Setting limits of y-axis using the axis() command


[Link]('X axis')

#Displaying grid in chart


[Link](True, color='k')

[Link]('Multiple lines with different shapes and


colors')
#Displaying the chart
[Link]()

OUTPUT

Prog3DotandLine

#Reading dataset
import pandas as pd
ibmdata = pd.read_csv("[Link]")
#Importing necessary libraries
import [Link] as plt

[Link](figsize=(20, 10))

#Creating and displaying a line chart and dot plot


together
[Link](1, 2, 1)
[Link](ibmdata['HourlyRate'],'ro',
ibmdata['HourlyRate'],'m')
[Link]("Dot and line pot together")
[Link](True)

#Creating and displaying a line chart and dot plot


together, but of initial 10 records
[Link](1, 2, 2)
[Link](ibmdata['HourlyRate'].head(10),'ro',
ibmdata['HourlyRate'].head(10),'m')
[Link]("Dot and line pot together")
[Link](True)
[Link]()

OUTPUT

Prog4PieChart

#Reading dataset
import pandas as pd
hribmdata = pd.read_csv("[Link]")
#Importing necessary libraries
import [Link] as plt
# Determine categories of job satisfaction
print("Categories of Job Satisfaction:\n",
hribmdata['JobSatisfaction'].unique())

# Filtering records of different job satisfaction


satisfaction_1 = hribmdata['JobSatisfaction'] == 1
satisfaction_2 = hribmdata['JobSatisfaction'] == 2
satisfaction_3 = hribmdata['JobSatisfaction'] == 3
satisfaction_4 = hribmdata['JobSatisfaction'] == 4

# Determining number of records for different


satisfaction levels
s1 = satisfaction_1.value_counts()
s2 = satisfaction_2.value_counts()
s3 = satisfaction_3.value_counts()
s4 = satisfaction_4.value_counts()

# Creating a list of elements


list = [s1[True], s2[True], s3[True], s4[True]]
print("The values are:\n", list)

# Creating and displaying a pie chart


level = ['satisfaction_1', 'satisfaction_2',
'satisfaction_3', 'satisfaction_4']
[Link](figsize=(20, 10))
[Link](list, labels=level)
[Link]("Job satisfaction of employees of HR
department")
[Link]()
OUTPUT

Prog5ScatterPlot

#Reading dataset
import pandas as pd
hribmdata = pd.read_csv("[Link]")
#Importing necessary libraries
import [Link] as plt
[Link](figsize=(20, 10))
[Link](121)
[Link](hribmdata['Age'], hribmdata['DailyRate'])
[Link]("Scatter plot for age with daily rate")
[Link](122)
[Link](hribmdata['Age'],
hribmdata['TotalWorkingYears'])
[Link]("Scatter plot for age and total working
years")
[Link]()
OUTPUT

Prog6BarChart

#Reading dataset
import pandas as pd
hribmdata = pd.read_csv("[Link]")
#Importing necessary libraries
import [Link] as plt
# Creating multiple bar charts on one image with respect
to gender
# Filtering the data of different gender
Male = hribmdata[hribmdata['Gender'] == 'Male']
Female = hribmdata[hribmdata['Gender'] == 'Female']

# Determining mean of variables for different gender


age_gender = [Male['Age'].mean(), Female['Age'].mean()]
daily_rate_gender = [Male['DailyRate'].mean(),
Female['DailyRate'].mean()]
hourly_rate_gender = [Male['HourlyRate'].mean(),
Female['HourlyRate'].mean()]
year_promotion_gender = [
Male['YearsSinceLastPromotion'].mean(),
Female['YearsSinceLastPromotion'].mean()
]
year_company_gender = [
Male['YearsAtCompany'].mean(),
Female['YearsAtCompany'].mean()
]
year_total_gender = [
Male['TotalWorkingYears'].mean(),
Female['TotalWorkingYears'].mean()
]

# Creating a list of gender


list = ['Male', 'Female']

# Creating a chart
[Link](1, figsize=(20, 10))
[Link]("Information of employees of different
genders")

[Link](231)
[Link](list, age_gender)
[Link]("Age")

[Link](232)
[Link](list, daily_rate_gender)
[Link]("Daily rate")

[Link](233)
[Link](list, hourly_rate_gender)
[Link]("Hourly rate")

[Link](234)
[Link](list, year_promotion_gender)
[Link]("Years since last promotion")
[Link](235)
[Link](list, year_company_gender)
[Link]("Years at company")

[Link](236)
[Link](list, year_total_gender)
[Link]("Total working years")

[Link]()

OUTPUT

Prog7StackedBar

#Importing necessary libraries


import [Link] as plt
[Link](1, figsize=(5,5))

Departments = ['Operations', 'Marketing', 'HR',


'Finance', 'Technical']

Salary_1930 = [10,20,24,30,20]
Salary_1940 = [12,27,30,42,26]
Salary_1950 = [15,35,34,50,33]
Salary_1960 = [27,43,43,57,38]
Salary_1970 = [35,45,46,62,40]
Salary_1980 = [38,49,55,65,45]
Salary_1990 = [42,52,60,72,50]
Salary_2000 = [48,54,65,75,58]
Salary_2010 = [53,58,70,77,63]

#Creating a stacked bar chart


[Link](Departments, Salary_2010, color='green')
[Link](Departments, Salary_2000, color='red')
[Link](Departments, Salary_1990, color='yellow')
[Link](Departments, Salary_1980, color='blue')
[Link](Departments, Salary_1970, color='brown')
[Link](Departments, Salary_1960, color='orange')
[Link](Departments, Salary_1950, color='purple')
[Link](Departments, Salary_1940, color='magenta')
[Link](Departments, Salary_1930, color='pink')

#Adding labels and title to the chart


[Link]('Departments')
[Link]('Salary in different years')
[Link]('Stacked bar chart')

#Displaying a bar chart


[Link]()

OUTPUT
Prog7ExpensesChart

#Program to create an area plot


import [Link] as plt

month_expenses = [1,2,3,4,5,6,7,8,9,10,11,12]
Expense_2023 = [8,11,9,8,10,9,10,11,12,9,10,11]
Expense_2022 = [8,7,8,6,8,7,5,8,5,7,8,6]
Expense_2021 = [5,6,2,4,6,5,2,4,2,3,4,5]
Expense_2020 = [3,4,5,3,3,6,6,2,6,5,3,4]

[Link](figsize=(15,10))
[Link]([], [], color='pink', label='Expense_2020',
linewidth=5)
[Link]([], [], color='blue', label='Expense_2021',
linewidth=5)
[Link]([], [], color='red', label='Expense_2022',
linewidth=5)
[Link]([], [], color='cyan', label='Expense_2023',
linewidth=5)

#Creating an area chart


[Link](month_expenses,
Expense_2020,
Expense_2021,
Expense_2022,
Expense_2023,
colors=['pink','blue','red','cyan'])

#Deciding the headings


[Link]('Months')
[Link]('Expenses')
[Link]('Area chart for expenses of different years')
[Link]()

#Displaying the chart


[Link]()

OUTPUT

You might also like