-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlotting.py
More file actions
161 lines (142 loc) · 5.94 KB
/
Copy pathPlotting.py
File metadata and controls
161 lines (142 loc) · 5.94 KB
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
import matplotlib.pyplot as plt
import seaborn as sns
from typing import Literal
import json
import networkx as nx
from tools.Graphs import get_node_labels, get_edge_labels, make_graph_network
class Theme:
"""
Static class that holds different plotting themes.
Theme can be set with 'Theme.set_theme(theme)'
Themes are:
- 'scientific': Matches te Latex style in scientific journals, default
- 'solarized_dark': Matches the corresponding VS Code theme
- 'solarized_light': Matches the corresponding VS Code theme
Example:
```python
from tools.Plotting import Theme
Theme.set_theme('solarized_light')
plt.plot(...)
```
"""
solarized_dark = rc={"axes.edgecolor": '#B7B7B7',
"axes.facecolor": "#00212B",
"axes.grid": True,
"axes.labelcolor": "#B7B7B7",
"axes.linewidth": 0.75,
'figure.facecolor': "#002B36",
'grid.color': "#B7B7B7",
"grid.linestyle": ":",
"grid.linewidth": .5,
"legend.facecolor": "#1F3744",
"text.color": "#B7B7B7",
"xtick.color": "#B7B7B7",
"ytick.color": "#B7B7B7",
}
solarized_light = rc={'axes.facecolor': "#F7F0E0",
"axes.grid": True,
"axes.labelcolor": "#7F7F7F",
"axes.linewidth": 0.75,
'figure.facecolor': "#FDF6E3",
'grid.color': "#7F7F7F",
"grid.linestyle": ":",
"grid.linewidth": .5,
'axes.edgecolor': '#7F7F7F',
"legend.facecolor": "#EEE8D5",
"text.color": "#7F7F7F",
"xtick.color": "#7F7F7F",
"ytick.color": "#7F7F7F",
}
scientific = {"axes.grid": True,"grid.linestyle":":","grid.color":".5","legend.facecolor":"#ffffff"}
@classmethod
def _set_solarized_dark(cls):
sns.set_theme(style="ticks", rc=cls.solarized_dark,palette="bright")
@classmethod
def _set_solarized_light(cls):
sns.set_theme(style="ticks",rc=cls.solarized_light)
@classmethod
def _set_scientific(cls):
sns.set_theme(style='ticks',font='STIXGeneral', rc=cls.scientific)
@classmethod
def set_theme(cls, theme:Literal["auto","scientific","solarized_light","solarized_dark"]="auto"):
"""
Sets the plotting theme.
Arguments:
- 'theme':
- 'auto', automatically detect the workbench theme, default
- 'scientific', fallback if 'auto' can't detect the current VS Code theme
- 'solarized_dark', force Solarized Dark theme
- 'solarized_light', force Solarized Light theme
"""
match theme:
case "auto":
implemented_themes = {
"Solarized Light":cls._set_solarized_light,
"Solarized Dark":cls._set_solarized_dark
}
with open('.vscode/settings.json', 'r') as file:
settings = json.load(file)
if "workbench.colorTheme" in settings.keys():
vstheme = settings["workbench.colorTheme"]
try:
implemented_themes[vstheme]()
except KeyError as e:
raise NotImplementedError(f"Theme {vstheme} not implemented! Currently implemented are: {implemented_themes.keys()}")
else:
# Fallback to default seaborn style
sns.set_style()
case "scientific":
cls._set_scientific()
case "solarized_dark":
cls._set_solarized_dark()
case "solarized_light":
cls._set_solarized_light()
case _:
raise NotImplementedError
def default_color_generator():
"""
Endlessly cycles through the default colors. See plt.rcParams['axes.prop_cycle'].
Example:
```python
gen = default_color_generator() \n
fig, ax = plt.subplots() \n
[...] \n
for i in range(10): \n
ax.plot(..., color = next(gen)) \n
[...]
```
"""
i = 0
while True:
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']
yield colors[i%len(colors)]
i+=1
def plot_vine(vine, trees:list[int],ax, layout="graphviz",edge_labels=True):
"""
Plots the specified trees of a vine.
Arguments:
- vine: Vine object
- trees: List of tree indices to plot, e.g. [0,1,2]
- ax: Matplotlib axis to plot on
- layout: Layout for the graph, default "graphviz". Options: "graphviz" or "spring_layout". See networkx.draw_networkx for more information.
- edge_labels: Whether to display edge labels w/ copula info, default True.
"""
G = make_graph_network(vine, trees)
node_labels = get_node_labels(G)
edge_labels = get_edge_labels(G)
match layout:
case "graphviz":
pos = nx.drawing.nx_pydot.graphviz_layout(G, prog="dot") # dot, twopi, fdp, sfdp, circo
case "spring_layout":
pos = nx.spring_layout(G)
case _ as e:
raise NotImplementedError(f"Layout {e} not implemented!")
for node, data in G.nodes(data=True):
tree = G.nodes[node]["tree"]
shape = "s" if tree == 0 else "o"
nx.draw_networkx_nodes(G,pos,nodelist=[node],node_shape=shape,ax=ax,node_size=750)
nx.draw_networkx_labels(G,pos,labels=node_labels,ax=ax)
nx.draw_networkx_edges(G,pos,ax=ax)
if edge_labels:
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels,ax=ax,bbox=dict(boxstyle="round", fc="0.8", ec="0.5", alpha=0.75))