0% found this document useful (0 votes)
2 views

4

The document outlines a series of data visualization tasks using Python libraries such as Pandas and Seaborn. It includes instructions for creating various plots, including stacked histograms, distribution plots, relation plots, joint plots, heatmaps, swarm plots, violin plots, and facet grids, all aimed at analyzing automobile sales and related economic factors. Each section provides code snippets to generate the visualizations based on a dataset of historical automobile sales.

Uploaded by

anuj rawat
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 views

4

The document outlines a series of data visualization tasks using Python libraries such as Pandas and Seaborn. It includes instructions for creating various plots, including stacked histograms, distribution plots, relation plots, joint plots, heatmaps, swarm plots, violin plots, and facet grids, all aimed at analyzing automobile sales and related economic factors. Each section provides code snippets to generate the visualizations based on a dataset of historical automobile sales.

Uploaded by

anuj rawat
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
You are on page 1/ 9

xiwf7pq1g

December 26, 2024

[1]: import pandas as pd

# Load the dataset


url = 'https://itv-contentbucket.s3.ap-south-1.amazonaws.com/Exams/AWP/
↪Matplotlib/historical_automobile_sales.csv'

df = pd.read_csv(url)

1) Create a stacked histogram to capture Automobile Sales for Vehicle Types

[2]: import seaborn as sns


import matplotlib.pyplot as plt

# Create stacked histogram for Vehicle Types


plt.figure(figsize=(12, 8))
sns.histplot(data=df, x="Automobile_Sales", hue="Vehicle_Type",␣
↪multiple="stack", palette="viridis")

plt.title('Stacked Histogram of Automobile Sales for Vehicle Types')


plt.xlabel('Automobile Sales')
plt.ylabel('Count')
plt.show()

1
2) Create a distribution plot (kernel density estimation plot) to understand the distribution of
Price for different Cities
[3]: plt.figure(figsize=(12, 8))
sns.kdeplot(data=df, x="Price", hue="City", fill=True, palette="muted")
plt.title('Distribution of Price for Different Cities')
plt.xlabel('Price')
plt.ylabel('Density')
plt.show()

2
3) Create a relation plot to visualise how, along with the effect of Recession, GDP has changed
with time (Year)

[4]: plt.figure(figsize=(14, 8))


sns.relplot(data=df, x="Year", y="GDP", hue="Recession", kind="line",␣
↪palette="coolwarm", height=6, aspect=2)

plt.title('Change in GDP Over Time with Effect of Recession')


plt.xlabel('Year')
plt.ylabel('GDP')
plt.show()

<Figure size 1400x800 with 0 Axes>

3
4) Prepare a Joint Plot to understand the relation between Price, Advertising Expenditure and
Automobile Sales
[5]: sns.jointplot(data=df, x="Price", y="Advertising_Expenditure",␣
↪hue="Automobile_Sales", palette="viridis", kind="scatter")

plt.suptitle('Joint Plot of Price, Advertising Expenditure and Automobile␣


↪Sales', y=1.02)

plt.xlabel('Price')
plt.ylabel('Advertising Expenditure')
plt.show()

4
5) Create a heatmap to understand the correlation for GDP, Growth Rate, and Unemployment
Rate
[9]: # Create a heatmap for GDP, Growth Rate, and Unemployment (assuming actual␣
↪column names)

plt.figure(figsize=(10, 6))
correlation_data = df[['GDP', 'Growth_Rate', 'unemployment_rate']].dropna()
correlation_matrix = correlation_data.corr()

sns.heatmap(correlation_matrix, annot=True, cmap="YlGnBu", fmt='.2f')


plt.title('Correlation Heatmap for GDP, Growth_Rate, and unemployment_rate')
plt.show()

5
6) Make a swarm plot to deduce the automotive sales for every Automobile Type within every
calendar month
[12]: plt.figure(figsize=(16, 10))
sns.swarmplot(data=df, x="Month", y="Automobile_Sales", hue="Vehicle_Type",␣
↪palette="Set2", dodge=True)

plt.title('Automotive Sales for Every Automobile Type Within Every Calendar␣


↪Month')

plt.xlabel('Month')
plt.ylabel('Automobile_Sales')
plt.show()

6
7) Prepare a Violin Plot of how Consumer Confidence has changed with every progressing year

[16]: # Create the violin plot


plt.figure(figsize=(12, 8))
sns.violinplot(data=df, x="Year", y="Consumer_Confidence", hue="Year",␣
↪palette="Set3", dodge=False)

plt.title('Violin Plot of Consumer Confidence Over the Years')


plt.xlabel('Year')
plt.ylabel('Consumer Confidence')
plt.legend(title='Year')
plt.show()

7
8) Create a facet grid of Competition to view the histogram of Advertising Expenditure

[17]: g = sns.FacetGrid(df, col="Competition", height=4, aspect=1.5)


g.map(sns.histplot, "Advertising_Expenditure", bins=20, color="purple")
g.add_legend()
plt.subplots_adjust(top=0.9)
g.fig.suptitle('Histogram of Advertising Expenditure by Competition')
plt.show()

8
9

You might also like