-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.3.Data_Visualization__Matplotlib-Seaborn.py
More file actions
118 lines (95 loc) · 2.47 KB
/
4.3.Data_Visualization__Matplotlib-Seaborn.py
File metadata and controls
118 lines (95 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# MATPLOTLIB & SEABORN
# Categorical variable --> M: bar chart, S: countplot
# Numerical variable --> Histogram and boxplot
# Categorical Variable Visualization
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 500)
df = sns.load_dataset("titanic")
df.head()
df["sex"].value_counts().plot(kind="bar") # !!! pip install matplotlib or pip install --upgrade matplotlib
plt.show()
# Numerical Variable Visualization
plt.hist(df["age"])
plt.show()
plt.boxplot(df["fare"])
plt.show()
# MATPLOTLIB
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 500)
# plot
x = np.array([1, 8])
y = np.array([0, 150])
plt.plot(x, y)
plt.show()
plt.plot(x, y, 'o')
plt.show()
x = np.array([2, 4, 6, 8, 10])
y = np.array([1, 3, 5, 7, 9])
plt.plot(x, y)
plt.show()
# marker : markers['o', '*', '.', ',', 'x', 'X', '+', 'P', 's', 'D', 'd', 'p', 'H', 'h']
x = np.array([13, 28, 11, 100])
plt.plot(x, marker='o')
plt.show()
x = np.array([13, 28, 11, 100])
plt.plot(x, marker='*')
plt.show()
# line
y = np.array([13, 28, 11, 100])
plt.plot(y, linestyle="dashed")
plt.show()
y = np.array([13, 28, 11, 100])
plt.plot(y, linestyle="dotted")
plt.show()
y = np.array([13, 28, 11, 100])
plt.plot(y, linestyle="dashdot", color="r")
plt.show()
# multiple lines
x = np.array([2, 4, 6, 8, 10])
y = np.array([1, 3, 5, 7, 9])
plt.plot(x)
plt.plot(y)
plt.show()
# labels
x = np.array([2, 4, 6, 8, 10])
y = np.array([1, 3, 5, 7, 9])
plt.plot(x, y)
plt.title("Main Header")
plt.xlabel("X axis: Name")
plt.ylabel("Y axis: Name")
plt.grid() # to increase readability
plt.show()
# subplots
# plot 1
x = np.array([20, 45, 60, 85, 105])
y = np.array([10, 30, 55, 70, 95])
plt.subplot(1, 2, 1) # create 1st plot 1 row 2 column graph
plt.title("1")
plt.plot(x, y)
# plot 2
x = np.array([25, 55, 65, 95, 115])
y = np.array([250, 230, 210, 205, 200])
plt.subplot(1, 2, 2) # create 2nd plot 1 row 2 column graph
plt.title("2")
plt.plot(x, y)
plt.show()
# SEABORN
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
df = sns.load_dataset("tips")
df["sex"].value_counts()
sns.countplot(x=df["sex"], data=df) # Categorical Vis.
plt.show()
sns.boxplot(df["total_bill"]) # Numerical Vis.
plt.show()
df["total_bill"].hist() # Numerical Vis.
plt.show()