-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matplotlib.py
142 lines (139 loc) · 3.46 KB
/
Matplotlib.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Line Plot
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1, 11)
print(x)
print()
y = 2 * x
print(y)
print()
plt.plot(x, y)
plt.show()
print()
# Line Plot - 2 (adding titles and labels)
plt.plot(x, y)
plt.title("Line Plot")
plt.xlabel("x-label")
plt.ylabel("y-label")
plt.show()
print()
# Line Plot - changing line aesthetics
plt.plot(x, y, color='red', linestyle=':', linewidth=2)
plt.show()
print()
# Line Plot - two lines
x = np.arange(1, 11)
y1 = 2 * x
y2 = 3 * x
plt.plot(x, y1, color='red', linestyle=':', linewidth=2)
plt.plot(x, y2, color='green', linestyle='-', linewidth=3)
plt.title("Line Plot")
plt.xlabel("x-label")
plt.ylabel("y-label")
plt.grid(True)
plt.show()
# Line Plot - adding subplots
x = np.arange(1, 11)
y1 = 2 * x
y2 = 3 * x
plt.subplot(1, 2, 1)
plt.plot(x, y1, color='red', linestyle=':', linewidth=2)
plt.subplot(1, 2, 2)
plt.plot(x, y2, color='green', linestyle=':', linewidth=3)
plt.show()
# Bar plot
student = {"Bob": 87, "Matt": 56, "Sam": 27}
names = list(student.keys())
values = list(student.values())
plt.bar(names, values)
plt.show()
# bar plot adding labels and grid
plt.bar(names, values)
plt.title("Marks of Students")
plt.xlabel("x-label")
plt.ylabel("y-label")
plt.grid(True)
plt.show()
# horizontal bar plot
plt.barh(names, values, color='orange')
plt.title("Marks of Students")
plt.xlabel("Marks")
plt.ylabel("Names")
plt.grid(True)
plt.show()
# Scalar plot
x = [10, 20, 30, 40, 50, 60, 70, 80, 90]
a = [8, 1, 7, 2, 0, 3, 5, 3, 4]
plt.scatter(x, a)
plt.show()
# Scalar plot - 2
x = [10, 20, 30, 40, 50, 60, 70, 80, 90]
a = [8, 1, 7, 2, 0, 3, 5, 3, 4]
plt.scatter(x, a, marker="*", c="g", s=100)
plt.show()
# Scalar plot - 3
x = [10, 20, 30, 40, 50, 60, 70, 80, 90]
a = [8, 1, 7, 2, 0, 3, 5, 3, 4]
b = [4, 3, 5, 3, 0, 2, 7, 1, 8]
plt.scatter(x, a, marker="*", c="red", s=100)
plt.scatter(x, b, marker=".", c="yellow", s=150)
plt.show()
# Scalar plot - 4
x = [10, 20, 30, 40, 50, 60, 70, 80, 90]
a = [8, 1, 7, 2, 0, 3, 5, 3, 4]
b = [4, 3, 5, 3, 0, 2, 7, 1, 8]
plt.subplot(1, 2, 1)
plt.scatter(x, a, marker="*", c="red", s=100)
plt.subplot(1, 2, 2)
plt.scatter(x, b, marker=".", c="yellow", s=150)
plt.show()
# Histogram
data = [1, 3, 3, 3, 3, 3, 9, 9, 5, 4, 4, 8, 8, 8, 6, 7]
plt.hist(data)
plt.show()
# Histogram - 2
plt.hist(data, color="g", bins=4)
plt.show()
# Histogram -3 dataset
import pandas as pd
iris = pd.read_csv('Book1.csv')
iris.head()
plt.hist(iris['Units'], bins=30, color="r")
plt.show()
# box plot
one = [1, 2, 3, 4, 5, 6, 7, 8, 9]
two = [1, 2, 3, 4, 5, 4, 3, 2, 1]
three = [6, 7, 8, 9, 8, 7, 6, 5, 4]
data = list([one, two, three])
plt.boxplot(data)
plt.show()
# Violin plot
one = [1, 2, 3, 4, 5, 6, 7, 8, 9]
two = [1, 2, 3, 4, 5, 4, 3, 2, 1]
three = [6, 7, 8, 9, 8, 7, 6, 5, 4]
data = list([one, two, three])
plt.violinplot(data, showmedians=True)
plt.show()
# pie chart
fruit = ['Apple', 'Orange', 'Mango', 'Guava']
quantity = [67, 34, 100, 29]
plt.pie(quantity, labels=fruit)
plt.show()
# pie chart - 2
fruit = ['Apple', 'Orange', 'Mango', 'Guava']
quantity = [53, 43, 12, 97]
plt.pie(quantity, labels=fruit, autopct='%0.1f%%', colors=['yellow', 'grey', 'blue', 'black'])
plt.show()
# doughnut-chart
fruit = ['Apple', 'Orange', 'Mango', 'Guava']
quantity = [53, 43, 12, 97]
plt.pie(quantity, labels=fruit, radius=2)
plt.pie([1], colors=['w'], radius=1)
plt.show()
# seaborn line plot
import seaborn as sns
from matplotlib import pyplot as plt
fmri = sns.load_dataset("fmri")
fmri.head()
sns.lineplot(x="time_point", y="signal", data=fmri)
plt.show()