-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
132 lines (106 loc) · 4.33 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
"""
:mod:`plot` -- basic plotting routines for extremes
===================================================
.. module:: plot
:synopsis: Provide some basic plotting routines for extreme value
distribution modelling.
.. moduleauthor:: Craig Arthur <craig.arthur@ga.gov.au>
"""
from __future__ import division
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import genpareto
import statsmodels.api as sm
import logging
LOG = logging.getLogger(__name__)
from return_period import returnLevels, empiricalReturnPeriod, \
returnPeriodUncertainty
from distributions import fittedPDF
sns.set_context("poster")
sns.set_style("ticks")
def plotFit(data, mu, xi, sigma, title, figfile):
"""
Plot a fitted distribution, with approximate 90% confidence interval
and empirical return period values.
:param data: :class:`numpy.ndarray` of observed data values.
:param float mu: Selected threshold value.
:param float xi: Fitted shape parameter.
:param float sigma: Fitted scale parameter.
:param str title: Title string for the plot.
:param str figfile: Path to store the file (includes image format)
"""
LOG.info("Plotting fitted return period curve")
rp = np.array([1, 2, 5, 10, 20, 50, 100, 200,
500, 1000, 2000, 5000, 10000])
rate = float(len(data[data > mu])) / float(len(data))
rval = returnLevels(rp, mu, xi, sigma, rate)
emprp = empiricalReturnPeriod(data)
err = returnPeriodUncertainty(data, mu, xi, sigma, rp)
sortedmax = np.sort(data)
fig, ax1 = plt.subplots(1, 1, figsize=(12, 12))
ax1.semilogx(rp, rval, label="Fitted RP curve")
ax1.semilogx(rp, rval + 1.96 * err, label="90% CI",
linestyle='--', color='0.5')
ax1.semilogx(rp, rval - 1.96 * err, linestyle='--', color='0.5')
ax1.scatter(emprp[emprp > 1], sortedmax[emprp > 1], s=100,
color='r', label="Empirical RP")
title_str = (title + "\n" +
r"$\mu$ = {0:.2f}, $\xi$ = {1:.5f}, $\sigma$ = {2:.4f}".
format(mu, xi, sigma))
ax1.set_title(title_str)
ax1.legend(loc=2)
ax1.set_ylim((0, 100))
ax1.set_xlim((1, 10000))
ax1.set_ylabel('Wind speed (m/s)')
ax1.set_xlabel('Return period (years)')
ax1.grid(which='major')
ax1.grid(which='minor', linestyle='--', linewidth=1)
plt.savefig(figfile)
plt.close()
def plotDiagnostics(data, mu, xi, sigma, figfile):
"""
Create a 4-panel diagnostics plot of the fitted distribution.
:param data: :class:`numpy.ndarray` of observed data values (in units
of metres/second).
:param float mu: Selected threshold value.
:param float xi: Fitted shape parameter.
:param float sigma: Fitted scale parameter.
:param str figfile: Path to store the file (includes image format)
"""
LOG.info("Plotting diagnostics")
fig, ax = plt.subplots(2, 2)
axes = ax.flatten()
# Probability plots
sortedmax = np.sort(data[data > mu])
gpdf = fittedPDF(data, mu, xi, sigma)
pp_x = sm.ProbPlot(sortedmax)
pp_x.ppplot(xlabel="Empirical", ylabel="Model", ax=axes[0], line='45')
axes[0].set_title("Probability plot")
prplot = sm.ProbPlot(sortedmax, genpareto, distargs=(xi,),
loc=mu, scale=sigma)
prplot.qqplot(xlabel="Model", ylabel="Empirical", ax=axes[1], line='45')
axes[1].set_title("Quantile plot")
ax2 = axes[2]
rp = np.array([1, 2, 5, 10, 20, 50, 100, 200,
500, 1000, 2000, 5000, 10000])
rate = float(len(sortedmax)) / float(len(data))
rval = returnLevels(rp, mu, xi, sigma, rate)
emprp = empiricalReturnPeriod(np.sort(data))
ax2.semilogx(rp, rval, label="Fitted RP curve", color='r')
ax2.scatter(emprp[emprp > 1], np.sort(data)[emprp > 1],
color='b', label="Empirical RP", s=100)
ax2.legend(loc=2)
ax2.set_xlabel("Return period")
ax2.set_ylabel("Return level")
ax2.set_title("Return level plot")
ax2.grid(True)
maxbin = 4 * np.ceil(np.floor(data.max() / 4) + 1)
sns.distplot(sortedmax, bins=np.arange(mu, maxbin, 2),
hist=True, axlabel='Wind speed (m/s)',
ax=axes[3])
axes[3].plot(sortedmax, gpdf, color='r')
axes[3].set_title("Density plot")
plt.tight_layout()
plt.savefig(figfile)
plt.close()