Practical Assignment #4 - Visualization on Graph (matplotlib.
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 [Link] as plt
import numpy as np
data = [[52,340,890],[64, 480, 560],[78,688,1102],[94,766,889]]
df1 = [Link](data,columns = ['1995', '2005', '2015'], index = ['a', 'b', 'c', 'd'])
print(df1)
data1995 = df1['1995']
data2005 = df1['2005']
x = ['a', 'b','c','d']
[Link](x,data1995, label = 'range1')
[Link](x,data2005,color = 'g', label = 'range2')
[Link]('x axis')
[Link]('y axis')
[Link]('Multi range Line chart')
[Link](loc = 'lower right')
[Link]()
b. A scatter chart from the 2005 and 2015 columns of dataframe df1.
import pandas as pd
import [Link] as plt
import numpy as np
data = [[52,340,890],[64, 480, 560],[78,688,1102],[94,766,889]]
df1 = [Link](data,columns = ['1995', '2005', '2015'], index = ['a', 'b', 'c', 'd'])
print(df1)
data2015 = df1['2015']
data2005 = df1['2005']
x = ['a', 'b','c','d']
[Link](x,data2015, c = 'r', marker = 'o')
[Link](x,data2005, c = 'g', marker = 'o')
[Link]('x axis')
[Link]('y axis')
[Link]('Multi range scatter chart')
[Link](loc = 'lower right')
[Link]()
c. A bar chart of all the three columns of dataframe df1.
import pandas as pd
import [Link] as plt
import numpy as np
data = [[52,340,890],[64, 480, 560],[78,688,1102],[94,766,889]]
df1 = [Link](data,columns = ['1995', '2005', '2015'], index = ['a', 'b', 'c', 'd'])
print(df1)
data1995 = df1['1995']
data2015 = df1['2015']
data2005 = df1['2005']
x = ['a','b','c','d']
x1 = [Link](4)
bar_width = 0.25
[Link](x, data1995, color = 'b', width = 0.25, label = '1995')
[Link](x1+bar_width,data2005, color = 'g', width = 0.25, label = '2005')
[Link](x1+bar_width*2,data2015, color = 'r', width = 0.25, label = '2015')
[Link]('x axis')
[Link]('y axis')
[Link](x1 + bar_width, x)
[Link]('Multi column chart')
[Link](loc = 'upper left')
[Link]()
Note: Give a suitable title, x label, y label, legend etc.
2. Given the following set of data:
78, 72, 69,81,63, 67, 65, 75, 79, 74, 71, 83, 71, 79, 80, 69
a. Create a simple histogram
import [Link] as plt
import numpy as np
[Link]([78, 72, 69,81,63, 67, 65, 75, 79, 74, 71, 83, 71, 79, 80, 69],bins =
[60,65,70,75,80,85,90],edgecolor = 'red')
[Link]('Marks of Computer',fontsize = 15)
[Link]('Frequency',fontsize = 10)
[Link]('Marks',fontsize = 10)
[Link](fontsize = 10,rotation = 30)
[Link](fontsize = 10,rotation = 30)
[Link]()
b. Create a step type histogram
import [Link] as plt
import numpy as np
[Link]([78, 72, 69,81,63, 67, 65, 75, 79, 74, 71, 83, 71, 79, 80, 69],bins =
[60,65,70,75,80,85,90],edgecolor = 'red',histtype = 'step')
[Link]('Marks of Computer',fontsize = 15)
[Link]('Frequency',fontsize = 10)
[Link]('Marks',fontsize = 10)
[Link](fontsize = 10,rotation = 30)
[Link](fontsize = 10,rotation = 30)
[Link]()
c. Note: Give a suitable title, x label, y label, legend etc.
3. From the following ordered set of data:
63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 81
a. Create a horizontal boxplot
import [Link] as plt
data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 81]
box=[Link](data,vert=0,patch_artist=True)
[Link]()
b. Create a vertical boxplot
import [Link] as plt
data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 81]
box=[Link](data,vert=1,patch_artist=True)
[Link]()
c. Show means in the boxplot
import [Link] as plt
data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 81]
box=[Link](data,vert=1,patch_artist=False,notch = True,showmeans = True)
[Link]()
d. Create boxplot without the box
import [Link] as plt
data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 81]
box=[Link](data,vert=1,patch_artist=True, showbox = False)
[Link]()
Note: Give a suitable title, x label, y label, legend etc.
4. Create a Pie chart of the following:
import [Link] as plt
labels = 'Dalmatians', 'Beagles', 'Labradors', 'German Shepherds'
sizes = [6, 10, 15, 9]
[Link](sizes, labels=labels, autopct='%.1f%%')
[Link]('equal')
[Link]()
[Link]()
Now, make some changes in the code.
import [Link] as plt
labels = 'Dalmatians', 'Beagles', 'Labradors', 'German Shepherds'
sizes = [6, 10, 15, 9]
[Link](sizes, labels=labels,explode = (0.1,0,0,0), autopct='%.1f%%')
[Link]('equal')
[Link](loc = “upper left”)
[Link]()
Project work
We are using Pandas and [Link] visualize company sales data.
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
import [Link] as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
profitList = df ['total_profit']
monthList = df ['month_number']
[Link](monthList, profitList, label = 'Month-wise Profit data of last year')
[Link]('Month number')
[Link]('Profit in dollar')
[Link](monthList)
[Link]('Company profit per month')
[Link]([100000, 200000, 300000, 400000, 500000])
[Link]()
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 [Link] as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
profitList = df ['total_profit']
monthList = df ['month_number']
[Link](monthList, profitList, label = 'Profit data of last year', color='r', marker='o',
markerfacecolor='k', linestyle='--', linewidth=3)
[Link]('Month Number')
[Link]('Profit in dollar')
[Link](loc='lower right')
[Link]('Company Sales data of last year')
[Link](monthList)
[Link]([100000, 200000, 300000, 400000, 500000])
[Link]()
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 [Link] 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']
[Link](monthList, faceCremSalesData, label = 'Face cream Sales Data', marker='o', linewidth=3)
[Link](monthList, faceWashSalesData, label = 'Face Wash Sales Data', marker='o', linewidth=3)
[Link](monthList, toothPasteSalesData, label = 'ToothPaste Sales Data', marker='o', linewidth=3)
[Link](monthList, bathingsoapSalesData, label = 'ToothPaste Sales Data', marker='o', linewidth=3)
[Link](monthList, shampooSalesData, label = 'ToothPaste Sales Data', marker='o', linewidth=3)
[Link](monthList, moisturizerSalesData, label = 'ToothPaste Sales Data', marker='o', linewidth=3)
[Link]('Month Number')
[Link]('Sales units in number')
[Link](loc='upper left')
[Link](monthList)
[Link]([1000, 2000, 4000, 6000, 8000, 10000, 12000, 15000, 18000])
[Link]('Sales data')
[Link]()
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 [Link] as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
monthList = df ['month_number']
toothPasteSalesData = df ['toothpaste']
[Link](monthList, toothPasteSalesData, label = 'Tooth paste Sales data')
[Link]('Month Number')
[Link]('Number of units Sold')
[Link](loc='upper left')
[Link](' Tooth paste Sales data')
[Link](monthList)
[Link](True, linewidth= 1, linestyle="--")
[Link]()
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 [Link] as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
monthList = df ['month_number'].tolist()
faceCremSalesData = df ['facecream']
faceWashSalesData = df ['facewash']
[Link]([a-0.25 for a in monthList], faceCremSalesData, width= 0.25, label = 'Face Cream sales
data', align='edge')
[Link]([a+0.25 for a in monthList], faceWashSalesData, width= -0.25, label = 'Face Wash sales
data', align='edge')
[Link]('Month Number')
[Link]('Sales units in number')
[Link](loc='upper left')
[Link](' Sales data')
[Link](monthList)
[Link](True, linewidth= 1, linestyle="--")
[Link]('Facewash and facecream sales data')
[Link]()
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 [Link] as plt
df = pd.read_csv("D:\\Python\\Articles\\matplotlib\\sales_data.csv")
monthList = df ['month_number']
bathingsoapSalesData = df ['bathingsoap']
[Link](monthList, bathingsoapSalesData)
[Link]('Month Number')
[Link]('Sales units in number')
[Link](' Sales data')
[Link](monthList)
[Link](True, linewidth= 1, linestyle="--")
[Link]('bathingsoap sales data')
[Link]('D:\Python\Articles\matplotlib\sales_data_of_bathingsoap.png', dpi=150)
[Link]()
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 [Link] 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]
[Link](profitList, profit_range, label = 'Profit data')
[Link]('profit range in dollar')
[Link]('Actual Profit in dollar')
[Link](loc='upper left')
[Link](profit_range)
[Link]('Profit data')
[Link]()
8. Calculate total sales data for last year for each product and show it using a Pie chart.
Solution
import pandas as pd
import [Link] 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()]
[Link]("equal")
[Link](salesData, labels=labels, autopct='%1.1f%%')
[Link](loc='lower right')
[Link]('Sales data')
[Link]()