forked from has2k1/plotnine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_geom_text_label.py
95 lines (79 loc) · 2.82 KB
/
test_geom_text_label.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
import numpy as np
import pandas as pd
from plotnine import (
aes,
geom_label,
geom_point,
geom_text,
ggplot,
scale_size_continuous,
scale_y_continuous,
)
from plotnine.data import mtcars
n = 5
labels = ['ggplot', 'aesthetics', 'data', 'geoms',
r'$\mathbf{statistics^2}$', 'scales', 'coordinates']
df = pd.DataFrame({
'x': [1] * n,
'y': range(n),
'label': labels[:n],
'z': range(n),
'angle': np.linspace(0, 90, n)
})
adjust_text = {
'expand_points': (2, 2),
'arrowprops': {
'arrowstyle': '->',
'color': 'red'
}
}
def test_text_aesthetics():
p = (ggplot(df, aes(y='y', label='label')) +
geom_text(aes('x', label='label'), size=15, ha='left') +
geom_text(aes('x+1', angle='angle'),
size=15, va='top', show_legend=False) +
geom_text(aes('x+2', label='label', alpha='z'),
size=15, show_legend=False) +
geom_text(aes('x+3', color='factor(z)'),
size=15, show_legend=False) +
geom_text(aes('x+5', size='z'),
ha='right', show_legend=False) +
scale_size_continuous(range=(12, 30)) +
scale_y_continuous(limits=(-0.5, n-0.5)))
assert p == 'text_aesthetics'
def test_label_aesthetics():
p = (ggplot(df, aes(y='y', label='label')) +
geom_label(aes('x', label='label', fill='z'),
size=15, ha='left', show_legend=False, boxcolor='red') +
geom_label(aes('x+1', angle='angle'),
size=15, va='top', show_legend=False) +
geom_label(aes('x+2', label='label', alpha='z'),
size=15, show_legend=False) +
geom_label(aes('x+3', color='factor(z)'),
size=15, show_legend=False) +
geom_label(aes('x+5', size='z'),
ha='right', show_legend=False) +
scale_size_continuous(range=(12, 30)) +
scale_y_continuous(limits=(-0.5, n-0.5)))
assert p == 'label_aesthetics'
def test_adjust_text():
p = (ggplot(mtcars.tail(2), aes('mpg', 'disp', label='name'))
+ geom_point(size=5, fill='black')
+ geom_text(adjust_text=adjust_text)
)
assert p == 'adjust_text'
def test_adjust_label():
p = (ggplot(mtcars.tail(2), aes('mpg', 'disp', label='name'))
+ geom_point(size=5, fill='black')
+ geom_label(adjust_text=adjust_text)
)
assert p == 'adjust_label'
def test_adjust_text_default_color():
adjust_text2 = adjust_text.copy()
del adjust_text2['arrowprops']['color']
p = (ggplot(mtcars.tail(2), aes('mpg', 'disp', label='name'))
+ aes(color='factor(cyl)')
+ geom_point(size=5, fill='black')
+ geom_text(adjust_text=adjust_text2)
)
assert p == 'adjust_text_default_color'