-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplotly_layout.py
275 lines (242 loc) · 7.97 KB
/
plotly_layout.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
""""""
BG_COLOR = "#FFFFFF"
LEGEND_BG_COLOR = "#FFFFFF"
LEGEND_BORDER_COLOR = "#FFFFFF"
DARK_TEXT_COLOR = "#000000"
LIGHT_TEXT_COLOR = "#808080" # gray
GRID_COLOR = "#DCDCDC" # gainsboro
FONT_FAMILY = "sans-serif"
BASE_FONT_SIZE = 16 # px
BASE_LINE_HEIGHT = 24 # px
"""The following functions are inspired from
http://sunlightfoundation.com/blog/2014/03/12/datavizguide/ and
https://betterwebtype.com/rhythm-in-web-typography
In the future, the parameters could be defined by data classes introduced
in python 3.7."""
def layout_by_line_height(
font_family=FONT_FAMILY,
font_size=BASE_FONT_SIZE,
line_height=BASE_LINE_HEIGHT,
bgcolor=BG_COLOR,
dark_text_color=DARK_TEXT_COLOR,
light_text_color=LIGHT_TEXT_COLOR,
n_canva=12,
canva_width=600,
left_margin=40,
right_margin=80,
title="title",
subtitle="subtitle",
ylabel="ylabel",
xlabel="xlabel",
**kwargs
):
"""This function returns a dictionary to be used in
plotly.graph_objs.Figure(data=[...], layout=[...]). The axes labels and
title are handled by annotations instead of the axes themselves in order
to use horizontal labelling for the vertical axis as well as a title and
a subtitle. The vertical dynamics is handled by a the lineheight.
Args:
font_family (str, default: FONT_FAMILY): font family
font_size (int, default: BASE_FONT_SIZE): font size of labels
line_height (int, default: BASE_LINE_HEIGHT)
bgcolor (str, default: BG_COLOR): background color,
dark_text_color (str, default: DARK_TEXT_COLOR): primary
color of the text in hexcode
light_text_color (str, defulat: LIGHT_TEXT_COLOR): secondary
color of the text in hexcode
n_canva (int, default: 12): the height of the canva in number
of line height
canva_width (float, defaults: 600): the plot (not chart) width
in pixels, the chart width is
canva_width + left_margin + right_margin
left_margin (int, default: 80): the left margin in pixels
right_margin (int, default: 80): the right margin in pixels
Returns:
dict: dictionary containing the parameters of the layout
"""
# Height and width of the plot
canva_height = n_canva * line_height
padding = 0.5 * line_height
# All positions are calculated in regard to the plot and
# at the baseline
parameters = dict(
xref="paper", yref="paper", yanchor="bottom", align="left", showarrow=False
)
# X label position
if xlabel:
x_label_bottom = 0.5 * line_height
x_label_height = 2.5 * line_height
else:
x_label_bottom = 0.5 * line_height
x_label_height = 1.5 * line_height
# Y label position on top of the plot
if ylabel:
y_label_bottom = padding
y_label_height = 1.0 * line_height + 2.0 * padding
else:
y_label_bottom = padding
y_label_height = 2.0 * padding
# If there is a subtitle, the number of lines is calculated
# from the number of <br>. Each line is treated independently.
subtitle_labels = []
if subtitle:
lines = list(reversed(subtitle.split("<br>")))
subtitle_height = line_height
for n, line in enumerate(lines):
subtitle_bottom = y_label_bottom + y_label_height + n * subtitle_height
subtitle_labels.append(
dict(
font=dict(size=0.889 * font_size, color=dark_text_color),
y=1 + subtitle_bottom / canva_height,
text=line,
)
)
else:
subtitle_bottom = y_label_bottom + y_label_height
subtitle_height = 0
# Title position
title_bottom = subtitle_bottom + subtitle_height
title_height = 1.5 * line_height
# Margins are calculated from the extreme positions: the title and
# the position of X label
bottom_margin = x_label_bottom + x_label_height
top_margin = title_bottom + title_height
# The left margin should take into account the number
# position of the labels
left_position_min = 40 # px
margin = dict(
l=max(left_margin, left_position_min + 5),
r=right_margin,
t=top_margin,
b=bottom_margin,
)
height = canva_height + margin["t"] + margin["b"]
width = canva_width + margin["l"] + margin["r"]
titles_left = -left_margin / canva_width
labels = []
if subtitle_labels:
for s in subtitle_labels:
labels.append(dict(x=titles_left, **s, **parameters))
labels += [
dict(
**parameters,
x=titles_left,
xanchor="left",
y=1 + title_bottom / canva_height,
font=dict(size=1.424 * font_size, color=dark_text_color),
text=title
),
dict(
**parameters,
x=titles_left,
xanchor="left",
y=1 + y_label_bottom / canva_height,
font=dict(size=font_size, color=dark_text_color),
text=ylabel
),
dict(
**parameters,
x=0.5,
xanchor="center",
y=-1.0 * x_label_height / canva_height,
font=dict(size=font_size, color=light_text_color),
text=xlabel
),
]
layout = dict(
width=width,
height=height,
font=dict(family=font_family),
hoverlabel=dict(font=dict(family=font_family)),
plot_bgcolor=bgcolor,
paper_bgcolor=bgcolor,
margin=margin,
annotations=labels,
)
layout.update(kwargs)
return layout
def axis_no_title(
font_size=0.889 * BASE_FONT_SIZE,
ticklen=5,
showgrid=True,
gridcolor=GRID_COLOR,
gridwidth=2,
zeroline=False,
linewidth=6,
linecolor=BG_COLOR,
**kwargs
):
"""
This function returns the parameters for an axis. The color of the axes are
by default the background ones, being therefore “transparent”. Ticks are
shown.
Args:
font_size (int, default: 0.889 * BASE_FONT_SIZE): font size
ticklen (int, default: 5)
showgrid (bool, default: True)
gridcolor (str, default: GRID_COLOR)
gridwidth (int, default: 2): manages the tick width too
zeroline (bool, default: False): show line for the axis
linewidth (int, default: 6),
linecolor (str, default: BG_COLOR)
Returns:
dict: dictionary containing the parameters of the axis
"""
d = dict(
tickfont=dict(size=font_size),
ticklen=ticklen,
tickwidth=gridwidth,
showgrid=showgrid,
gridcolor=gridcolor,
gridwidth=gridwidth,
zeroline=zeroline,
linewidth=linewidth,
linecolor=linecolor,
)
d.update(kwargs)
return d
def legend_dark(
bgcolor=LEGEND_BG_COLOR,
bordercolor=LEGEND_BORDER_COLOR,
borderwidth=1,
font_size=0.889 * BASE_FONT_SIZE,
font_color=DARK_TEXT_COLOR,
traceorder="normal",
xanchor="left",
x=1.05,
yanchor="bottom",
y=0.0,
**kwargs
):
"""
This function returns the parameters for the legend. The position is
determined in regards of the plotting area (paper ref).
Args:
bgcolor (str, default: LEGEND_BG_COLOR)
bordercolor (str, default: LEGEND_BORDER_COLOR)
borderwidth (int, default: 1)
font_size (int, default: 20)
font_color (str, default: DARK_TEXT_COLOR)
traceorder (str, default:normal)
xanchor (str, default: left)
x (float, default: 1.05): legend X position
yanchor (str, default: bottom)
y (float, default: 0.): legend Y position
"""
legend = dict(
traceorder=traceorder,
bgcolor=bgcolor,
bordercolor=bordercolor,
borderwidth=borderwidth,
font=dict(size=font_size, color=font_color),
xanchor=xanchor,
x=x,
yanchor=yanchor,
y=y,
)
legend.update(kwargs)
return legend
def main():
pass
if __name__ == "__main__":
main()