-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoolbox.py
207 lines (178 loc) · 8.56 KB
/
toolbox.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
Name: Hangliang Ren
This program provides a series of plotting methods, both 2d and 3d plotting.
Through these methods, you can easily make plots relevant to manifold learning,
from original shape to subplots of all manifold learning results.
"""
import itertools
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker
def plot_3d_shape_single(datapoints, colors, title=None, figsize=(10, 10), marker_size=20, rotate=False,
set_division=False, save_path=False, show_plot=True, hide_axes=True):
"""
parameters
"datapoints": Dataset used to draw the shape.
"colors": Dataset used to color the shape; can be None. For example, colors=originData[:, 0] means color by x.
"title": Name of the shape; None as default value.
"figsize": Size of plot, 10 * 10 as default value.
"marker_size": Size of marker, 20 as default value.
"rotate": A tuple (azim, elev) to adjust figure, azim is
the degree to rotate by z axis, elev is the degree to rotate
by y axis; can be None.
"set_division": Choose whether set division in each axis scale;
False as default value; if wanna set division, input real value.
"save_path": The path for saving the plot; False as default value;
if wanna to save plot, input absolute path for saving.
"show_plot": Whether you want to display the generated plot;
True as default value.
"hide_axes": Whether you want to hide axes of generated plot;
True as default value.
post
Display and save the plotted shape, generated by input dataset.
"""
fig = plt.figure(figsize=figsize) # set figure size
ax = fig.add_subplot(projection='3d')
if hide_axes:
ax.set_axis_off()
ax.scatter(datapoints.T[0], datapoints.T[1], datapoints.T[2], s=marker_size, c=colors)
plt.title(title, fontsize=20)
# rotate figure
if rotate:
ax.view_init(azim=rotate[0], elev=rotate[1])
if set_division != False:
# set division in each axis scale
ax.xaxis.set_major_locator(ticker.MultipleLocator(set_division))
ax.yaxis.set_major_locator(ticker.MultipleLocator(set_division))
ax.zaxis.set_major_locator(ticker.MultipleLocator(set_division))
if save_path != False:
plt.savefig(save_path)
if show_plot:
plt.show()
def plot_3d_shape_all(datapoints, colors, title=None, figsize=(10, 20), marker_size=1,
set_division=False, save_path=False, show_plot=True, hide_axes=True):
"""
parameters
"datapoints": Dataset used for plotting.
"colors": Dataset used to color the shape; can be None. For example, colors=originData[:, 0] means color by x.
"title": Name of the shape; None as default value.
"figsize": Size of plot, 10 * 20 as default value.
"marker_size": Size of marker, 1 as default value.
"set_division": Choose whether set division in each axis scale;
False as default value; if wanna set division, input real value.
"save_path": The path for saving the plot; False as default value;
if wanna to save plot, input absolute path for saving.
"show_plot": Whether you want to display the generated plot;
True as default value.
"hide_axes": Whether you want to hide axes of generated plot;
True as default value.
post
Plot and save all 3d shapes; each shape is plotted by combining first and
other two columns in "datapoints".
"""
indices = [i for i in range(1, datapoints.shape[-1])]
combinations = list(itertools.combinations(indices, 2))
num_of_plots = len(combinations)
fig = plt.figure(figsize=figsize)
i = 1
for combination in combinations:
sub_data = datapoints[:, [0,combination[0],combination[1]]]
x, y, z = sub_data.T
ax = fig.add_subplot(math.ceil(num_of_plots/2), 2, i, projection='3d')
if hide_axes:
ax.set_axis_off()
ax.scatter(x, y, z, s=marker_size, c=colors)
ax.set_title("v0, v" + str(combination[0]) + ", v" + str(combination[1]))
if set_division != False:
# set division in each axis scale
ax.xaxis.set_major_locator(ticker.MultipleLocator(set_division))
ax.yaxis.set_major_locator(ticker.MultipleLocator(set_division))
ax.zaxis.set_major_locator(ticker.MultipleLocator(set_division))
i += 1
plt.suptitle(title, fontsize=20)
if save_path != False:
plt.savefig(save_path)
if show_plot:
plt.show()
def plot_2d_shape_single(datapoints, colors, title=None, figsize=(10, 10), marker_size=20, set_division=False, save_path=False,
show_plot=True, hide_axes=True):
"""
parameters
"datapoints": Dataset used to draw the shape, a matrix with 2 columns,
returned from sklearn manifold learning methods.
"colors": Dataset used to color the shape; can be None.
"title": Name of the shape; None as default value.
"figsize": Size of plot, 10 * 10 as default value.
"marker_size": Size of marker, 20 as default value.
"set_division": Choose whether set division in each axis scale;
False as default value; if wanna set division, input real value.
"save_path": The path for saving the plot; False as default value;
if wanna to save plot, input absolute path for saving.
"show_plot": Whether you want to display the generated plot;
True as default value.
"hide_axes": Whether you want to hide axes of generated plot;
True as default value.
post
Plot and save the 2d shape, through data in "datapoints".
"""
x, y = datapoints.T
plt.figure(figsize=figsize)
plt.scatter(x, y, c=colors, s=marker_size, alpha=0.8)
plt.title(title, fontsize=20)
if set_division != False:
# set division in each axis scale
ax.xaxis.set_major_locator(ticker.MultipleLocator(set_division))
ax.yaxis.set_major_locator(ticker.MultipleLocator(set_division))
if hide_axes:
ax = plt.gca()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.set_frame_on(False)
if save_path != False:
plt.savefig(save_path)
if show_plot:
plt.show()
def plot_2d_shape_all(datapoints, colors, title=None, figsize=(20, 20), marker_size=2, set_division=False, save_path=False,
show_plot=True, hide_axes=True):
"""
parameters
"datapoints": Returned matrix from sklearn manifold learning methods, for plotting.
"colors": Dataset used to color each shape; can be None.
"title": Name of the shape; None as default value.
"figsize": Size of plot, 20 * 20 as default value.
"marker_size": Size of marker, 2 as default value.
"set_division": Choose whether set division in each axis scale;
False as default value; if wanna set division, input real value.
"save_path": The path for saving the plot; False as default value;
if wanna to save plot, input absolute path for saving.
"show_plot": Whether you want to display the generated plot;
True as default value.
"hide_axes": Whether you want to hide axes of generated plot;
True as default value.
post
Plot and save all 2d shapes; each shape is plotted by combining first and
another column in "datapoints".
"""
num_of_plots = datapoints.shape[-1] - 1
fig = plt.figure(figsize=figsize)
for i in range(1, num_of_plots+1):
sub_data = datapoints[:, [0,i]]
x, y = sub_data.T
ax = fig.add_subplot(math.ceil(num_of_plots/2), 2, i)
ax.scatter(x, y, s=marker_size, c=colors)
ax.set_title("v0, v"+str(i))
if set_division != False:
# set division in each axis scale
ax.xaxis.set_major_locator(ticker.MultipleLocator(set_division))
ax.yaxis.set_major_locator(ticker.MultipleLocator(set_division))
if hide_axes:
ax = plt.gca()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.set_frame_on(False)
plt.suptitle(title, fontsize=20)
if save_path != False:
plt.savefig(save_path)
if show_plot:
plt.show()