Code untuk belajar visualisasi data dengan bahasa python

Saturday, July 18th, 2020

Data silahkan download di sini ya:
https://www.kaggle.com/samipjshah/wa-sales-products-201214

tonton videonya di link ini: https://youtu.be/g7LNO3Gcgko

Berikut code untuk import datanya dulu

names = ['Country','Order','Retailer','Product_line','Item','Product','Year','Quarter','Revenue','Sold','Cost','margin','Profit']

import dulu pandas dataframe ya

import pandas as pd
sales = pd.read_csv('WA_Sales_Products_2012-14_Updated.csv', sep=',', names=names, low_memory=False)

ambil revenue tiap quarter nya

q1a = sales[sales.Quarter == 'Q1 2012'].Revenue
q2a = sales[sales.Quarter == 'Q2 2012'].Revenue
q3a = sales[sales.Quarter == 'Q3 2012'].Revenue
q1b = sales[sales.Quarter == 'Q1 2013'].Revenue
q2b = sales[sales.Quarter == 'Q2 2013'].Revenue
q3b = sales[sales.Quarter == 'Q3 2013'].Revenue


q1c = sales[sales.Quarter == 'Q1 2014'].Revenue
q2c = sales[sales.Quarter == 'Q2 2014'].Revenue
q3c = sales[sales.Quarter == 'Q3 2014'].Revenue

kemudian jumlahkan dengan fungsi sum

sumq1a = q1a.sum()
sumq2a = q2a.sum()
sumq3a = q3a.sum()
sumq1b = q1b.sum()
sumq2b = q2b.sum()
sumq3b = q3b.sum()
sumq1c = q1c.sum()
sumq2c = q2c.sum()
sumq3c = q3c.sum()

bikin variable baru untuk menyimpan array yang berisi value dan nama quaternya

Qarray = [sumq1a, sumq2a, sumq3a, sumq1b, sumq2b, sumq3b, sumq1c, sumq2c, sumq3c]

QTimearray = ['2012 q1', '2012 q2', '2012 q3', '2013 q1', '2013 q2', '2013 q3', '2014 q1', '2014 q2', '2014 q3']

import dulu matplotlib

import matplotlib.pyplot as plt
import seaborn as sns

jadikan satu fungsi

def salesGraf():
	sumq1a = sales[sales.Quarter == 'Q1 2012'].Revenue.sum()
	sumq2a = sales[sales.Quarter == 'Q2 2012'].Revenue.sum()
	sumq3a = sales[sales.Quarter == 'Q3 2012'].Revenue.sum()
	sumq1b = sales[sales.Quarter == 'Q1 2013'].Revenue.sum()
	sumq2b = sales[sales.Quarter == 'Q2 2013'].Revenue.sum()
	sumq3b = sales[sales.Quarter == 'Q3 2013'].Revenue.sum()
	sumq1c = sales[sales.Quarter == 'Q1 2014'].Revenue.sum()
	sumq2c = sales[sales.Quarter == 'Q2 2014'].Revenue.sum()
	sumq3c = sales[sales.Quarter == 'Q3 2014'].Revenue.sum()
	Qarray = [sumq1a, sumq2a, sumq3a, sumq1b, sumq2b, sumq3b, sumq1c, sumq2c, sumq3c]	
	QTimearray = ['2012 q1', '2012 q2', '2012 q3', '2013 q1', '2013 q2', '2013 q3', '2014 q1', '2014 q2', '2014 q3']
	sns.set()
	pal = sns.color_palette("Set2", 2)
	plt.figure(figsize=(8, 8))
	plt.plot(QTimearray, Qarray, c=pal[0], label='Grafik keuntungan', alpha=0.5, linewidth=5)
	plt.legend(loc='best', fontsize=20)
	plt.xticks(fontsize=12);
	plt.yticks(fontsize=16);
	plt.xlabel('quarter', fontsize=30);
	plt.ylabel('revenue', fontsize=30);

Berikut fungsi untuk memunculkan produk

def prodGraf():
	produkArr = []
	for i in xrange(len(sales.Product.unique())):
		produkArr += [sales[sales.Product == sales.Product.unique()[i]].Revenue.sum()]
	pal = sns.color_palette("Set2", 2)
	plt.figure(figsize=(14, 8))
	plt.plot(produkArr, c=pal[0], label='Grafik keuntungan Produk', alpha=0.5, linewidth=5)
	plt.legend(loc='best', fontsize=20)
	plt.xticks(fontsize=12);
	plt.yticks(fontsize=16);
	plt.xlabel('produk', fontsize=30);
	plt.ylabel('revenue', fontsize=30);

Bikin scatter plot untuk order type

def orderTypeRevenue():	
	orderType = []
	for i in xrange(len(sales.Order_method_type.unique())):
		orderType += [sales[sales.Order_method_type == sales.Order_method_type.unique()[i]].Revenue.sum()]
	plt.figure(figsize=(14, 8))
	z = np.random.randint(-2,3,7)
	cmap=plt.cm.rainbow
	norm = matplotlib.colors.BoundaryNorm(np.arange(-2.5,3,1), cmap.N)
	plt.scatter(sales.Order_method_type.unique(), orderType,c=z,cmap=cmap,norm=norm,s=100,edgecolor='none')
	plt.colorbar(ticks=np.linspace(-2,2,5))
	plt.show()

Leave a Reply

Your email address will not be published. Required fields are marked *

post_id=417