泰勒图

​ 目前可以用SkillMetrics这个库中的相关函数来快速实现泰勒图的绘制。虽然总体效果还是差强人意,美感比NCL差不少,但好在简便可以快速出图了。

可以通过

1
pip3 install SkillMetrics

来安装这个库。

直接上绘图代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import skill_metrics as sm 
import matplotlib.pyplot as plt
# 提供数据,SD标准差(也可以是相对标准差),CC相关系数,rmsd均方根误差。 存放顺序分别为(观测,模式1,模式2,模式3...)所以SD和CC的第一个数值是1。
sd = np.array([1,0.82,1.12,1.48,0.65,0.8])
cc = np.array([1,0.48,0.36,0.74,0.54,0.61])
rmsd = np.array([0,1.68,1.57,1.92,2.34,2.18])
label=['obs','model_1','model_2','model_3','model_4','model_5']

fig = plt.figure(figsize=(12,8))
ax = fig.add_axes([0.1, 0.6, 0.6, 0.6])
# 绘图核心函数
sm.taylor_diagram(sd,rmsd,cc,markerLabel = label,markercolor="k",markerSize=5,markerLegend = 'on',#基本参数
colCOR="k",styleCOR="--",widthCOR=.4, #CC相关设置
colSTD="k",widthSTD=.9,styleSTD="--",axismax=1.7, #SD相关设置
widthRMS=0.5,labelRMS='',colRMS='k', #RMSD相关设置
colOBS="r",styleOBS="-",widthOBS=1,markerObs="",titleOBS="Observation", #观测值设置
)

ax.grid(False)
plt.show()

图形输出:

image-20200523154901802

以下是sm.taylor_diagram函数所支持的参数:

基础参数

参数 说明
alpha Blending of symbol face color (0.0 transparent through 1.0 opaque) (Default: 1.0)
axisMax Maximum for the radial contours
colFrame Color for the y and x spines
colorMap ‘on’ / ‘off’ (default): Switch to map color shading of markers to colormap (“on”) or min to max range of RMSDz values (“off”). Set to same value as option “nonRMSDz”.
labelWeight Weight of the x & y axis labels
numberPanels 1 or 2: Panels to display (1 for positive correlations, 2 for positive and negative correlations). Default value depends on correlations (CORs).
overlay ‘on’ / ‘off’ (default): Switch to overlay current statistics on Taylor diagram. Only markers will be displayed.

当ColorMap = ‘on’ 时:

参数 说明
cmap Choice of colormap. (Default: ‘jet’)
cmap_marker Marker to use with colormap (Default: ‘d’)
cmap_vmax Maximum range of colormap (Default: None)
cmap_vmin Minimum range of colormap (Default: None)

Marker 设置

参数 说明
MarkerDisplayed ‘marker’ (default): Experiments are represented by individual symbols
‘colorBar’: Experiments are represented by a color described in a colorbar

当 MarkerDisplayed = ‘marker’

参数 说明
markerColor Single color to use for all markers
markerColors Dictionary with up to two colors as keys (‘face’, ‘edge’) to use for all markers when ‘markerlegend’ == ‘off’ or None. If None or ‘markerlegend’ == ‘on’, then uses only the value of ‘markercolor’. (Default: None)
markerLabel Labels for markers
markerLabelColor Marker label color (Default: black)
markerLayout Matrix layout for markers in legend [nrow, ncolumn], e.g. [4,None] for 4 rows and [None,3] for 3 columns. (Default [15, no. markers/15] )
markerLegend ‘on’ / ‘off’ (default): Use legend for markers
markers Dictionary providing individual control of the marker label, label color, symbol, size, face color, and edge color (Default: None)
markerSize Marker size (Default: 10)
markerSymbol Marker symbol (Default: ‘o’)

当MarkerDisplayed = ‘colorbar’时:

参数 说明
cmapZData Data values to use for color mapping of markers, e.g. RMSD or BIAS (Default empty). Used to make range of values appear beside color bar.
locationColorBar Location for the colorbar, ‘NorthOutside’ or ‘EastOutside’.
titleColorBar Title of the colorbar

RMSD的相关设置

参数 说明
colRMS RMS grid and tick labels color. (Default: green)
labelRMS RMS axis label (Default: ‘RMSD’)
rincRMS Axis tick increment for RMS values
rmsLabelFormat String format for RMS contour labels, e.g. ‘0:.2f’. (Default ‘0’, format as specified by str function.)
showlabelsRMS ‘on’ / ‘off’ (default): Show the RMS tick labels
styleRMS Line style of the RMS grid
tickRMS RMS values to plot gridding circles from observation point
tickRMSangle Angle for RMS tick labels with the observation point. (Default: 135 deg.)
titleRMS ‘on’ / ‘off’ (default): Show RMSD axis title
titleRMSDangle Angle at which to display the ‘RMSD’ label for the RMS contours (Default: 160 degrees)
widthRMS Line width of the RMS grid

标准差的相关设置

colSTD STD grid and tick labels color. (Default: black)
colsSTD STD dictionary of grid colors with: ‘grid’, ‘tick_labels’, ‘title’ keys/values. If not provided or None, considers the monotonic ‘colSTD’ argument. (“Default: None”)
rincSTD Axis tick increment for STD values
showlabelsSTD ‘on’ / ‘off’ (default): Show the STD tick labels
styleSTD Line style of the STD grid
tickSTD STD values to plot gridding circles from observation point
titleSTD ‘on’ / ‘off’ (default): Show STD axis title
widthSTD Line width of the STD grid

相关系数的相关设置

参数 说明
colCOR CORRELATION grid color. (Default: blue)
colsCOR CORRELATION dictionary of grid colors with: ‘grid’, ‘tick_labels’, ‘title’ keys/values. If not provided or None, considers the monotonic ‘colSTD’ argument. (“Default: None”)
showlabelsCOR ‘on’ (default) / ‘off’: Show the CORRELATION tick labels
styleCOR Line style of the CORRELATION grid
tickCOR[panel] Tick values for correlation coefficients for two types of panelsCOR grid values
titleCOR ‘on’ / ‘off’ (default): Show CORRELATION axis title
titleCORshape The shape of the label ‘correlation coefficient’. Accepted values are ‘curved’ or ‘linear’. (Default: ‘curved’)
widthCOR Line width of the COR grid

观测点设置

参数 说明
colOBS Observation STD color. (Default: magenta)
markerObs Marker to use for x-axis indicating observed STD.A choice of ‘None’ will suppress appearance of marker. (Default None)
styleOBS Line style for observation grid line. A choice of empty string (‘’) will suppress appearance of the grid line. (Default: ‘’)
titleOBS Label for observation STD point on axis
widthOBS Line width of the observation STD circle

—————————————————以下是老版本,不删了留个记忆————————————————————

泰勒图绘制的核心思想是设计一个只有第一象限的极坐标,并将方差,相关系数进行捆绑,通过转化为极坐标系坐标进行绘制。为了实现泰勒图的绘制,设计了两个函数:

set_tayloraxes(fig, location=111) 和plot_taylor(axes, refsample, sample, args, *kwargs)

set_tayloraxes()函数用于建立一个泰勒图的坐标系,这个自定义函数一般情况下不建议修改,每一个参数都是经过多次调试得到的,很可能牵一发动全身。因此,将绘图部分的独立成为了plot_taylor函数(),这部分函数较为简单,目的就是将需要绘图的数据,转换为极坐标系坐标,通过plot函数将散点打在泰勒图上,这个函数模块较为简单,可以根据自己的输入数据情况进行调整。

下面直接给出两个函数的完整代码:

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
from matplotlib.projections import PolarAxes
from mpl_toolkits.axisartist import floating_axes
from mpl_toolkits.axisartist import grid_finder
import numpy as np
import matplotlib.pyplot as plt

def set_tayloraxes(fig, location=111):
trans = PolarAxes.PolarTransform()
r1_locs = np.hstack((np.arange(1,10)/10.0,[0.95,0.99]))
t1_locs = np.arccos(r1_locs)
gl1 = grid_finder.FixedLocator(t1_locs)
tf1 = grid_finder.DictFormatter(dict(zip(t1_locs, map(str,r1_locs))))
r2_locs = np.arange(0,2,0.25)
r2_labels = ['0 ', '0.25 ', '0.50 ', '0.75 ', 'REF ', '1.25 ', '1.50 ', '1.75 ']
gl2 = grid_finder.FixedLocator(r2_locs)
tf2 = grid_finder.DictFormatter(dict(zip(r2_locs, map(str,r2_labels))))
ghelper = floating_axes.GridHelperCurveLinear(trans,extremes=(0,np.pi/2,0,1.75),
grid_locator1=gl1,tick_formatter1=tf1,
grid_locator2=gl2,tick_formatter2=tf2)
ax = floating_axes.FloatingSubplot(fig, location, grid_helper=ghelper)
fig.add_subplot(ax)
ax.axis["top"].set_axis_direction("bottom")
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")
ax.axis["top"].label.set_text("Correlation")
ax.axis["left"].set_axis_direction("bottom")
ax.axis["left"].label.set_text("Standard deviation")
ax.axis["right"].set_axis_direction("top")
ax.axis["right"].toggle(ticklabels=True)
ax.axis["right"].major_ticklabels.set_axis_direction("left")
ax.axis["bottom"].set_visible(False)
ax.grid()
polar_ax = ax.get_aux_axes(trans)
t = np.linspace(0,np.pi/2)
r = np.zeros_like(t) + 1
polar_ax.plot(t,r,'k--')
polar_ax.text(np.pi/2+0.042,1.03, " 1.00", size=10.5,ha="right", va="top",
bbox=dict(boxstyle="square",ec='w',fc='w'))
return polar_ax

def plot_taylor(axes, refsample, sample, *args, **kwargs):
std = np.std(sample)
corr = np.corrcoef(refsample, sample)
theta = np.arccos(corr[0,1])
t,r = theta,std
d = axes.plot(t,r, *args, **kwargs)
return d

下面介绍下函数的具体用法:

1
setup_axes(fig, rect=111)

输入:

fig: 需要绘图的figure

rect:图的位置,如111为1行1列第一个,122为1行2列第2个

输出:

polar_ax:泰勒坐标系

1
plot_taylor(axes, refsample, sample, *args, **kwargs)

输入:

axes : setup_axes返回的泰勒坐标系

refsample :参照样本

sample :评估样本

args, *kwargs :plt.plot()函数的相关参数,设置点的颜色,形状等等。

下面给出示例:

1
2
3
4
5
6
7
8
9
10
11
x = np.linspace(0,10*np.pi,100)
data = np.sin(x)
m1 = data + 0.4*np.random.randn(len(x))
m2 = 0.3*data + 0.6*np.random.randn(len(x))
m3 = np.sin(x-np.pi/10)
fig = plt.figure(figsize=(10,4))
ax1 = set_tayloraxes(fig, 121)
ax2 = set_tayloraxes(fig, 122)
d1 = plot_taylor(ax2,data,m1, 'bo')
d2 = plot_taylor(ax2,data,m2, 'ro')
d3 = plot_taylor(ax2,data,m3, 'go')

image-20200523154901802