-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtaylor.py
94 lines (82 loc) · 3.96 KB
/
taylor.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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.projections import PolarAxes
import mpl_toolkits.axisartist.grid_finder as gf
import mpl_toolkits.axisartist.floating_axes as fa
class TaylorDiagram(object):
def __init__(self, STD, fig=None, rect=111, label='_'):
self.STD = STD
tr = PolarAxes.PolarTransform()
# Correlation labels
rlocs = np.concatenate(((np.arange(11.0) / 10.0), [0.95, 0.99]))
tlocs = np.arccos(rlocs) # Conversion to polar angles
gl1 = gf.FixedLocator(tlocs) # Positions
tf1 = gf.DictFormatter(dict(zip(tlocs, map(str, rlocs))))
# Standard deviation axis extent
self.smin = 0
self.smax = 3.6 * 2
gh = fa.GridHelperCurveLinear(tr, extremes=(0, (np.pi / 2), self.smin, self.smax), grid_locator1=gl1,
tick_formatter1=tf1)
if fig is None:
fig = plt.figure()
ax = fa.FloatingSubplot(fig, rect, grid_helper=gh)
fig.add_subplot(ax)
# Angle axis
ax.axis['top'].set_axis_direction('bottom')
ax.axis['top'].label.set_text("Correlation coefficient")
ax.axis['top'].toggle(ticklabels=True, label=True)
ax.axis['top'].major_ticklabels.set_axis_direction('top')
ax.axis['top'].label.set_axis_direction('top')
# X axis
ax.axis['left'].set_axis_direction('bottom')
ax.axis['left'].label.set_text("Standard deviation")
ax.axis['left'].toggle(ticklabels=True, label=True)
ax.axis['left'].major_ticklabels.set_axis_direction('bottom')
ax.axis['left'].label.set_axis_direction('bottom')
# Y axis
ax.axis['right'].set_axis_direction('top')
ax.axis['right'].label.set_text("Standard deviation")
ax.axis['right'].toggle(ticklabels=True, label=True)
ax.axis['right'].major_ticklabels.set_axis_direction('left')
ax.axis['right'].label.set_axis_direction('top')
# Useless
ax.axis['bottom'].set_visible(False)
# Contours along standard deviations
ax.grid()
self._ax = ax # Graphical axes
self.ax = ax.get_aux_axes(tr) # Polar coordinates
# Add reference point and STD contour
l, = self.ax.plot([0], 3, 'k*', ls='', ms=12, label=label)
t = np.linspace(0, (np.pi / 2.0))
r = np.zeros_like(t) + 3
self.ax.plot(t, r, 'k--', label='_')
# Collect sample points for later use (e.g., legend)
self.samplePoints = [l]
def add_sample(self, STD, r, *args, **kwargs):
l, = self.ax.plot(np.arccos(r), STD, *args, **kwargs) # (theta, radius)
self.samplePoints.append(l)
return l
def add_contours(self, levels=5, **kwargs):
rs, ts = np.meshgrid(np.linspace(self.smin, self.smax), np.linspace(0, (np.pi / 2.0)))
RMSE = np.sqrt(np.power(self.STD, 2) + np.power(rs, 2) - (2.0 * self.STD * rs * np.cos(ts)))
contours = self.ax.contour(ts, rs, RMSE, levels, **kwargs)
return contours
def srl(obsSTD, s, r, l, fname):
plt.rcParams['font.family'] = "Times New Roman"
plt.rcParams['font.size'] = 12
fig = plt.figure(figsize=(8, 8),dpi=200)
dia = TaylorDiagram(obsSTD, fig=fig, rect=111, label='ref')
plt.clabel(dia.add_contours(colors='#808080'), inline=1, fontsize=10)
srlc = zip(s, r, l)
colors = ['red', 'blue', 'green', 'purple', 'orange', 'cyan', 'magenta', 'brown', 'lime', 'gray']
for i, (std, corr, label) in enumerate(srlc):
dia.add_sample(std, corr, label=label, marker='o', mec=colors[i], mfc='none', mew=1.6)
spl = [p.get_label() for p in dia.samplePoints]
fig.legend(dia.samplePoints, spl, numpoints=1, prop=dict(size='small'), loc=[0.7, 0.3])
plt.show()
obsSTD = 1
s = [0.568829 ,1.02795, 0.359491, 0.923264, 6.14735, 3.23984]
r = [0.823332767 ,0.794347551, 0.952444351 ,0.983533619 ,0.83546723, 0.939996293]
l = ["BR","AR", "XGB", "LR", "RF","DTR", "GBR"]
fname = 'TaylorDiagram.jpg'
srl(obsSTD, s, r, l, fname)