You are on page 1of 4

I utilized the "titanic_data.

xls" dataset from a previous assignment to design a multi-graph with

Python. The process involved implementing a library to create several subplots arranged in a

grid-like format. Each subplot can exhibit various visualizations or depict distinct aspects of the

data. Below is the Python code utilized and the corresponding output.

—------------------------------------

import pandas as pd

import matplotlib.pyplot as plt

# Loading the data

df = pd.read_excel('titanic_data.xls')

#Creating a Multi-Graph

fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(12, 8)) # Create a 2x2 grid of subplots

# Subplot 1: Bar Plot of Passenger Class Distribution

axs[0, 0].bar(df['Passenger Class'].value_counts().index, df['Passenger

Class'].value_counts().values)

axs[0, 0].set_title('Passenger Class Distribution')

axs[0, 0].set_xlabel('Passenger Class')

axs[0, 0].set_ylabel('Count')

# Subplot 2: Histogram of Age Distribution

axs[0, 1].hist(df['Age'].dropna(), bins=20, color='green')

axs[0, 1].set_title('Age Distribution')

axs[0, 1].set_xlabel('Age')

axs[0, 1].set_ylabel('Count')
# Subplot 3: Pie Chart of Survival Rate

survival_counts = df['Survived'].value_counts()

axs[1, 0].pie(survival_counts, labels=['Not Survived', 'Survived'], autopct='%1.1f%%',

startangle=90)

axs[1, 0].set_title('Survival Rate')

# Subplot 4: Box Plot of Fare Distribution

axs[1, 1].boxplot(df['Passenger Fare'].dropna())

axs[1, 1].set_title('Fare Distribution')

# Adjusting the spacing between subplots

plt.tight_layout()

# Displaying the Multi-Graph

plt.show()

—------------------------------------
To create a multi-graph using the "titanic_data.xls" dataset, we first load it using pd.read_excel()

and create a DataFrame named df. We then use plt.subplots() to specify the number of rows

(nrows) and columns (ncols) of subplots we want in the grid and set the overall size of the

multi-graph using figsize.

Each subplot can display different visualizations. In this example, we have created four

subplots:

1. The passenger class distribution is shown using a bar plot in axs[0, 0].bar().

2. The age distribution is displayed using a histogram in axs[0, 1].hist().

3. The survival rate is presented as a pie chart in axs[1, 0].pie().

4. The fare distribution is demonstrated with a box plot in axs[1, 1].boxplot().


We set the titles, labels, and other relevant properties for each subplot using methods like

set_title(), set_xlabel(), set_ylabel(), etc. Finally, we use plt.tight_layout() to adjust the spacing

between subplots and plt.show() to display the multi-graph.

You might also like