forked from ghtmtt/DataPlotly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_plotly_plot.py
796 lines (673 loc) · 30.8 KB
/
data_plotly_plot.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# -*- coding: utf-8 -*-
"""
/***************************************************************************
DataPlotlyDialog
A QGIS plugin
D3 Plots for QGIS
-------------------
begin : 2017-03-05
git sha : $Format:%H$
copyright : (C) 2017 by matteo ghetta
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import plotly
import plotly.graph_objs as go
from plotly import tools
import tempfile
import os
import platform
import re
class Plot(object):
'''
Plot Class that creates the initial Plot object
Console usage:
# create the object
p = Plot(plot_type, plot_properties, layout_properties)
# where:
# plot_type (string): 'scatter'
# plot_properties (dictionary): {'x':[1,2,3], 'marker_width': 10}
# layout_properties (dictionary): {'legend'; True, 'title': 'Plot Title'}
The object created is ready to be elaborated by the other methods
'''
# create fixed class variables as paths for local javascript files
if platform.system() == 'Windows':
polyfillpath = 'file:///'
plotlypath = 'file:///'
polyfillpath += os.path.join(os.path.dirname(__file__), 'jsscripts/polyfill.min.js')
plotlypath += os.path.join(os.path.dirname(__file__), 'jsscripts/plotly-1.34.0.min.js')
else:
polyfillpath = os.path.join(os.path.dirname(__file__), 'jsscripts/polyfill.min.js')
plotlypath = os.path.join(os.path.dirname(__file__), 'jsscripts/plotly-1.34.0.min.js')
def __init__(self, plot_type, plot_properties, plot_layout):
# Define default plot dictionnary used as a basis for plot initilization
# prepare the default dictionary with None values
# plot properties
plotBaseProperties = {
'x': None,
'y': None,
'z': None,
'marker': None,
'featureIds': None,
'featureBox': None,
'custom': None,
'hover_text': None,
'additional_hover_text': None,
'x_name': None,
'y_name': None,
'z_name': None,
'in_color': None,
'out_color': None,
'marker_width': 1,
'marker_size': 10,
'marker_symbol': None,
'line_dash': None,
'box_orientation': 'v',
'opacity': None,
'box_stat': None,
'box_outliers': False,
'name': None,
'normalization': None,
'cont_type': None,
'color_scale': None,
'colorscale_in': None,
'show_lines': False,
'cumulative': False,
'show_colorscale_legend': False,
'invert_color_scale': False,
'invert_hist': False,
'bins': None
}
# layout nested dictionary
plotBaseLayout = {
'title': 'Plot Title',
'legend': True,
'legend_orientation': 'h',
'x_title': None,
'y_title': None,
'z_title': None,
'xaxis': None,
'bar_mode': None,
'x_type': None,
'y_type': None,
'x_inv': None,
'y_inv': None,
'range_slider': {'visible': False},
'bargaps': None
}
self.plotBaseDic = {
'plot_type': None,
'layer': None,
'plot_prop': plotBaseProperties,
'layout_prop': plotBaseLayout
}
# Set needed properties which are not yet set
# update the plot_prop
for k in self.plotBaseDic["plot_prop"]:
if k not in plot_properties:
plot_properties[k] = self.plotBaseDic["plot_prop"][k]
# update the layout_prop
for k in self.plotBaseDic["layout_prop"]:
if k not in plot_layout:
plot_layout[k] = self.plotBaseDic["layout_prop"][k]
# Set class properties
self.plot_type = plot_type
self.plot_properties = plot_properties
self.plot_layout = plot_layout
def buildTrace(self):
'''
build the final trace calling the go.xxx plotly method
this method here is the one performing the real job
From the initial object created (e.g. p = Plot(plot_type, plot_properties,
layout_properties)) this methods checks the plot_type and elaborates the
plot_properties dictionary passed
Console usage:
# create the initial object
p = Plot(plot_type, plot_properties, layout_properties)
# call the method
p.buildTrace()
Returns the final Plot Trace (final Plot object, AKA go.xxx plot type)
'''
if self.plot_type == 'scatter':
self.trace = [go.Scatter(
x=self.plot_properties['x'],
y=self.plot_properties['y'],
mode=self.plot_properties['marker'],
name=self.plot_properties['name'],
ids=self.plot_properties['featureIds'],
customdata=self.plot_properties['custom'],
text=self.plot_properties['additional_hover_text'],
hoverinfo=self.plot_properties['hover_text'],
marker=dict(
color=self.plot_properties['in_color'],
colorscale=self.plot_properties['colorscale_in'],
showscale=self.plot_properties['show_colorscale_legend'],
reversescale=self.plot_properties['invert_color_scale'],
colorbar=dict(
len=0.8
),
size=self.plot_properties['marker_size'],
symbol=self.plot_properties['marker_symbol'],
line=dict(
color=self.plot_properties['out_color'],
width=self.plot_properties['marker_width']
)
),
line=dict(
width=self.plot_properties['marker_width'],
dash=self.plot_properties['line_dash']
),
opacity=self.plot_properties['opacity']
)]
elif self.plot_type == 'box':
# flip the variables according to the box orientation
if self.plot_properties['box_orientation'] == 'h':
self.plot_properties['x'], self.plot_properties['y'] = self.plot_properties['y'], self.plot_properties['x']
self.trace = [go.Box(
x=self.plot_properties['x'],
y=self.plot_properties['y'],
name=self.plot_properties['name'],
customdata=self.plot_properties['custom'],
boxmean=self.plot_properties['box_stat'],
orientation=self.plot_properties['box_orientation'],
boxpoints=self.plot_properties['box_outliers'],
fillcolor=self.plot_properties['in_color'],
line=dict(
color=self.plot_properties['out_color'],
width=self.plot_properties['marker_width']
),
opacity=self.plot_properties['opacity']
)]
elif self.plot_type == 'bar':
if self.plot_properties['box_orientation'] == 'h':
self.plot_properties['x'], self.plot_properties['y'] = self.plot_properties['y'], self.plot_properties['x']
self.trace = [go.Bar(
x=self.plot_properties['x'],
y=self.plot_properties['y'],
name=self.plot_properties['name'],
ids=self.plot_properties['featureBox'],
customdata=self.plot_properties['custom'],
orientation=self.plot_properties['box_orientation'],
marker=dict(
color=self.plot_properties['in_color'],
colorscale=self.plot_properties['colorscale_in'],
showscale=self.plot_properties['show_colorscale_legend'],
reversescale=self.plot_properties['invert_color_scale'],
colorbar=dict(
len=0.8
),
line=dict(
color=self.plot_properties['out_color'],
width=self.plot_properties['marker_width']
)
),
opacity=self.plot_properties['opacity']
)]
elif self.plot_type == 'histogram':
self.trace = [go.Histogram(
x=self.plot_properties['x'],
y=self.plot_properties['x'],
name=self.plot_properties['name'],
orientation=self.plot_properties['box_orientation'],
nbinsx=self.plot_properties['bins'],
nbinsy=self.plot_properties['bins'],
marker=dict(
color=self.plot_properties['in_color'],
line=dict(
color=self.plot_properties['out_color'],
width=self.plot_properties['marker_width']
)
),
histnorm=self.plot_properties['normalization'],
opacity=self.plot_properties['opacity'],
cumulative=dict(
enabled=self.plot_properties['cumulative'],
direction=self.plot_properties['invert_hist']
)
)]
elif self.plot_type == 'pie':
self.trace = [go.Pie(
labels=self.plot_properties['x'],
values=self.plot_properties['y'],
name=self.plot_properties['custom'][0],
)]
elif self.plot_type == '2dhistogram':
self.trace = [go.Histogram2d(
x=self.plot_properties['x'],
y=self.plot_properties['y'],
colorscale=self.plot_properties['color_scale']
)]
elif self.plot_type == 'polar':
self.trace = [go.Scatterpolar(
r=self.plot_properties['x'],
theta=self.plot_properties['y'],
mode=self.plot_properties['marker'],
name=self.plot_properties['y_name'],
marker=dict(
color=self.plot_properties['in_color'],
size=self.plot_properties['marker_size'],
symbol=self.plot_properties['marker_symbol'],
line=dict(
color=self.plot_properties['out_color'],
width=self.plot_properties['marker_width']
)
),
line=dict(
color=self.plot_properties['in_color'],
width=self.plot_properties['marker_width'],
dash=self.plot_properties['line_dash']
),
opacity=self.plot_properties['opacity'],
)]
elif self.plot_type == 'ternary':
# prepare the hover text to display if the additional combobox is empty or not
# this setting is necessary to overwrite the standard hovering labels
if self.plot_properties['additional_hover_text'] == []:
text = [self.plot_properties['x_name'] + ': {}'.format(self.plot_properties['x'][k]) + '<br>{}: {}'.format(self.plot_properties['y_name'], self.plot_properties['y'][k]) + '<br>{}: {}'.format(self.plot_properties['z_name'], self.plot_properties['z'][k]) for k in range(len(self.plot_properties['x']))]
else:
text = [self.plot_properties['x_name'] + ': {}'.format(self.plot_properties['x'][k]) + '<br>{}: {}'.format(self.plot_properties['y_name'], self.plot_properties['y'][k]) + '<br>{}: {}'.format(self.plot_properties['z_name'], self.plot_properties['z'][k]) + '<br>{}'.format(self.plot_properties['additional_hover_text'][k]) for k in range(len(self.plot_properties['x']))]
self.trace = [go.Scatterternary(
a=self.plot_properties['x'],
b=self.plot_properties['y'],
c=self.plot_properties['z'],
name=self.plot_properties['x_name'] + ' + ' + self.plot_properties['y_name'] + ' + ' + self.plot_properties['z_name'],
hoverinfo='text',
text=text,
mode='markers',
marker=dict(
color=self.plot_properties['in_color'],
colorscale=self.plot_properties['colorscale_in'],
showscale=self.plot_properties['show_colorscale_legend'],
reversescale=self.plot_properties['invert_color_scale'],
colorbar=dict(
len=0.8
),
size=self.plot_properties['marker_size'],
symbol=self.plot_properties['marker_symbol'],
line=dict(
color=self.plot_properties['out_color'],
width=self.plot_properties['marker_width']
)
),
opacity=self.plot_properties['opacity']
)]
elif self.plot_type == 'contour':
self.trace = [go.Contour(
z=[self.plot_properties['x'], self.plot_properties['y']],
contours=dict(
coloring=self.plot_properties['cont_type'],
showlines=self.plot_properties['show_lines']
),
colorscale=self.plot_properties['color_scale'],
opacity=self.plot_properties['opacity']
)]
elif self.plot_type == 'violin':
# flip the variables according to the box orientation
if self.plot_properties['box_orientation'] == 'h':
self.plot_properties['x'], self.plot_properties['y'] = self.plot_properties['y'], self.plot_properties['x']
self.trace = [go.Violin(
x=self.plot_properties['x'],
y=self.plot_properties['y'],
name=self.plot_properties['name'],
customdata=self.plot_properties['custom'],
orientation=self.plot_properties['box_orientation'],
points=self.plot_properties['box_outliers'],
fillcolor=self.plot_properties['in_color'],
line=dict(
color=self.plot_properties['out_color'],
width=self.plot_properties['marker_width']
),
opacity=self.plot_properties['opacity'],
meanline=dict(
visible=self.plot_properties['show_mean_line']
),
side=self.plot_properties['violin_side']
)]
return self.trace
def buildLayout(self):
'''
build the final layout calling the go.Layout plotly method
From the initial object created (e.g. p = Plot(plot_type, plot_properties,
layout_properties)) this methods checks the plot_type and elaborates the
layout_properties dictionary passed
Console usage:
# create the initial object
p = Plot(plot_type, plot_properties, layout_properties)
# call the method
p.buildLayout()
Returns the final Plot Layout (final Layout object, AKA go.Layout)
'''
# flip the variables according to the box orientation
if self.plot_properties['box_orientation'] == 'h':
self.plot_layout['x_title'], self.plot_layout['y_title'] = self.plot_layout['y_title'], self.plot_layout['x_title']
self.layout = go.Layout(
showlegend=self.plot_layout['legend'],
legend=dict(
orientation=self.plot_layout['legend_orientation']
),
title=self.plot_layout['title'],
xaxis=dict(
title=self.plot_layout['x_title'],
autorange=self.plot_layout['x_inv']
),
yaxis=dict(
title=self.plot_layout['y_title'],
autorange=self.plot_layout['y_inv']
)
)
# update the x and y axis and add the linear and log only if the data are numeric
# pass if field is empty
try:
if isinstance(self.plot_properties['x'][0], (int, float)):
self.layout['xaxis'].update(type=self.plot_layout['x_type'])
except:
pass
try:
if isinstance(self.plot_properties['y'][0], (int, float)):
self.layout['yaxis'].update(type=self.plot_layout['y_type'])
except:
pass
# update layout properties depending on the plot type
if self.plot_type == 'scatter':
self.layout['xaxis'].update(rangeslider=self.plot_layout['range_slider'])
elif self.plot_type == 'bar':
self.layout['barmode'] = self.plot_layout['bar_mode']
elif self.plot_type == 'histogram':
self.layout['barmode'] = self.plot_layout['bar_mode']
self.layout['bargroupgap'] = self.plot_layout['bargaps']
elif self.plot_type == 'pie':
self.layout['xaxis'].update(title=''),
self.layout['xaxis'].update(showgrid=False),
self.layout['xaxis'].update(zeroline=False),
self.layout['xaxis'].update(showline=False),
self.layout['xaxis'].update(showticklabels=False),
self.layout['yaxis'].update(title=''),
self.layout['yaxis'].update(showgrid=False),
self.layout['yaxis'].update(zeroline=False),
self.layout['yaxis'].update(showline=False),
self.layout['yaxis'].update(showticklabels=False)
elif self.plot_type == 'ternary':
self.layout['xaxis'].update(title=''),
self.layout['xaxis'].update(showgrid=False),
self.layout['xaxis'].update(zeroline=False),
self.layout['xaxis'].update(showline=False),
self.layout['xaxis'].update(showticklabels=False),
self.layout['yaxis'].update(title=''),
self.layout['yaxis'].update(showgrid=False),
self.layout['yaxis'].update(zeroline=False),
self.layout['yaxis'].update(showline=False),
self.layout['yaxis'].update(showticklabels=False)
self.layout['ternary'] = dict(
sum=100,
aaxis=dict(
title=self.plot_layout['x_title'],
ticksuffix='%',
),
baxis=dict(
title=self.plot_layout['y_title'],
ticksuffix='%'
),
caxis=dict(
title=self.plot_layout['z_title'],
ticksuffix='%'
),
)
return self.layout
def js_callback(self, code_string):
'''
returns a string that is added to the end of the plot. This string is
necessary for the interaction between plot and map objects
WARNING! The string ReplaceTheDiv is a default string that will be
replaced in a second moment
'''
js_str = '''
<script>
// additional js function to select and click on the data
// returns the ids of the selected/clicked feature
var plotly_div = document.getElementById('ReplaceTheDiv')
var plotly_data = plotly_div.data
// selecting function
plotly_div.on('plotly_selected', function(data){
var dds = {};
dds["mode"] = 'selection'
dds["type"] = data.points[0].data.type
featureIds = [];
featureIdsTernary = [];
data.points.forEach(function(pt){
featureIds.push(parseInt(pt.id))
featureIdsTernary.push(parseInt(pt.pointNumber))
dds["id"] = featureIds
dds["tid"] = featureIdsTernary
})
//console.log(dds)
window.status = JSON.stringify(dds)
})
// clicking function
plotly_div.on('plotly_click', function(data){
var featureIds = [];
var dd = {};
dd["fidd"] = data.points[0].id
dd["mode"] = 'clicking'
// loop and create dictionary depending on plot type
for(var i=0; i < data.points.length; i++){
// scatter plot
if(data.points[i].data.type == 'scatter'){
dd["uid"] = data.points[i].data.uid
dd["type"] = data.points[i].data.type
data.points.forEach(function(pt){
dd["fid"] = pt.id
})
}
// pie
else if(data.points[i].data.type == 'pie'){
dd["type"] = data.points[i].data.type
dd["label"] = data.points[i].label
dd["field"] = data.points[i].data.name
console.log(data.points[i].label)
console.log(data.points[i])
}
// histogram
else if(data.points[i].data.type == 'histogram'){
dd["type"] = data.points[i].data.type
dd["uid"] = data.points[i].data.uid
dd["field"] = data.points[i].data.name
// correct axis orientation
if(data.points[i].data.orientation == 'v'){
dd["id"] = data.points[i].x
dd["bin_step"] = data.points[i].data.xbins.size
}
else {
dd["id"] = data.points[i].y
dd["bin_step"] = data.points[i].data.ybins.size
}
}
// box plot
else if(data.points[i].data.type == 'box'){
dd["uid"] = data.points[i].data.uid
dd["type"] = data.points[i].data.type
dd["field"] = data.points[i].data.customdata[0]
// correct axis orientation
if(data.points[i].data.orientation == 'v'){
dd["id"] = data.points[i].x
}
else {
dd["id"] = data.points[i].y
}
}
// violin plot
else if(data.points[i].data.type == 'violin'){
dd["uid"] = data.points[i].data.uid
dd["type"] = data.points[i].data.type
dd["field"] = data.points[i].data.customdata[0]
// correct axis orientation (for violin is viceversa)
if(data.points[i].data.orientation == 'v'){
dd["id"] = data.points[i].x
}
else {
dd["id"] = data.points[i].y
}
}
// bar plot
else if(data.points[i].data.type == 'bar'){
dd["uid"] = data.points[i].data.uid
dd["type"] = data.points[i].data.type
dd["field"] = data.points[i].data.customdata
// correct axis orientation
if(data.points[i].data.orientation == 'v'){
dd["id"] = data.points[i].x
}
else {
dd["id"] = data.points[i].y
}
}
// ternary
else if(data.points[i].data.type == 'scatterternary'){
dd["uid"] = data.points[i].data.uid
dd["type"] = data.points[i].data.type
dd["field"] = data.points[i].data.customdata
dd["fid"] = data.points[i].pointNumber
}
}
window.status = JSON.stringify(dd)
});
</script>'''
return js_str
def buildFigure(self):
'''
draw the final plot (single plot)
call the go.Figure plotly method and build the figure object adjust the
html file and add some line (including the js_string for the interaction)
save the html plot file in a temporary directory and return the path
that can be loaded in the QWebView
This method is directly usable after the plot object has been created and
the 2 methods (buildTrace and buildLayout) have been called
Returns the final html path containing the plot
Console usage:
# create the initial object
p = Plot(plot_type, plot_properties, layout_properties)
# call the methods to create the Trace and the Layout
p.buildTrace()
p.buildLayout()
# finally create the Figure
fig = p.buildFigure()
'''
fig = go.Figure(data=self.trace, layout=self.layout)
# first lines of additional html with the link to the local javascript
self.raw_plot = '<head><meta charset="utf-8" /><script src="{}"></script><script src="{}"></script></head>'.format(self.polyfillpath, self.plotlypath)
# set some configurations
config = {'scrollZoom': True, 'editable': True}
# call the plot method without all the javascript code
self.raw_plot += plotly.offline.plot(fig, output_type='div', include_plotlyjs=False, show_link=False, config=config)
# insert callback for javascript events
self.raw_plot += self.js_callback(self.raw_plot)
# use regex to replace the string ReplaceTheDiv with the correct plot id generated by plotly
match = re.search('Plotly.newPlot\("([^"]+)', self.raw_plot)
substr = match.group(1)
self.raw_plot = self.raw_plot.replace('ReplaceTheDiv', substr)
self.plot_path = os.path.join(tempfile.gettempdir(), 'temp_plot_name.html')
with open(self.plot_path, "w") as f:
f.write(self.raw_plot)
return self.plot_path
def buildFigures(self, plot_type, ptrace):
'''
Overlaps plots on the same map canvas
params:
plot_type (string): 'scatter'
ptrace (list of Plot Traces): list of all the different Plot Traces
plot_type argument in necessary for Bar and Histogram plots when the
options stack is chosen.
In this case the layouts of the firsts plot are deleted and only the last
one is taken into account (so to have the stack option).
self.layout is DELETED, so the final layout is taken from the LAST plot
configuration added
Returns the final html path containing the plot with the js_string for
the interaction
Console usage:
# create the initial object
p = Plot(plot_type, plot_properties, layout_properties)
# call the methods to create the Trace and the Layout
p.buildTrace()
p.buildLayout()
# finally create the Figure
fig = p.buildFigures(plot_type, ptrace)
'''
# assign the variables from the kwargs arguments
# plot_type = kwargs['plot_type']
# ptrace = kwargs['pl']
# check if the plot type and render the correct figure
if plot_type == 'bar' or 'histogram':
del self.layout
self.layout = go.Layout(
barmode=self.plot_layout['bar_mode']
)
figures = go.Figure(data=ptrace, layout=self.layout)
else:
figures = go.Figure(data=ptrace, layout=self.layout)
# set some configurations
config = {'scrollZoom': True, 'editable': True}
# first lines of additional html with the link to the local javascript
self.raw_plot = '<head><meta charset="utf-8" /><script src="{}"></script><script src="{}"></script></head>'.format(self.polyfillpath, self.plotlypath)
# call the plot method without all the javascript code
self.raw_plot += plotly.offline.plot(figures, output_type='div', include_plotlyjs=False, show_link=False, config=config)
# insert callback for javascript events
self.raw_plot += self.js_callback(self.raw_plot)
# use regex to replace the string ReplaceTheDiv with the correct plot id generated by plotly
match = re.search('Plotly.newPlot\("([^"]+)', self.raw_plot)
substr = match.group(1)
self.raw_plot = self.raw_plot.replace('ReplaceTheDiv', substr)
self.plot_path = os.path.join(tempfile.gettempdir(), 'temp_plot_name.html')
with open(self.plot_path, "w") as f:
f.write(self.raw_plot)
return self.plot_path
def buildSubPlots(self, grid, row, column, ptrace, tit_lst):
'''
Draws plot in different plot canvases (not overlapping)
params:
grid (string): 'row' or 'col'. Plot are created in rows or columns
row (int): number of rows (if row is selected)
column (int): number of columns (if column is selected)
ptrace (list of Plot Traces): list of all the different Plot Traces
tit_lst (tuple): tuple containing the plot titles
Returns the final html path containing the plot with the js_string for
the interaction
Console usage:
# create the initial object
p = Plot(plot_type, plot_properties, layout_properties)
# call the methods to create the Trace and the Layout
p.buildTrace()
p.buildLayout()
# finally create the Figure
fig = p.buildSubPlots('row', 1, gr, pl, tt)
'''
if grid == 'row':
fig = tools.make_subplots(rows=row, cols=column, subplot_titles=tit_lst)
for i, itm in enumerate(ptrace):
fig.append_trace(itm, row, i + 1)
elif grid == 'col':
fig = tools.make_subplots(rows=row, cols=column)
for i, itm in enumerate(ptrace):
fig.append_trace(itm, i + 1, column)
# set some configurations
config = {'scrollZoom': True, 'editable': True}
# first lines of additional html with the link to the local javascript
self.raw_plot = '<head><meta charset="utf-8" /><script src="{}"></script><script src="{}"></script></head>'.format(self.polyfillpath, self.plotlypath)
# call the plot method without all the javascript code
self.raw_plot += plotly.offline.plot(fig, output_type='div', include_plotlyjs=False, show_link=False, config=config)
# insert callback for javascript events
self.raw_plot += self.js_callback(self.raw_plot)
# use regex to replace the string ReplaceTheDiv with the correct plot id generated by plotly
match = re.search('Plotly.newPlot\("([^"]+)', self.raw_plot)
substr = match.group(1)
self.raw_plot = self.raw_plot.replace('ReplaceTheDiv', substr)
self.plot_path = os.path.join(tempfile.gettempdir(), 'temp_plot_name.html')
with open(self.plot_path, "w") as f:
f.write(self.raw_plot)
return self.plot_path