/
theme.py
142 lines (124 loc) · 3.89 KB
/
theme.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
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from copy import deepcopy
import warnings
THEME_PARAMETERS = {
"axis_line": "?",
"axis_text": "?",
"axis_text_x": "?",
"axis_text_y": "?",
"axis_title": "?",
"axis_title_x": "?",
"axis_title_y": "?",
"axis_ticks": "?",
"axis_ticks_length": "?",
"axis_ticks_margin": "?",
"legend_background": "?",
"legend_key": "?",
"legend_key_size": "?",
"legend_key_height": "?",
"legend_key_width": "?",
"legend_margin": "?",
"legend_text": "?",
"legend_text_align": "?",
"legend_title": "?",
"legend_title_align": "?",
"legend_position": "?",
"legend_direction": "?",
"legend_justification": "?",
"legend_box": "?",
"plot_background": "?",
"plot_title": "?",
"plot_margin": "?",
"strip_background": "?",
"strip_text_x": "?",
"strip_text_y": "?",
"panel_background": "?",
"panel_border": "?",
"panel_grid_major_x": "?",
"panel_grid_minor_x": "?",
"panel_grid_major_y": "?",
"panel_grid_minor_y": "?",
"panel_margin": "?"
}
class theme_base(object):
_rcParams = {}
def __init__(self):
pass
def __radd__(self, other):
if other.__class__.__name__=="ggplot":
other.theme = self
return other
return self
def get_rcParams(self):
return self._rcParams
def apply_final_touches(self, ax):
pass
class theme(theme_base):
"""
Custom theme for your plot.
Parameters
----------
title:
title of your plot
plot_title:
title of your plot (same as title)
plot_margin:
size of plot margins
axis_title:
title of your plot (same as title)
axis_title_x:
x axis title
axis_title_y:
y axis title
axis_text:
theme for text
axis_text_x:
theme for x axis text
axis_text_y:
theme for y axis text
Examples
--------
>>> ggplot(mtcars, aes(x='mpg')) + geom_histogram() + theme()
>>> ggplot(mtcars, aes(x='mpg')) + geom_histogram() + theme(plot_margin=dict(bottom=0.2, left=0.2))
>>> ggplot(mtcars, aes(x='mpg')) + geom_histogram() + theme(axis_text=element_text(size=20))
>>> ggplot(mtcars, aes(x='mpg')) + geom_histogram() + theme(x_axis_text=element_text(color="orange"), y_axis_text=element_text(color="blue"))
>>> ggplot(mtcars, aes(x='mpg')) + geom_histogram() + theme(axis_text=element_text(size=20), x_axis_text=element_text(color="orange"), y_axis_text=element_text(color="blue"))
"""
# this maps theme element names to attributes of a ggplot object. there are
# more than one way to say the same thing
ATTRIBUTE_MAPPING = dict(
# title
title="title",
plot_title="title",
axis_title="title",
# margins
plot_margin="margins",
# text for x and y axis labels
axis_title_x="xlab",
axis_title_y="ylab",
axis_text="axis_text",
# text for x-axis
x_axis_text="x_axis_text",
axis_text_x="x_axis_text",
# text for y-axis
y_axis_text="y_axis_text",
axis_text_y="y_axis_text",
)
def __init__(self, *args, **kwargs):
self.things = deepcopy(kwargs)
def __radd__(self, other):
if other.__class__.__name__=="ggplot":
other.theme = self
for key, value in self.things.items():
try:
ggplot_attr_name = self.ATTRIBUTE_MAPPING[key]
except:
msg = "%s is an invalid theme parameter" % key
warnings.warn(msg, RuntimeWarning)
continue
setattr(other, ggplot_attr_name, value)
return other
return self
def parameter_lookup(self, parameter):
return THEME_PARAMETERS.get(parameter)