-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
executable file
·140 lines (120 loc) · 3.88 KB
/
plot.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
#!/usr/bin/env python
"""Plot the Coding Game trajectories."""
import re
from typing import Dict, Union
from plotly.subplots import make_subplots
from code_busters import PATH, HOME
COLOR_SHORT = dict(
r="red",
g="green",
b="blue",
y="yellow",
v="violet",
o="orange",
c="cyan",
m="magenta",
k="black",
w="white",
)
COLOR_REPLACEMENT = dict(
darkyellow="gold",
darkblack="black",
darkwhite="lightslategrey",
lightred="indianred",
lightviolet="palevioletred",
lightorange="orangered",
lightmagenta="lightcoral",
lightblack="darkslategrey",
lightwhite="white",
)
def look(
desc: str = "", *, y2: bool = False
) -> Dict[str, Union[str, Dict[str, Union[str, int]]]]:
f"""
Helper function to format a plotly scatter plot.
Parameters
----------
desc: str, optional
A simplified description of the plot. Can contain a line shape (default
to marker only), a color (line and/or marker), and/or a width/size.
Line: _ or . or - or -- or -. or --. (solid, dotted, dash, longdash...)
Color: all the plotly colors
+ {', '.join([f'{k} ({v})' for k, v in COLOR_SHORT.items()])}
with optional prefix l (light) or d (dark)
Width (for lines) / Size (for markers):
0 to 6
y2: int, optional, keyword-only
If True, set the option 'secondary_y' to True
Examples
--------
>>> look()
{{'mode': 'markers'}}
>>> look("-dr1")
{{'mode': 'lines', 'line': {{'dash': 'dash', 'color': 'darkred', 'width': 1}} }}
>>> look(y2=True)
{{'mode': 'markers', 'secondary_y': True}}
>>> look("--.lb")
{{'mode': 'lines', 'line': {{'dash': 'longdashdot', 'color': 'lightblue'}} }}
>>> look("white3")
{{'mode': 'markers', 'marker': {{'color': 'white', 'size': 3}} }}
Returns
-------
Dict[str, Union[str, Dict[str, Union[str, int]]]]
"""
kwargs = dict(mode="markers")
if y2 is True:
kwargs["secondary_y"] = True
if not desc:
return kwargs
# noinspection RegExpAnonymousGroup
reg_desc = re.match(
r"^(?P<dash>_|(--\.)|(--)|(-\.)|\.|-)?(?P<color>[a-z]+)?(?P<width>\d)?$", desc
)
if not reg_desc:
print(f"Can not match look description: {desc}")
return kwargs
if reg_desc["dash"]:
kwargs["mode"] = "lines"
dash = ""
if "_" in reg_desc["dash"]:
dash += "solid"
elif "--" in reg_desc["dash"]:
dash += "longdash"
elif "-" in reg_desc["dash"]:
dash += "dash"
if "." in reg_desc["dash"]:
dash += "dot"
kwargs["line"] = dict(dash=dash)
if reg_desc["color"]:
cols = "".join(COLOR_SHORT.keys())
reg_col = re.match(fr"^(?P<prefix>[ld])?(?P<col>[{cols}])$", reg_desc["color"])
if reg_col:
color = ""
if reg_col["prefix"]:
color = "light" if "l" in reg_col["prefix"] else "dark"
color += COLOR_SHORT[reg_col["col"]]
else:
color = reg_desc["color"]
color = COLOR_REPLACEMENT.get(color, color)
if kwargs["mode"] == "lines":
kwargs.setdefault("line", {})["color"] = color
else:
kwargs.setdefault("marker", {})["color"] = color
if reg_desc["width"]:
if kwargs["mode"] == "lines":
# noinspection PyTypeChecker
kwargs.setdefault("line", {})["width"] = int(reg_desc["width"])
else:
# noinspection PyTypeChecker
kwargs.setdefault("marker", {})["size"] = int(reg_desc["width"])
return kwargs
if __name__ == "__main__":
fig = make_subplots()
for num, point_list in PATH.items():
x = [HOME.x]
y = [HOME.y]
for p in point_list:
x.append(p.x)
y.append(p.y)
fig.add_scatter(x=x, y=y, name=num)
fig.show()