-
Notifications
You must be signed in to change notification settings - Fork 275
/
tutorial26_plotting_using_pyplots_python.py
191 lines (145 loc) · 4.51 KB
/
tutorial26_plotting_using_pyplots_python.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#Video Playlist: https://www.youtube.com/playlist?list=PLHae9ggVvqPgyRQQOtENr6hK0m1UquGaG
"""
matplotlib.pyplot contains various functions for plotting, including images.
Each pyplot function makes a change to the plot (e.g. add titles, labels, plots)
"""
#PLot using lists
from matplotlib import pyplot as plt
x = [1,2,3,4,5]
y = [1,4,9,16,25]
plt.plot(x,y) #plot takes any number of arguments
#Also understands numpy arrays
import numpy as np
a = np.array(x)
b = np.array(y)
plt.plot(a,b)
#Images can also be plotted (covered in viewing images tutorial)
#Images are numpy arrays
import cv2
gray_img = cv2.imread('images/sandstone.tif', 0)
plt.imshow(gray_img, cmap="gray")
plt.hist(gray_img.flat, bins=100, range=(0,150))
#Formatting
from matplotlib import pyplot as plt
import numpy as np
a = np.array([1,2,3,4,5])
b = np.array([1,4,9,16,25])
plt.plot(a,b)
plt.plot(a, b, 'bo') #Blue dots. Also try: 'r--' 'g^' 'bs'
plt.axis([0, 6, 0, 50]) #Define range for x and y axes [xmin, xmax, ymin, ymax]
#Various types of plots
from matplotlib import pyplot as plt
wells = ['well1', 'well2', 'well3', 'well4', 'well5']
cells = [80, 62, 88, 110, 90]
plt.bar(wells, cells)
plt.scatter(wells, cells)
plt.plot(wells, cells)
#If you run all lines at once you'll see that all plots share same axes.
#Multiple plots topic is covered below.
#################################
#Defining line properties
from matplotlib import pyplot as plt
import numpy as np
a = np.array([1,2,3,4,5])
b = np.array([1,4,9,16,25])
plt.plot(a, b, linewidth=5.0)
#use setp() to define multiple parameters
fig = plt.plot(a, b, linewidth=5.0)
plt.setp(fig, color='r', linewidth=4.0)
#look at the documentation for more infor on setp()
# https://matplotlib.org/api/_as_gen/matplotlib.pyplot.setp.html#matplotlib.pyplot.setp
#####################################
from matplotlib import pyplot as plt
#Adding labels and annotations
wells = [1,2,3,4,5]
cells = [80, 62, 88, 110, 90]
plt.figure(figsize=(8, 8))
plt.bar(wells, cells)
plt.xlabel('Well #', fontsize=18, color='red')
plt.ylabel('# dead cells')
plt.title('Dead cells in each well')
plt.axis([1, 6, 60, 120]) #xmin, xmax, ymin, ymax
plt.grid(True)
plt.show()
############################
#Log scale
from matplotlib import pyplot as plt
x = [1,2,3,4,5]
y = [10, 125, 1350, 11250, 100500]
plt.figure(figsize=(12, 6))
# linear
plt.subplot(121)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)
#Log
plt.subplot(122)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')
plt.grid(True)
################################
#Use subplots to arrange multiple plots
from matplotlib import pyplot as plt
wells = ['well1', 'well2', 'well3', 'well4', 'well5']
cells = [80, 62, 88, 110, 90]
plt.bar(wells, cells)
plt.scatter(wells, cells)
plt.plot(wells, cells)
#Multiple plots using subplot
#Define a figure size first
#PLotting horizontally
plt.figure(figsize=(16, 6))
#Let us plot in 1 row and 3 columns (13x) x is for the position
plt.subplot(131)
plt.bar(wells, cells)
plt.subplot(132)
plt.scatter(wells, cells)
plt.subplot(133)
plt.plot(wells, cells)
plt.suptitle('Multiple Plots')
plt.show()
#PLotting vertically
plt.figure(figsize=(6, 16))
#Let us plot in 1 column and 2 rows (31x) x is for the position
plt.subplot(311)
plt.bar(wells, cells)
plt.subplot(312)
plt.scatter(wells, cells)
plt.subplot(313)
plt.plot(wells, cells)
plt.suptitle('Multiple Plots')
plt.show()
#Plotting as a grid
#PLotting vertically
plt.figure(figsize=(12, 12))
#Let us plot in 2 columns and 2 rows (22x) x is for the position
plt.subplot(221)
plt.bar(wells, cells)
plt.subplot(222)
plt.scatter(wells, cells)
plt.subplot(223)
plt.plot(wells, cells)
plt.suptitle('Multiple Plots')
plt.show()
###################################################
#Another way to define multiple plots
from matplotlib import pyplot as plt
wells = ['well1', 'well2', 'well3', 'well4', 'well5']
cells = [80, 62, 88, 110, 90]
#Initialize the plot and sublots
# Initialize the plot
fig = plt.figure(figsize=(16,6))
ax1 = fig.add_subplot(131)
ax1.set(title='vertical bar', xlabel='Well #', ylabel='# cells')
ax2 = fig.add_subplot(132)
ax1.set(title='horizontal bar', xlabel='Well #', ylabel='# cells')
ax3 = fig.add_subplot(133)
# Plot the data
ax1.bar(wells, cells)
ax2.barh(wells, cells)
ax3.plot(wells, cells)
plt.savefig("images/my_plot.jpg") #Save plot
# Show the plot
plt.show()