Assignment 4 On Visualization On Graph With Solution
Assignment 4 On Visualization On Graph With Solution
pyplot)
1. Given a data frame df1 as shown below:
1995 2005 2015
a 52 340 890
b 64 480 560
c 78 688 1102
d 94 766 889
Write code to create
a. A line chart from the 1995 and 2005 columns of dataframe df1.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = [[52,340,890],[64, 480, 560],[78,688,1102],[94,766,889]]
df1 = pd.DataFrame(data,columns = ['1995', '2005', '2015'], index = ['a', 'b', 'c', 'd'])
print(df1)
data1995 = df1['1995']
data2005 = df1['2005']
x = ['a', 'b','c','d']
plt.plot(x,data1995, label = 'range1')
plt.plot(x,data2005,color = 'g', label = 'range2')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Multi range Line chart')
plt.legend(loc = 'lower right')
plt.show()
b. A scatter chart from the 2005 and 2015 columns of dataframe df1.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = [[52,340,890],[64, 480, 560],[78,688,1102],[94,766,889]]
df1 = pd.DataFrame(data,columns = ['1995', '2005', '2015'], index = ['a', 'b', 'c', 'd'])
print(df1)
data2015 = df1['2015']
data2005 = df1['2005']
x = ['a', 'b','c','d']
plt.scatter(x,data2015, c = 'r', marker = 'o')
plt.scatter(x,data2005, c = 'g', marker = 'o')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Multi range scatter chart')
plt.legend(loc = 'lower right')
plt.show()
1. Read Total profit of all months and show it using a line plot. The line plot must include the
following properties:
X label name – Month Number
Y label name – Total profit
Title – Company profit per month
Xticks – month number
Yticks – 100000, 200000, 300000, 400000, 500000
Solution
2. Get Total profit of all months and show line plot with the following style properties:
Line Style dotted and Line color should be red.
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
Solution
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
profitList = df ['total_profit']
monthList = df ['month_number']
plt.plot(monthList, profitList, label = 'Profit data of last year', color='r', marker='o',
markerfacecolor='k', linestyle='--', linewidth=3)
plt.xlabel('Month Number')
plt.ylabel('Profit in dollar')
plt.legend(loc='lower right')
plt.title('Company Sales data of last year')
plt.xticks(monthList)
plt.yticks([100000, 200000, 300000, 400000, 500000])
plt.show()
3. Read all product sales data and show it using a multiline plot. The graph should look like this:
Solution
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
monthList = df ['month_number']
faceCremSalesData = df ['facecream']
faceWashSalesData = df ['facewash']
toothPasteSalesData = df ['toothpaste']
bathingsoapSalesData = df ['bathingsoap']
shampooSalesData = df ['shampoo']
moisturizerSalesData = df ['moisturizer']
plt.plot(monthList, faceCremSalesData, label = 'Face cream Sales Data', marker='o', linewidth=3)
plt.plot(monthList, faceWashSalesData, label = 'Face Wash Sales Data', marker='o', linewidth=3)
plt.plot(monthList, toothPasteSalesData, label = 'ToothPaste Sales Data', marker='o', linewidth=3)
plt.plot(monthList, bathingsoapSalesData, label = 'ToothPaste Sales Data', marker='o', linewidth=3)
plt.plot(monthList, shampooSalesData, label = 'ToothPaste Sales Data', marker='o', linewidth=3)
plt.plot(monthList, moisturizerSalesData, label = 'ToothPaste Sales Data', marker='o', linewidth=3)
plt.xlabel('Month Number')
plt.ylabel('Sales units in number')
plt.legend(loc='upper left')
plt.xticks(monthList)
plt.yticks([1000, 2000, 4000, 6000, 8000, 10000, 12000, 15000, 18000])
plt.title('Sales data')
plt.show()
4. Read toothpaste sales data of each month and show it using a scatter plot. Add a grid in the plot.
The gridline style should be “-“.
Solution
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
monthList = df ['month_number']
toothPasteSalesData = df ['toothpaste']
plt.scatter(monthList, toothPasteSalesData, label = 'Tooth paste Sales data')
plt.xlabel('Month Number')
plt.ylabel('Number of units Sold')
plt.legend(loc='upper left')
plt.title(' Tooth paste Sales data')
plt.xticks(monthList)
plt.grid(True, linewidth= 1, linestyle="--")
plt.show()
5. Read face cream and facewash product sales data and show it using the bar chart. Bar chart should
display the number of units sold per month for each product. Add a separate bar for each product
in the same chart.
Solution
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
monthList = df ['month_number'].tolist()
faceCremSalesData = df ['facecream']
faceWashSalesData = df ['facewash']
plt.bar([a-0.25 for a in monthList], faceCremSalesData, width= 0.25, label = 'Face Cream sales
data', align='edge')
plt.bar([a+0.25 for a in monthList], faceWashSalesData, width= -0.25, label = 'Face Wash sales
data', align='edge')
plt.xlabel('Month Number')
plt.ylabel('Sales units in number')
plt.legend(loc='upper left')
plt.title(' Sales data')
plt.xticks(monthList)
plt.grid(True, linewidth= 1, linestyle="--")
plt.title('Facewash and facecream sales data')
plt.show()
6. Read sales data of bathing soap of all months and show it using a bar chart. Save this plot to your
hard disk.
Solution
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
monthList = df ['month_number']
bathingsoapSalesData = df ['bathingsoap']
plt.bar(monthList, bathingsoapSalesData)
plt.xlabel('Month Number')
plt.ylabel('Sales units in number')
plt.title(' Sales data')
plt.xticks(monthList)
plt.grid(True, linewidth= 1, linestyle="--")
plt.title('bathingsoap sales data')
plt.savefig('D:\Python\Articles\matplotlib\sales_data_of_bathingsoap.png', dpi=150)
plt.show()
7. Read the total profit of each month and show it using the histogram to see most common profit
ranges.
Solution
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
profitList = df ['total_profit']
labels = ['low', 'average', 'Good', 'Best']
profit_range = [150000, 175000, 200000, 225000, 250000, 300000, 350000]
plt.hist(profitList, profit_range, label = 'Profit data')
plt.xlabel('profit range in dollar')
plt.ylabel('Actual Profit in dollar')
plt.legend(loc='upper left')
plt.xticks(profit_range)
plt.title('Profit data')
plt.show()
8. Calculate total sales data for last year for each product and show it using a Pie chart.
Solution
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
monthList = df ['month_number']
labels = ['FaceCream', 'FaseWash', 'ToothPaste', 'Bathing soap', 'Shampoo', 'Moisturizer']
salesData = [df ['facecream'].sum(), df ['facewash'].sum(), df ['toothpaste'].sum(),
df ['bathingsoap'].sum(), df ['shampoo'].sum(), df ['moisturizer'].sum()]
plt.axis("equal")
plt.pie(salesData, labels=labels, autopct='%1.1f%%')
plt.legend(loc='lower right')
plt.title('Sales data')
plt.show()