Sei sulla pagina 1di 14

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 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()

c. A bar chart of all the three 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']
data2015 = df1['2015']
data2005 = df1['2005']
x = ['a','b','c','d']
x1 = np.arange(4)
bar_width = 0.25
plt.bar(x, data1995, color = 'b', width = 0.25, label = '1995')
plt.bar(x1+bar_width,data2005, color = 'g', width = 0.25, label = '2005')
plt.bar(x1+bar_width*2,data2015, color = 'r', width = 0.25, label = '2015')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.xticks(x1 + bar_width, x)
plt.title('Multi column chart')
plt.legend(loc = 'upper left')
plt.show()

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 matplotlib.pyplot as plt
import numpy as np
plt.hist([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')
plt.title('Marks of Computer',fontsize = 15)
plt.ylabel('Frequency',fontsize = 10)
plt.xlabel('Marks',fontsize = 10)
plt.xticks(fontsize = 10,rotation = 30)
plt.yticks(fontsize = 10,rotation = 30)
plt.show()

b. Create a step type histogram


import matplotlib.pyplot as plt
import numpy as np
plt.hist([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')
plt.title('Marks of Computer',fontsize = 15)
plt.ylabel('Frequency',fontsize = 10)
plt.xlabel('Marks',fontsize = 10)
plt.xticks(fontsize = 10,rotation = 30)
plt.yticks(fontsize = 10,rotation = 30)
plt.show()
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 matplotlib.pyplot as plt
data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 81]
box=plt.boxplot(data,vert=0,patch_artist=True)
plt.show()
b. Create a vertical boxplot
import matplotlib.pyplot as plt
data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 81]
box=plt.boxplot(data,vert=1,patch_artist=True)
plt.show()

c. Show means in the boxplot


import matplotlib.pyplot as plt
data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 81]
box=plt.boxplot(data,vert=1,patch_artist=False,notch = True,showmeans = True)
plt.show()
d. Create boxplot without the box
import matplotlib.pyplot as plt
data = [63, 65, 67, 69, 69, 71, 71, 72, 74, 75, 78, 79, 79, 80, 81, 81]
box=plt.boxplot(data,vert=1,patch_artist=True, showbox = False)
plt.show()
Note: Give a suitable title, x label, y label, legend etc.
4. Create a Pie chart of the following:
import matplotlib.pyplot as plt
labels = 'Dalmatians', 'Beagles', 'Labradors', 'German Shepherds'
sizes = [6, 10, 15, 9]
plt.pie(sizes, labels=labels, autopct='%.1f%%')
plt.axis('equal')
plt.legend()
plt.show()
Now, make some changes in the code.
import matplotlib.pyplot as plt
labels = 'Dalmatians', 'Beagles', 'Labradors', 'German Shepherds'
sizes = [6, 10, 15, 9]
plt.pie(sizes, labels=labels,explode = (0.1,0,0,0), autopct='%.1f%%')
plt.axis('equal')
plt.legend(loc = “upper left”)
plt.show()
Project work
We are using Pandas and matplotlib.pyplot 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 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 = 'Month-wise Profit data of last year')
plt.xlabel('Month number')
plt.ylabel('Profit in dollar')
plt.xticks(monthList)
plt.title('Company profit per month')
plt.yticks([100000, 200000, 300000, 400000, 500000])
plt.show()

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()

Potrebbero piacerti anche