Statement of Completion#039c7fe0
Visualizations with Matplotlib
easy
Practice Matplotlib with Business sales analysis
Resolution
Activities
Import Libraries¶
In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
Load the dataset¶
In [2]:
sales_df = pd.read_csv('business-sales.csv')
sales_df.head()
Out[2]:
division | level of education | training level | work experience | salary | sales | |
---|---|---|---|---|---|---|
0 | computer hardware | associate's degree | 0 | 4 | 75650 | 209271 |
1 | office supplies | associate's degree | 2 | 7 | 96502 | 394503 |
2 | printers | some college | 0 | 2 | 55359 | 113345 |
3 | printers | associate's degree | 1 | 6 | 83955 | 301349 |
4 | computer software | some college | 1 | 6 | 103202 | 372674 |
In [5]:
sales_df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 1000 entries, 0 to 999 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 division 1000 non-null object 1 level of education 1000 non-null object 2 training level 1000 non-null int64 3 work experience 1000 non-null int64 4 salary 1000 non-null int64 5 sales 1000 non-null int64 dtypes: int64(4), object(2) memory usage: 47.0+ KB
Activities¶
1. Analyzing salary vs sales¶
Template for Guidance:
In [4]:
fig, ax = plt.subplots(figsize=(10, 6))
# Edit the code below to create your scatter plot
ax.scatter(sales_df['salary'], sales_df['sales'], marker='o')
ax.set_title('Salary vs. Sales')
ax.set_xlabel('Salary')
ax.set_ylabel('Sales')
Out[4]:
Text(0, 0.5, 'Sales')
2. Use different sizes to represent working experiences¶
Template for Guidance:
In [7]:
fig, ax = plt.subplots(figsize=(10, 6))
# Edit the code below to create your scatter plot
my_plot = ax.scatter(sales_df['salary'], sales_df['sales'],
s=sales_df['work experience'],
alpha=0.3)
ax.set_title('Salary vs. Sales')
ax.set_xlabel('Salary')
ax.set_ylabel('Sales')
legend = ax.legend(*my_plot.legend_elements("sizes", num=10),
title="Work experience years")
ax.add_artist(legend)
Out[7]:
<matplotlib.legend.Legend at 0x701aa2d83b90>
3. Use colors to show different training levels¶
Template for Guidance:
In [9]:
sales_df['training level'].unique()
Out[9]:
array([0, 2, 1, 3])
In [15]:
# Figure and axis
fig, ax = plt.subplots(figsize=(10, 6))
# Define unique training levels and corresponding colors
unique_levels = sales_df['training level'].unique()
colors = ['red', 'blue', 'green', 'orange'] # Add more colors if needed
color_map = dict(zip(unique_levels, colors))
# Plot each training level with a different color
for level in unique_levels: # Write your code here
# Select data for the current training level
subset = sales_df[sales_df['training level'] == level]
# Scatter plot with specified color and label
# Edit the code below to create your scatter plot
ax.scatter(subset['salary'], subset['sales'],
color=color_map[level], label=f'Training Level {level}')
# Set plot title and axis labels
ax.set_title('Salary vs. Sales')
ax.set_xlabel('Salary')
ax.set_ylabel('Sales')
# Display legend
ax.legend()
Out[15]:
<matplotlib.legend.Legend at 0x701aa0863a90>