Swivt Test – 3
29. Pandas & Matplotlib – Exercises
Dataset link: https://pynative.com/wp-content/uploads/2019/01/company_sales_data.csv
29.1. Read Total profit of all months and show it using a line plot
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('company_sales_data.csv')
print(df[['month_number', 'total_profit']])
plt.figure(figsize=(8, 5))
plt.plot(df['month_number'], df['total_profit'], marker='s')
plt.xlabel("Month Number")
plt.ylabel("Total Profits")
plt.title("Total Profits of All Months")
plt.grid(linestyle=":", color="g")
plt.xticks(df['month_number'])
plt.show()
Output,
month_number total_profit
0 1 211000
1 2 183300
2 3 224700
3 4 222700
4 5 209600
5 6 201400
6 7 295500
7 8 361400
8 9 234000
9 10 266700
10 11 412800
11 12 300200

29.2. Get total profit of all months and show line plot with the following Style properties. Generated line plot must include following Style properties: –
- Line Style dotted and Line-color should be red
- Show legend at the lower right location.
- X label name = Month Number
- Y label name = Sold units number
- Add a circle marker.
- Line marker color as red.
- Line width should be 3.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('company_sales_data.csv')
print(df[['month_number', 'total_units']])
plt.figure(figsize=(8, 5))
plt.plot(df['month_number'], df['total_units'], marker='o', color="red", linestyle="dotted", label="Total Profits", linewidth=3, markerfacecolor="red")
plt.xlabel("Month Number")
plt.ylabel("Sold units number")
plt.legend(loc="lower right")
plt.grid(linestyle=":", color="g")
plt.show()
Output,
month_number total_units
0 1 21100
1 2 18330
2 3 22470
3 4 22270
4 5 20960
5 6 20140
6 7 29550
7 8 36140
8 9 23400
9 10 26670
10 11 41280
11 12 30020

29.3. Read all product sales data and show it using a multiline plot
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('company_sales_data.csv')
print(df[['month_number', 'facecream', 'facewash', 'toothpaste', 'bathingsoap', 'shampoo', 'moisturizer']])
plt.figure(figsize=(8, 5))
plt.plot(df['month_number'],df['facecream'], label="facecream", marker="s")
plt.plot(df['month_number'],df['facewash'], label="facewash", marker="s")
plt.plot(df['month_number'],df['toothpaste'], label="toothpaste", marker="s")
plt.plot(df['month_number'],df['bathingsoap'], label="bathingsoap", marker="s")
plt.plot(df['month_number'],df['shampoo'], label="shampoo", marker="s")
plt.plot(df['month_number'],df['moisturizer'], label="moisturizer", marker="s", linestyle=":")
plt.xlabel("Month Number")
plt.ylabel("Sold units number")
plt.grid(linestyle=":", color="g")
plt.legend()
plt.show()
Output,
month_number facecream facewash toothpaste bathingsoap shampoo \
0 1 2500 1500 5200 9200 1200
1 2 2630 1200 5100 6100 2100
2 3 2140 1340 4550 9550 3550
3 4 3400 1130 5870 8870 1870
4 5 3600 1740 4560 7760 1560
5 6 2760 1555 4890 7490 1890
6 7 2980 1120 4780 8980 1780
7 8 3700 1400 5860 9960 2860
8 9 3540 1780 6100 8100 2100
9 10 1990 1890 8300 10300 2300
10 11 2340 2100 7300 13300 2400
11 12 2900 1760 7400 14400 1800
moisturizer
0 1500
1 1200
2 1340
3 1130
4 1740
5 1555
6 1120
7 1400
8 1780
9 1890
10 2100
11 1760

29.4. Read toothpaste sales data of each month and show it using a scatter plot
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('company_sales_data.csv')
print(df[['month_number', 'toothpaste']])
plt.figure(figsize=(8, 5))
plt.scatter(df['month_number'], df['toothpaste'], s=300)
plt.xlabel("Month Number")
plt.ylabel("Sold units number")
plt.grid(linestyle=":", color="g")
plt.title("Toothpaste Monthly Sales")
plt.xticks(df['month_number'])
plt.show()
Output,
month_number toothpaste
0 1 5200
1 2 5100
2 3 4550
3 4 5870
4 5 4560
5 6 4890
6 7 4780
7 8 5860
8 9 6100
9 10 8300
10 11 7300
11 12 7400

29.5. Read face cream and facewash product sales data and show it using the bar chart.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('company_sales_data.csv')
print(df[['month_number', 'facecream', 'facewash']])
plt.figure(figsize=(8,5))
plt.bar(df['month_number'], df['facecream'], label="Facecream")
plt.bar(df['month_number'], df['facewash'], label="Facewash", width=0.35)
plt.xlabel("Month Numbers")
plt.ylabel("Sales Units")
plt.title("Facecream and Facewash: monthly sales data")
plt.grid(linestyle=":", color="g")
plt.legend()
plt.xticks(df['month_number'])
plt.show()
Output,
month_number facecream facewash
0 1 2500 1500
1 2 2630 1200
2 3 2140 1340
3 4 3400 1130
4 5 3600 1740
5 6 2760 1555
6 7 2980 1120
7 8 3700 1400
8 9 3540 1780
9 10 1990 1890
10 11 2340 2100
11 12 2900 1760

29.6. Read sales data of bathing soap of all months and show it using a bar chart. Save this plot to your hard disk.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('company_sales_data.csv')
print(df[['month_number', 'bathingsoap']])
plt.figure(figsize=(8,5))
plt.bar(df['month_number'], df['bathingsoap'])
plt.xlabel("Month Numbers")
plt.ylabel("Total Sales")
plt.title("Bathing Soap Monthly Sales")
plt.grid(linestyle=":", color="g")
plt.xticks(df['month_number'])
plt.show()
Output,
month_number bathingsoap
0 1 9200
1 2 6100
2 3 9550
3 4 8870
4 5 7760
5 6 7490
6 7 8980
7 8 9960
8 9 8100
9 10 10300
10 11 13300
11 12 14400

29.7. Read the total profit of each month and show it using the histogram to see the most common profit ranges
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('company_sales_data.csv')
print(df[['month_number', 'total_profit']])
plt.figure(figsize=(8, 5))
plt.hist(df['total_profit'])
plt.xlabel("Total Profits")
plt.title("Total Profit")
plt.grid(linestyle=":", color="g")
plt.show()
Output,
month_number total_profit
0 1 211000
1 2 183300
2 3 224700
3 4 222700
4 5 209600
5 6 201400
6 7 295500
7 8 361400
8 9 234000
9 10 266700
10 11 412800
11 12 300200

29.8. Calculate total sale data for last year for each product and show it using a Pie chart
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('company_sales_data.csv')
total_facecream = df['facecream'].sum()
total_facewash = df['facewash'].sum()
total_toothpaste = df['toothpaste'].sum()
total_bathingsoap = df['bathingsoap'].sum()
total_shampoo = df['shampoo'].sum()
total_moisturizer = df['moisturizer'].sum()
print("Total Facecream:", total_facecream)
print("Total Facewash:", total_facewash)
print("Total Toothpaste:", total_toothpaste)
print("Total Bathingsoap:", total_bathingsoap)
print("Total Shampoo:", total_shampoo)
print("Total Moisturizer:", total_moisturizer)
plt.figure(figsize=(8, 5))
plt.pie([total_facecream, total_facewash, total_toothpaste, total_bathingsoap, total_shampoo, total_moisturizer], labels = ['facecream', 'facewash', 'toothpaste', 'bathingsoap', 'shampoo', 'moisturizer'], colors=["red", "hotpink", "g", "#93B41B", 'tomato', "#5271C0"], explode=[0, 0, 0, 0, 0.03, 0], startangle=0, shadow = False)
plt.title("Company Sales Data")
plt.show()
Output,
Total Facecream: 34480
Total Facewash: 18515
Total Toothpaste: 69910
Total Bathingsoap: 114010
Total Shampoo: 25410
Total Moisturizer: 18515

29.9. Read Bathing soap facewash of all months and display it using the Subplot
code
Output,
output

29.10. Read all product sales data and show it using the stack plot.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('company_sales_data.csv')
print(df[['month_number', 'facecream', 'facewash', 'toothpaste', 'bathingsoap', 'shampoo', 'moisturizer']])
plt.figure(figsize=(8, 5))
plt.plot(df['month_number'], df['facecream'], marker="s", label="Facecream")
plt.plot(df['month_number'], df['facewash'], marker="s", label="Facewash")
plt.plot(df['month_number'], df['toothpaste'], marker="s", label="Toothpaste")
plt.plot(df['month_number'], df['bathingsoap'], marker="s", label="Bathing Soap")
plt.plot(df['month_number'], df['shampoo'], marker="s", label="Shampoo")
plt.plot(df['month_number'], df['moisturizer'], marker="s", label="Moisturizer", linestyle=":")
plt.xlabel("Month Number")
plt.ylabel("Sold Unit Numbers")
plt.title("Company Sales Data")
plt.grid(linestyle=":", color="g")
plt.xticks(df['month_number'])
plt.legend()
plt.show()
Output,
month_number facecream facewash toothpaste bathingsoap shampoo \
0 1 2500 1500 5200 9200 1200
1 2 2630 1200 5100 6100 2100
2 3 2140 1340 4550 9550 3550
3 4 3400 1130 5870 8870 1870
4 5 3600 1740 4560 7760 1560
5 6 2760 1555 4890 7490 1890
6 7 2980 1120 4780 8980 1780
7 8 3700 1400 5860 9960 2860
8 9 3540 1780 6100 8100 2100
9 10 1990 1890 8300 10300 2300
10 11 2340 2100 7300 13300 2400
11 12 2900 1760 7400 14400 1800
moisturizer
0 1500
1 1200
2 1340
3 1130
4 1740
5 1555
6 1120
7 1400
8 1780
9 1890
10 2100
11 1760
