Anda di halaman 1dari 6

3/31/2020 DataVis2 - Jupyter Notebook

Data dengan Kategori


In [1]:  1 # import pandas and matplotlib
2 import pandas as pd
3 import matplotlib.pyplot as plt

Untuk data yang terdiri dari beberapa kategori, dalam contoh ini adalah ID pegawai, Jenis kelamin,
Umur, ID Penjual, Index massa tubuh, dan Gaji dalam dolar. Grafik berikut menampilkan seluruh
data dalam tiap kategori yang berupa data numerik

In [2]:  1 # create 2D array of table given above


2 data = [['E001', 'M', 34, 123, 'Normal', 350],
3 ['E002', 'F', 40, 114, 'Overweight', 450],
4 ['E003', 'F', 37, 135, 'Obesity', 169],
5 ['E004', 'M', 30, 139, 'Underweight', 189],
6 ['E005', 'F', 44, 117, 'Underweight', 183],
7 ['E006', 'M', 36, 121, 'Normal', 80],
8 ['E007', 'M', 32, 133, 'Obesity', 166],
9 ['E008', 'F', 26, 140, 'Normal', 120],
10 ['E009', 'M', 32, 133, 'Normal', 75],
11 ['E010', 'M', 36, 133, 'Underweight', 40] ]
12
13 # dataframe created with
14 # the above data array
15 df = pd.DataFrame(data, columns = ['EMPID', 'Gender',
16 'Age', 'Sales',
17 'BMI', 'Income'] )
18
19 # create histogram for numeric data
20 df.hist()
21
22 # show plot
23 plt.show()

localhost:8888/notebooks/ProbStat/DataVis2.ipynb 1/6
3/31/2020 DataVis2 - Jupyter Notebook

In [38]:  1 # Dataframe of previous code is used here


2
3 # Plot the bar chart for numeric values
4 # a comparison will be shown between
5 # all 3 age, income, sales
6 df.plot.bar()
7
8 # plot between 2 attributes
9 x_axis = list(df['EMPID'])
10 lst = [i for i in range(len(x_axis))]
11 plt.bar(df['Age'], df['Sales'])
12 plt.xticks(lst,x_axis)
13 #plt.xlabel("Age")
14 plt.xlabel("Employee ID")
15 plt.ylabel("Values")
16 plt.show()

Grafik Box plot


adalah grafik yang menunjukkan nilai minimum, quartil pertama, median, kuartil ketiga, dan nilai
maximum. Disebut box plot karena grafiknya tampak seperti sebuah persegi empat dengan garis-
garis yang membatasi bagian atas dan bawah.

localhost:8888/notebooks/ProbStat/DataVis2.ipynb 2/6
3/31/2020 DataVis2 - Jupyter Notebook

In [6]:  1 # For each numeric attribute of dataframe


2 df.plot.box()
3 plt.show()
4
5 # individual attribute box plot
6 plt.boxplot(df['Income'])
7 plt.show()

localhost:8888/notebooks/ProbStat/DataVis2.ipynb 3/6
3/31/2020 DataVis2 - Jupyter Notebook

Grafik Pie
Grafik pie menampilkan nilai per kategori di mana totalnya adalah 100%.

localhost:8888/notebooks/ProbStat/DataVis2.ipynb 4/6
3/31/2020 DataVis2 - Jupyter Notebook

In [7]:  1 plt.pie(df['Age'], labels = {"A", "B", "C",


2 "D", "E", "F",
3 "G", "H", "I", "J"},
4
5 autopct ='% 1.1f %%', shadow = True)
6 plt.show()
7
8 plt.pie(df['Income'], labels = {"A", "B", "C",
9 "D", "E", "F",
10 "G", "H", "I", "J"},
11
12 autopct ='% 1.1f %%', shadow = True)
13 plt.show()
14
15 plt.pie(df['Sales'], labels = {"A", "B", "C",
16 "D", "E", "F",
17 "G", "H", "I", "J"},
18 autopct ='% 1.1f %%', shadow = True)
19 plt.show()

localhost:8888/notebooks/ProbStat/DataVis2.ipynb 5/6
3/31/2020 DataVis2 - Jupyter Notebook

In [ ]:  1

localhost:8888/notebooks/ProbStat/DataVis2.ipynb 6/6

Anda mungkin juga menyukai