13
13
DEFUALT_MARKER_COLOR = '#000000'
14
14
TICK_COLOR = '#969696'
15
15
AXIS_TITLE_COLOR = '#0f0f0f'
16
+ AXIS_TITLE_SIZE = 12
16
17
GRID_COLOR = '#ffffff'
17
18
LEGEND_COLOR = '#efefef'
18
- PLOT_BGCOLOR = '#e0e0e0 '
19
+ PLOT_BGCOLOR = '#ededed '
19
20
ANNOT_RECT_COLOR = '#d0d0d0'
20
- HORIZONTAL_SPACING = 0.02
21
- VERTICAL_SPACING = 0.02
21
+ HORIZONTAL_SPACING = 0.015
22
+ VERTICAL_SPACING = 0.015
22
23
LEGEND_BORDER_WIDTH = 1
23
24
LEGEND_ANNOT_X = 1.05
24
25
LEGEND_ANNOT_Y = 0.5
25
-
26
+ DTICK = 1
26
27
27
28
def _return_label (original_label , facet_labels , facet_var ):
28
29
if isinstance (facet_labels , dict ):
@@ -94,7 +95,7 @@ def _axis_title_annotation(text, x_or_y_axis):
94
95
y_pos = 0.5
95
96
textangle = 270
96
97
97
- annot = {'font' : {'color' : '#000000' , 'size' : 16 },
98
+ annot = {'font' : {'color' : '#000000' , 'size' : AXIS_TITLE_SIZE },
98
99
'showarrow' : False ,
99
100
'text' : text ,
100
101
'textangle' : textangle ,
@@ -143,7 +144,9 @@ def _add_shapes_to_fig(fig, annot_rect_color):
143
144
def _facet_grid_color_categorical (df , x , y , facet_row , facet_col , color_name ,
144
145
colormap , title , height , width , num_of_rows ,
145
146
num_of_cols , facet_row_labels ,
146
- facet_col_labels , trace_type , ** kwargs ):
147
+ facet_col_labels , trace_type , size ,
148
+ ** kwargs ):
149
+
147
150
fig = make_subplots (rows = num_of_rows , cols = num_of_cols ,
148
151
shared_xaxes = True , shared_yaxes = True ,
149
152
horizontal_spacing = HORIZONTAL_SPACING ,
@@ -160,7 +163,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
160
163
type = trace_type ,
161
164
name = group [0 ],
162
165
marker = dict (
163
- color = colormap [group [0 ]]
166
+ color = colormap [group [0 ]],
167
+ size = size
164
168
),
165
169
** kwargs
166
170
)
@@ -178,7 +182,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
178
182
type = trace_type ,
179
183
name = color_val ,
180
184
marker = dict (
181
- color = colormap [color_val ]
185
+ color = colormap [color_val ],
186
+ size = size
182
187
),
183
188
** kwargs
184
189
)
@@ -203,7 +208,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
203
208
type = trace_type ,
204
209
name = color_val ,
205
210
marker = dict (
206
- color = colormap [color_val ]
211
+ color = colormap [color_val ],
212
+ size = size
207
213
),
208
214
** kwargs
209
215
)
@@ -242,7 +248,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
242
248
type = trace_type ,
243
249
name = color_val ,
244
250
marker = dict (
245
- color = colormap [color_val ]
251
+ color = colormap [color_val ],
252
+ size = size
246
253
),
247
254
** kwargs
248
255
)
@@ -254,7 +261,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
254
261
type = trace_type ,
255
262
name = color_val ,
256
263
marker = dict (
257
- color = colormap [color_val ]
264
+ color = colormap [color_val ],
265
+ size = size
258
266
),
259
267
showlegend = False ,
260
268
** kwargs
@@ -287,7 +295,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
287
295
def _facet_grid_color_numerical (df , x , y , facet_row , facet_col , color_name ,
288
296
colormap , title , height , width ,
289
297
num_of_rows , num_of_cols , facet_row_labels ,
290
- facet_col_labels , trace_type , ** kwargs ):
298
+ facet_col_labels , trace_type , size , ** kwargs ):
299
+
291
300
fig = make_subplots (rows = num_of_rows , cols = num_of_cols ,
292
301
shared_xaxes = True , shared_yaxes = True ,
293
302
horizontal_spacing = HORIZONTAL_SPACING ,
@@ -302,8 +311,9 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
302
311
type = trace_type ,
303
312
marker = dict (
304
313
color = df [color_name ],
314
+ size = size ,
305
315
colorscale = colormap ,
306
- showscale = True
316
+ showscale = True ,
307
317
),
308
318
** kwargs
309
319
)
@@ -319,6 +329,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
319
329
type = trace_type ,
320
330
marker = dict (
321
331
color = df [color_name ].tolist (),
332
+ size = size ,
322
333
colorscale = colormap ,
323
334
showscale = True ,
324
335
colorbar = dict (x = 1.15 )
@@ -344,6 +355,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
344
355
type = trace_type ,
345
356
marker = dict (
346
357
color = df [color_name ].tolist (),
358
+ size = size ,
347
359
colorscale = colormap ,
348
360
showscale = True ,
349
361
colorbar = dict (x = 1.15 )
@@ -381,6 +393,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
381
393
type = trace_type ,
382
394
marker = dict (
383
395
color = df [color_name ].tolist (),
396
+ size = size ,
384
397
colorscale = colormap ,
385
398
showscale = (row_count == 0 ),
386
399
colorbar = dict (x = 1.15 )
@@ -423,7 +436,8 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
423
436
424
437
def _facet_grid (df , x , y , facet_row , facet_col , title , height , width ,
425
438
num_of_rows , num_of_cols , facet_row_labels , facet_col_labels ,
426
- trace_type , ** kwargs ):
439
+ trace_type , size , ** kwargs ):
440
+
427
441
fig = make_subplots (rows = num_of_rows , cols = num_of_cols ,
428
442
shared_xaxes = True , shared_yaxes = True ,
429
443
horizontal_spacing = HORIZONTAL_SPACING ,
@@ -436,7 +450,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
436
450
mode = 'markers' ,
437
451
type = trace_type ,
438
452
marker = dict (
439
- color = DEFUALT_MARKER_COLOR
453
+ color = DEFUALT_MARKER_COLOR ,
454
+ size = size ,
440
455
),
441
456
** kwargs
442
457
)
@@ -451,7 +466,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
451
466
mode = 'markers' ,
452
467
type = trace_type ,
453
468
marker = dict (
454
- color = DEFUALT_MARKER_COLOR
469
+ color = DEFUALT_MARKER_COLOR ,
470
+ size = size ,
455
471
),
456
472
** kwargs
457
473
)
@@ -481,7 +497,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
481
497
mode = 'markers' ,
482
498
type = trace_type ,
483
499
marker = dict (
484
- color = DEFUALT_MARKER_COLOR
500
+ color = DEFUALT_MARKER_COLOR ,
501
+ size = size ,
485
502
),
486
503
** kwargs
487
504
)
@@ -512,7 +529,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
512
529
mode = 'markers' ,
513
530
type = trace_type ,
514
531
marker = dict (
515
- color = DEFUALT_MARKER_COLOR
532
+ color = DEFUALT_MARKER_COLOR ,
533
+ size = size ,
516
534
),
517
535
** kwargs
518
536
)
@@ -546,8 +564,9 @@ def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
546
564
547
565
def create_facet_grid (df , x , y , facet_row = None , facet_col = None ,
548
566
color_name = None , colormap = None , facet_row_labels = None ,
549
- facet_col_labels = None , title = 'facet grid' , height = 600 ,
550
- width = 600 , trace_type = 'scatter' , ** kwargs ):
567
+ facet_col_labels = None , title = 'facet grid' , height = 700 ,
568
+ width = 700 , trace_type = 'scatter' , binwidth = 2 ,
569
+ scales = 'fixed' , size = 5 , ** kwargs ):
551
570
"""
552
571
Returns figure for facet grid.
553
572
@@ -575,7 +594,12 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
575
594
:param (int) height: the height of the facet grid figure.
576
595
:param (int) width: the width of the facet grid figure.
577
596
:param (str) trace_type: decides the type of plot to appear in the
578
- facet grid. The defualt is 'scatter'.
597
+ facet grid. The options are 'scatter' and 'scattergl'. The defualt is
598
+ 'scatter'.
599
+ :param (str) scales: determines if axes have fixed ranges or not. Valid
600
+ settings are 'fixed' (all axes fixed), 'free_x' (x axis free only),
601
+ 'free_y' (y axis free only) or 'free' (both axes free).
602
+ :param (int) size: scatterplot size.
579
603
:param (dict) kwargs: a dictionary of scatterplot arguments.
580
604
581
605
"""
@@ -602,6 +626,17 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
602
626
"in your dataframe."
603
627
)
604
628
629
+ # validate scales
630
+ if scales not in ['fixed' , 'free_x' , 'free_y' , 'free' ]:
631
+ raise exceptions .PlotlyError (
632
+ "'scales' must be set to 'fixed', 'free_x', 'free_y' and 'free'."
633
+ )
634
+
635
+ #if trace_type not in ['scatter', 'scattergl']:
636
+ # raise exceptions.PlotlyError(
637
+ # "'trace_type' must be either 'scatter' or 'scattergl'."
638
+ # )
639
+
605
640
# make sure dataframe index starts at 0
606
641
df .index = range (len (df ))
607
642
@@ -662,7 +697,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
662
697
width , num_of_rows ,
663
698
num_of_cols , facet_row_labels ,
664
699
facet_col_labels , trace_type ,
665
- ** kwargs )
700
+ size , ** kwargs )
666
701
667
702
elif isinstance (df [color_name ][0 ], Number ):
668
703
if isinstance (colormap , dict ):
@@ -683,7 +718,8 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
683
718
num_of_cols ,
684
719
facet_row_labels ,
685
720
facet_col_labels ,
686
- trace_type , ** kwargs )
721
+ trace_type , size ,
722
+ ** kwargs )
687
723
688
724
elif isinstance (colormap , list ):
689
725
colorscale_list = colormap
@@ -696,7 +732,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
696
732
num_of_cols ,
697
733
facet_row_labels ,
698
734
facet_col_labels ,
699
- trace_type , ** kwargs )
735
+ trace_type , size , ** kwargs )
700
736
elif isinstance (colormap , str ):
701
737
if colormap in colors .PLOTLY_SCALES .keys ():
702
738
colorscale_list = colors .PLOTLY_SCALES [colormap ]
@@ -713,7 +749,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
713
749
num_of_cols ,
714
750
facet_row_labels ,
715
751
facet_col_labels ,
716
- trace_type , ** kwargs )
752
+ trace_type , size , ** kwargs )
717
753
else :
718
754
colorscale_list = colors .PLOTLY_SCALES ['Reds' ]
719
755
fig = _facet_grid_color_numerical (df , x , y , facet_row ,
@@ -723,12 +759,12 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
723
759
num_of_cols ,
724
760
facet_row_labels ,
725
761
facet_col_labels ,
726
- trace_type , ** kwargs )
762
+ trace_type , size , ** kwargs )
727
763
728
764
else :
729
765
fig = _facet_grid (df , x , y , facet_row , facet_col , title , height ,
730
766
width , num_of_rows , num_of_cols , facet_row_labels ,
731
- facet_col_labels , trace_type , ** kwargs )
767
+ facet_col_labels , trace_type , size , ** kwargs )
732
768
733
769
fig ['layout' ].update (height = height , width = width , title = title )
734
770
fig ['layout' ].update (plot_bgcolor = PLOT_BGCOLOR )
@@ -767,42 +803,57 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
767
803
# add shaded regions behind axis titles
768
804
_add_shapes_to_fig (fig , ANNOT_RECT_COLOR )
769
805
770
- # fix ranges for subplots in same facet row/col
771
806
axis_labels = {'x' : [], 'y' : []}
772
-
773
807
for key in fig ['layout' ]:
774
808
if 'xaxis' in key :
775
809
axis_labels ['x' ].append (key )
776
810
elif 'yaxis' in key :
777
811
axis_labels ['y' ].append (key )
778
812
813
+ # ticks
779
814
for x_y in ['x' , 'y' ]:
780
- if len (axis_labels [x_y ]) > 1 :
781
- min_ranges = []
782
- max_ranges = []
783
- for trace in fig ['data' ]:
784
- if len (trace [x_y ]) > 0 :
785
- min_ranges .append (min (trace [x_y ]))
786
- max_ranges .append (max (trace [x_y ]))
787
- while None in min_ranges :
788
- min_ranges .remove (None )
789
- while None in max_ranges :
790
- max_ranges .remove (None )
791
-
792
- min_range = min (min_ranges )
793
- max_range = max (max_ranges )
794
-
795
- range_are_numbers = (isinstance (min_range , Number ) and
796
- isinstance (max_range , Number ))
797
-
798
- # floor and ceiling the range endpoints
799
- if range_are_numbers :
800
- min_range = math .floor (min_range ) - 1
801
- max_range = math .ceil (max_range ) + 1
802
-
803
- # insert ranges into fig
804
- for key in fig ['layout' ]:
805
- if '{}axis' .format (x_y ) in key and range_are_numbers :
806
- fig ['layout' ][key ]['range' ] = [min_range , max_range ]
815
+ for key in fig ['layout' ]:
816
+ if '{}axis' .format (x_y ) in key :
817
+ fig ['layout' ][key ]['dtick' ] = DTICK
818
+ fig ['layout' ][key ]['tickwidth' ] = 1
819
+ fig ['layout' ][key ]['ticklen' ] = 4
820
+
821
+ # fixed ranges
822
+ if scales in ['fixed' , 'free_x' , 'free_y' ]:
823
+ if scales == 'fixed' :
824
+ fixed_axes = ['x' , 'y' ]
825
+ elif scales == 'free_x' :
826
+ fixed_axes = ['y' ]
827
+ elif scales == 'free_y' :
828
+ fixed_axes = ['x' ]
829
+
830
+ for x_y in fixed_axes :
831
+ if len (axis_labels [x_y ]) > 1 :
832
+ min_ranges = []
833
+ max_ranges = []
834
+ for trace in fig ['data' ]:
835
+ if len (trace [x_y ]) > 0 :
836
+ min_ranges .append (min (trace [x_y ]))
837
+ max_ranges .append (max (trace [x_y ]))
838
+ while None in min_ranges :
839
+ min_ranges .remove (None )
840
+ while None in max_ranges :
841
+ max_ranges .remove (None )
842
+
843
+ min_range = min (min_ranges )
844
+ max_range = max (max_ranges )
845
+
846
+ range_are_numbers = (isinstance (min_range , Number ) and
847
+ isinstance (max_range , Number ))
848
+
849
+ # floor and ceiling the range endpoints
850
+ if range_are_numbers :
851
+ min_range = math .floor (min_range ) #- 1
852
+ max_range = math .ceil (max_range ) #+ 1
853
+
854
+ # insert ranges into fig
855
+ for key in fig ['layout' ]:
856
+ if '{}axis' .format (x_y ) in key and range_are_numbers :
857
+ fig ['layout' ][key ]['range' ] = [min_range , max_range ]
807
858
808
859
return fig
0 commit comments