Skip to content

Commit 4856988

Browse files
committed
making it look good
1 parent 8f5dc34 commit 4856988

File tree

1 file changed

+107
-56
lines changed

1 file changed

+107
-56
lines changed

plotly/figure_factory/_facet_grid.py

Lines changed: 107 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
DEFUALT_MARKER_COLOR = '#000000'
1414
TICK_COLOR = '#969696'
1515
AXIS_TITLE_COLOR = '#0f0f0f'
16+
AXIS_TITLE_SIZE = 12
1617
GRID_COLOR = '#ffffff'
1718
LEGEND_COLOR = '#efefef'
18-
PLOT_BGCOLOR = '#e0e0e0'
19+
PLOT_BGCOLOR = '#ededed'
1920
ANNOT_RECT_COLOR = '#d0d0d0'
20-
HORIZONTAL_SPACING = 0.02
21-
VERTICAL_SPACING = 0.02
21+
HORIZONTAL_SPACING = 0.015
22+
VERTICAL_SPACING = 0.015
2223
LEGEND_BORDER_WIDTH = 1
2324
LEGEND_ANNOT_X = 1.05
2425
LEGEND_ANNOT_Y = 0.5
25-
26+
DTICK = 1
2627

2728
def _return_label(original_label, facet_labels, facet_var):
2829
if isinstance(facet_labels, dict):
@@ -94,7 +95,7 @@ def _axis_title_annotation(text, x_or_y_axis):
9495
y_pos = 0.5
9596
textangle = 270
9697

97-
annot = {'font': {'color': '#000000', 'size': 16},
98+
annot = {'font': {'color': '#000000', 'size': AXIS_TITLE_SIZE},
9899
'showarrow': False,
99100
'text': text,
100101
'textangle': textangle,
@@ -143,7 +144,9 @@ def _add_shapes_to_fig(fig, annot_rect_color):
143144
def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
144145
colormap, title, height, width, num_of_rows,
145146
num_of_cols, facet_row_labels,
146-
facet_col_labels, trace_type, **kwargs):
147+
facet_col_labels, trace_type, size,
148+
**kwargs):
149+
147150
fig = make_subplots(rows=num_of_rows, cols=num_of_cols,
148151
shared_xaxes=True, shared_yaxes=True,
149152
horizontal_spacing=HORIZONTAL_SPACING,
@@ -160,7 +163,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
160163
type=trace_type,
161164
name=group[0],
162165
marker=dict(
163-
color=colormap[group[0]]
166+
color=colormap[group[0]],
167+
size=size
164168
),
165169
**kwargs
166170
)
@@ -178,7 +182,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
178182
type=trace_type,
179183
name=color_val,
180184
marker=dict(
181-
color=colormap[color_val]
185+
color=colormap[color_val],
186+
size=size
182187
),
183188
**kwargs
184189
)
@@ -203,7 +208,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
203208
type=trace_type,
204209
name=color_val,
205210
marker=dict(
206-
color=colormap[color_val]
211+
color=colormap[color_val],
212+
size=size
207213
),
208214
**kwargs
209215
)
@@ -242,7 +248,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
242248
type=trace_type,
243249
name=color_val,
244250
marker=dict(
245-
color=colormap[color_val]
251+
color=colormap[color_val],
252+
size=size
246253
),
247254
**kwargs
248255
)
@@ -254,7 +261,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
254261
type=trace_type,
255262
name=color_val,
256263
marker=dict(
257-
color=colormap[color_val]
264+
color=colormap[color_val],
265+
size=size
258266
),
259267
showlegend=False,
260268
**kwargs
@@ -287,7 +295,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
287295
def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
288296
colormap, title, height, width,
289297
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+
291300
fig = make_subplots(rows=num_of_rows, cols=num_of_cols,
292301
shared_xaxes=True, shared_yaxes=True,
293302
horizontal_spacing=HORIZONTAL_SPACING,
@@ -302,8 +311,9 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
302311
type=trace_type,
303312
marker=dict(
304313
color=df[color_name],
314+
size=size,
305315
colorscale=colormap,
306-
showscale=True
316+
showscale=True,
307317
),
308318
**kwargs
309319
)
@@ -319,6 +329,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
319329
type=trace_type,
320330
marker=dict(
321331
color=df[color_name].tolist(),
332+
size=size,
322333
colorscale=colormap,
323334
showscale=True,
324335
colorbar=dict(x=1.15)
@@ -344,6 +355,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
344355
type=trace_type,
345356
marker=dict(
346357
color=df[color_name].tolist(),
358+
size=size,
347359
colorscale=colormap,
348360
showscale=True,
349361
colorbar=dict(x=1.15)
@@ -381,6 +393,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
381393
type=trace_type,
382394
marker=dict(
383395
color=df[color_name].tolist(),
396+
size=size,
384397
colorscale=colormap,
385398
showscale=(row_count == 0),
386399
colorbar=dict(x=1.15)
@@ -423,7 +436,8 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
423436

424437
def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
425438
num_of_rows, num_of_cols, facet_row_labels, facet_col_labels,
426-
trace_type, **kwargs):
439+
trace_type, size, **kwargs):
440+
427441
fig = make_subplots(rows=num_of_rows, cols=num_of_cols,
428442
shared_xaxes=True, shared_yaxes=True,
429443
horizontal_spacing=HORIZONTAL_SPACING,
@@ -436,7 +450,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
436450
mode='markers',
437451
type=trace_type,
438452
marker=dict(
439-
color=DEFUALT_MARKER_COLOR
453+
color=DEFUALT_MARKER_COLOR,
454+
size=size,
440455
),
441456
**kwargs
442457
)
@@ -451,7 +466,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
451466
mode='markers',
452467
type=trace_type,
453468
marker=dict(
454-
color=DEFUALT_MARKER_COLOR
469+
color=DEFUALT_MARKER_COLOR,
470+
size=size,
455471
),
456472
**kwargs
457473
)
@@ -481,7 +497,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
481497
mode='markers',
482498
type=trace_type,
483499
marker=dict(
484-
color=DEFUALT_MARKER_COLOR
500+
color=DEFUALT_MARKER_COLOR,
501+
size=size,
485502
),
486503
**kwargs
487504
)
@@ -512,7 +529,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
512529
mode='markers',
513530
type=trace_type,
514531
marker=dict(
515-
color=DEFUALT_MARKER_COLOR
532+
color=DEFUALT_MARKER_COLOR,
533+
size=size,
516534
),
517535
**kwargs
518536
)
@@ -546,8 +564,9 @@ def _facet_grid(df, x, y, facet_row, facet_col, title, height, width,
546564

547565
def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
548566
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):
551570
"""
552571
Returns figure for facet grid.
553572
@@ -575,7 +594,12 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
575594
:param (int) height: the height of the facet grid figure.
576595
:param (int) width: the width of the facet grid figure.
577596
: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.
579603
:param (dict) kwargs: a dictionary of scatterplot arguments.
580604
581605
"""
@@ -602,6 +626,17 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
602626
"in your dataframe."
603627
)
604628

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+
605640
# make sure dataframe index starts at 0
606641
df.index = range(len(df))
607642

@@ -662,7 +697,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
662697
width, num_of_rows,
663698
num_of_cols, facet_row_labels,
664699
facet_col_labels, trace_type,
665-
**kwargs)
700+
size, **kwargs)
666701

667702
elif isinstance(df[color_name][0], Number):
668703
if isinstance(colormap, dict):
@@ -683,7 +718,8 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
683718
num_of_cols,
684719
facet_row_labels,
685720
facet_col_labels,
686-
trace_type, **kwargs)
721+
trace_type, size,
722+
**kwargs)
687723

688724
elif isinstance(colormap, list):
689725
colorscale_list = colormap
@@ -696,7 +732,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
696732
num_of_cols,
697733
facet_row_labels,
698734
facet_col_labels,
699-
trace_type, **kwargs)
735+
trace_type, size, **kwargs)
700736
elif isinstance(colormap, str):
701737
if colormap in colors.PLOTLY_SCALES.keys():
702738
colorscale_list = colors.PLOTLY_SCALES[colormap]
@@ -713,7 +749,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
713749
num_of_cols,
714750
facet_row_labels,
715751
facet_col_labels,
716-
trace_type, **kwargs)
752+
trace_type, size, **kwargs)
717753
else:
718754
colorscale_list = colors.PLOTLY_SCALES['Reds']
719755
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,
723759
num_of_cols,
724760
facet_row_labels,
725761
facet_col_labels,
726-
trace_type, **kwargs)
762+
trace_type, size, **kwargs)
727763

728764
else:
729765
fig = _facet_grid(df, x, y, facet_row, facet_col, title, height,
730766
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)
732768

733769
fig['layout'].update(height=height, width=width, title=title)
734770
fig['layout'].update(plot_bgcolor=PLOT_BGCOLOR)
@@ -767,42 +803,57 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
767803
# add shaded regions behind axis titles
768804
_add_shapes_to_fig(fig, ANNOT_RECT_COLOR)
769805

770-
# fix ranges for subplots in same facet row/col
771806
axis_labels = {'x': [], 'y': []}
772-
773807
for key in fig['layout']:
774808
if 'xaxis' in key:
775809
axis_labels['x'].append(key)
776810
elif 'yaxis' in key:
777811
axis_labels['y'].append(key)
778812

813+
# ticks
779814
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]
807858

808859
return fig

0 commit comments

Comments
 (0)