-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarchart.py
More file actions
385 lines (341 loc) · 12 KB
/
barchart.py
File metadata and controls
385 lines (341 loc) · 12 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from __future__ import annotations
import itertools
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
def generate_mesh(x_min, x_max, y_min, y_max, z_min, z_max, color_value, flat_shading, hover_info, opacity: float = 1):
return go.Mesh3d(
x=[
x_min, x_min, x_max, x_max,
x_min, x_min, x_max, x_max,
],
y=[
y_min, y_max, y_max, y_min,
y_min, y_max, y_max, y_min,
],
z=[
z_min, z_min, z_min, z_min,
z_max, z_max, z_max, z_max,
],
color=color_value,
i=[7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2],
j=[3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3],
k=[0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6],
opacity=opacity,
flatshading=flat_shading,
hovertext='text',
hoverinfo=hover_info,
)
def create_z_grid(len_x_df_uniq, len_y_df_uniq, z_df):
z_temp_df = []
for x, y in itertools.product(range(len_x_df_uniq), range(len_y_df_uniq)):
if x == y:
z_temp_df.append(z_df[x])
else:
z_temp_df.append(None)
return z_temp_df
def figure_layout(
fig: go.Figure,
x_legend: str, y_legend, x_min, len_x_df_uniq, x_title, y_title, len_y_df_uniq,
z_legend, z_title, title,
):
y_min = 0
fig.update_layout(
scene=dict(
xaxis=dict(
tickmode='array',
ticktext=x_legend,
tickvals=np.arange(x_min, len_x_df_uniq * 2, step=2),
title=x_title,
),
yaxis=dict(
tickmode='array',
ticktext=y_legend,
tickvals=np.arange(y_min, len_y_df_uniq * 2, step=2),
title=y_title,
),
zaxis=dict(title=z_title),
),
)
if z_legend is None:
fig.update_layout(
scene=dict(
zaxis=dict(
tickmode='array',
ticktext=z_legend,
title=z_title,
),
),
template='plotly_white',
)
fig.update_layout(title=title)
return fig
def bar_charts_from_sparse_array(
x_df, y_df, z_df, x_min=0, y_min=0, z_min='auto', step=1, color='x',
x_legend='auto', y_legend='auto', z_legend='auto', flat_shading=True,
x_title='', y_title='', z_title='', hover_info='z', title='',
) -> go.Figure:
"""
Convert a dataframe in 3D barchart similar to matplotlib ones
Example :
xdf = pd.Series([1, 10])
ydf = pd.Series([2, 4])
zdf = pd.Series([10, 30, 20, 45])
fig = plotly_bar_charts_3d(xdf, ydf, zdf, color='x+y')
fig.show()
:param x_df: Serie or list of data corresponding to x-axis
:param y_df: Serie or list of data corresponding to y-axis
:param z_df: Serie or list of data corresponding to height of the bar chart
:param x_min: Starting position for x-axis
:param y_min: Starting position for y-axis
:param z_min: Minimum value of the barchart, if set to auto minimum value is 0.8 * minimum of z_df to obtain more
packed charts
:param step: Distance between two bar charts
:param color: Axis to create color, possible parameters are
x for a different color for each change of x
y for a different color for each change of y
or x+y to get a different color for each bar
:param x_legend: Legend of x-axis, if set to auto the legend is based on x_df
:param y_legend: Legend of y-axis, if set to auto the legend is based on y_df
:param z_legend: Legend of z axis, if set to auto the legend is not shown
:param flat_shading:
:param x_title: Title of x-axis
:param y_title: Title of y-axis
:param z_title: Title of z axis
:param hover_info: Hover info, z by default
:param title: Title of the graph, not functional for the moment
:return: 3D mesh figure acting as 3D bar charts
"""
z_df = list(pd.Series(z_df))
if z_min == 'auto':
z_min = 0.8 * min(z_df)
mesh_list = []
colors = px.colors.qualitative.Plotly
color_value = 0
len_x_df_uniq = len(x_df)
len_y_df_uniq = len(y_df)
z_temp_df = create_z_grid(len_x_df_uniq, len_y_df_uniq, z_df)
for idx, x_data in enumerate(x_df):
if color == 'x':
color_value = colors[idx % 9]
for idx2, y_data in enumerate(y_df):
if color == 'x+y':
color_value = colors[(idx + idx2 * len_y_df_uniq) % 9]
elif color == 'y':
color_value = colors[idx2 % 9]
x_max = x_min + step
y_max = y_min + step
z_max = z_temp_df[idx * len_x_df_uniq + idx2]
if z_max is not None:
mesh_list.append(
generate_mesh(
x_min, x_max, y_min, y_max, z_min, z_max, color_value,
flat_shading, hover_info,
),
)
else:
mesh_list.append(
generate_mesh(
x_min, x_max, y_min, y_max, z_min, z_max, color_value,
flat_shading, hover_info, opacity=0.01,
),
)
x_min += 2 * step
y_min += 2 * step
x_min = 0
fig = go.Figure(mesh_list)
if x_legend == 'auto':
x_legend = x_df
x_legend = [str(x_ax) for x_ax in x_legend]
if y_legend == 'auto':
y_legend = y_df
y_legend = [str(y_ax) for y_ax in y_legend]
if z_legend == 'auto':
z_legend = None
fig = figure_layout(
fig, x_legend, y_legend, x_min, len_x_df_uniq, x_title, y_title, len_y_df_uniq,
z_legend, z_title, title,
)
return fig
def bar_charts3d_from_array(
x_df, y_df, z_df, x_min=0, y_min=0, z_min='auto', step=1, color='x',
x_legend='auto', y_legend='auto', z_legend='auto', flat_shading=True,
x_title='', y_title='', z_title='', hover_info='z', title='',
) -> go.Figure:
"""
Convert a dataframe in 3D bar charts similar to matplotlib ones
Example :
x_df = pd.Series([1, 1, 10, 10])
y_df = pd.Series([2, 4, 2 ,4])
z_df = pd.Series([10, 30, 20, 45])
:param x_df: Serie of data corresponding to x-axis
:param y_df: Serie of data corresponding to y-axis
:param z_df: Serie of data corresponding to height of the bar chart
:param x_min: Starting position for x-axis
:param y_min: Starting position for y-axis
:param z_min: Minimum value of the barchart, if set to auto minimum value is 0.8 * minimum of z_df to obtain more
packed charts
:param step: Distance between two bar charts
:param color: Axis to create color, possible parameters are
x for a different color for each change of x
y for a different color for each change of y
or x+y to get a different color for each bar
:param x_legend: Legend of x-axis, if set to auto the legend is based on x_df
:param y_legend: Legend of y-axis, if set to auto the legend is based on y_df
:param z_legend: Legend of z axis, if set to auto the legend is not shown
:param flat_shading:
:param x_title: Title of x-axis
:param y_title: Title of y-axis
:param z_title: Title of z axis
:param hover_info: Hover info, z by default
:param title: Title of the graph, not functional for the moment
:return: 3D mesh figure acting as 3D bar charts
"""
if z_min == 'auto':
z_min = 0.8 * min(z_df)
mesh_list = []
colors = px.colors.qualitative.Plotly
color_value = 0
x_df = pd.Series(x_df)
y_df = pd.Series(y_df)
z_df = pd.Series(z_df)
x_df_uniq = x_df.unique()
y_df_uniq = y_df.unique()
len_x_df_uniq = len(x_df_uniq)
len_y_df_uniq = len(y_df_uniq)
for idx, x_data in enumerate(x_df_uniq):
if color == 'x':
color_value = colors[idx % 9]
for idx2, y_data in enumerate(y_df_uniq):
if color == 'x+y':
color_value = colors[(idx + idx2 * len(y_df.unique())) % 9]
elif color == 'y':
color_value = colors[idx2 % 9]
x_max = x_min + step
y_max = y_min + step
z_max = z_df[idx + idx2 * len_x_df_uniq]
mesh_list.append(
generate_mesh(
x_min, x_max, y_min, y_max, z_min, z_max, color_value, flat_shading,
hover_info,
),
)
x_min += 2 * step
y_min += 2 * step
x_min = 0
if x_legend == 'auto':
x_legend = x_df_uniq
x_legend = [str(x_ax) for x_ax in x_legend]
if y_legend == 'auto':
y_legend = y_df_uniq
y_legend = [str(y_ax) for y_ax in y_legend]
if z_legend == 'auto':
z_legend = None
fig = go.Figure(mesh_list)
fig = figure_layout(
fig, x_legend, y_legend, x_min, len_x_df_uniq, x_title, y_title, len_y_df_uniq,
z_legend, z_title, title,
)
return fig
def verify_input(x, y, z) -> bool:
""""
Verify that input is valid
"""
if len(x) * len(y) == len(z):
return True
if len(x) == len(y) == len(z):
return True
raise (
ValueError(
f'Input arguments are not matching, received x:{len(x)}, y:{len(y)}, z:{len(z)}, expected x*y=z '
f'or x=y=z',
)
)
def convert_to_str(x: list[str]):
""""
Convert a list to a string
"""
return [str(x) for x in x]
def plotly_bar_charts_3d(
x_df, y_df, z_df, x_min=0, y_min=0, z_min='auto', step=1, color='x',
x_legend='auto', y_legend='auto', z_legend='auto', flat_shading=True,
x_title='', y_title='', z_title='', hover_info='z', title='',
):
"""
Generate a barchart in 3D or a sparse barchart in 3D
Examples :
xdf = pd.Series([1, 10])
ydf = pd.Series([2, 4])
zdf = pd.Series([10, 30, 20, 45])
fig = plotly_bar_charts_3d(xdf, ydf, zdf, color='x+y')
fig.show()
features = [2, 3, 5, 10, 20]
neighbours = [31, 24, 10, 28, 48]
accuracies = [0.9727, 0.9994, 0.9994, 0.9995, 0.9995]
plotly_bar_charts_3d(
features, neighbours, accuracies,
x_title='Features', y_title='Neighbours', z_title='Accuracy',
).show()
"""
verify_input(x_df, y_df, z_df)
return (
bar_charts3d_from_array(
x_df,
y_df,
z_df,
x_min=x_min,
y_min=y_min,
z_min=z_min,
step=step,
color=color,
x_legend=x_legend,
y_legend=y_legend,
z_legend=z_legend,
flat_shading=flat_shading,
x_title=x_title,
y_title=y_title,
z_title=z_title,
hover_info=hover_info,
title=title,
)
if len(z_df) == (len(x_df) * len(y_df)) or (len(set(x_df)) + len(set(y_df))) == len(set(z_df))
else bar_charts_from_sparse_array(
x_df,
y_df,
z_df,
x_min=x_min,
y_min=y_min,
z_min=z_min,
step=step,
color=color,
x_legend=x_legend,
y_legend=y_legend,
z_legend=z_legend,
flat_shading=flat_shading,
x_title=x_title,
y_title=y_title,
z_title=z_title,
hover_info=hover_info,
title=title,
)
)
if __name__ == '__main__':
xdf = pd.Series([1, 10])
ydf = pd.Series([2, 4])
zdf = pd.Series([10, 30, 20, 45])
fig = plotly_bar_charts_3d(xdf, ydf, zdf, color='x+y')
fig.show()
features = [2, 3, 5, 10, 20]
neighbours = [31, 24, 10, 28, 48]
accuracies = [0.9727, 0.9994, 0.9994, 0.9995, 0.9995]
plotly_bar_charts_3d(
features, neighbours, accuracies,
x_title='Features', y_title='Neighbours', z_title='Accuracy',
).show()
df = pd.read_csv('examples/dataExample.csv')
fig = plotly_bar_charts_3d(
df['Gamma'], df['C'], df['score 1'], x_title='Gamma', y_title='C',
color='y',
)
fig.show()