|
5 | 5 | from scipy import stats
|
6 | 6 | import moss
|
7 | 7 |
|
8 |
| -f = plt.figure(figsize=(10, 12)) |
9 |
| -gs = plt.GridSpec(6, 2) |
10 |
| -np.random.seed(0) |
| 8 | +def lmplot(ax): |
11 | 9 |
|
12 |
| -# Linear regression |
13 |
| -# ----------------- |
| 10 | + n = 80 |
| 11 | + c = "#222222" |
| 12 | + rs = np.random.RandomState(5) |
| 13 | + x = rs.normal(4, 1, n) |
| 14 | + y = 2 + 1.5 * x + rs.normal(0, 3, n) |
| 15 | + ax.plot(x, y, "o", c=c, alpha=.8) |
14 | 16 |
|
15 |
| -ax = plt.subplot(gs[:2, 0]) |
16 |
| -plt.title("lmplot()") |
| 17 | + xx = np.linspace(1 + 1e-9, 7 - 1e-9, 100) |
| 18 | + lmpred = lambda x, y: np.polyval(np.polyfit(x, y, 1), xx) |
| 19 | + yy = lmpred(x, y) |
| 20 | + ax.plot(xx, yy, c=c) |
| 21 | + boots = moss.bootstrap(x, y, func=lmpred, n_boot=100) |
| 22 | + ci = moss.percentiles(boots, [2.5, 97.5], 0) |
| 23 | + ax.fill_between(xx, *ci, alpha=.15, color=c) |
| 24 | + ax.set_title("lmplot()") |
17 | 25 |
|
18 |
| -n = 80 |
19 |
| -c = "#222222" |
20 |
| -rs = np.random.RandomState(5) |
21 |
| -x = rs.normal(4, 1, n) |
22 |
| -y = 2 + 1.5 * x + rs.normal(0, 3, n) |
23 |
| -ax.plot(x, y, "o", c=c, alpha=.8) |
24 | 26 |
|
25 |
| -xx = np.linspace(1, 7, 100) |
26 |
| -lmpred = lambda x, y: np.polyval(np.polyfit(x, y, 1), xx) |
27 |
| -yy = lmpred(x, y) |
28 |
| -ax.plot(xx, yy, c=c) |
29 |
| -boots = moss.bootstrap(x, y, func=lmpred, n_boot=100) |
30 |
| -ci = moss.percentiles(boots, [2.5, 97.5], 0) |
31 |
| -ax.fill_between(xx, *ci, alpha=.15, color=c) |
| 27 | +def tsplot(ax): |
32 | 28 |
|
33 |
| -# Timeseries plot |
34 |
| -# --------------- |
| 29 | + n = 20 |
| 30 | + t = 10 |
| 31 | + ax.set_xlim(0, t) |
| 32 | + x = np.linspace(0, t, 100) |
| 33 | + s = np.array([stats.gamma.pdf(x, a) for a in [3, 5, 7]]) |
| 34 | + d = s[:, np.newaxis, :] |
| 35 | + rs = np.random.RandomState(24) |
35 | 36 |
|
36 |
| -ax = plt.subplot(gs[:2, 1]) |
37 |
| -plt.title("tsplot()") |
| 37 | + d = d * np.array([1, -1])[rs.binomial(1, .3, 3)][:, np.newaxis, np.newaxis] |
| 38 | + d = d + rs.normal(0, .15, (3, n))[:, :, np.newaxis] |
| 39 | + d = d + rs.uniform(0, .25, 3)[:, np.newaxis, np.newaxis] |
| 40 | + d *= 10 |
| 41 | + d = d.transpose((1, 2, 0)) |
38 | 42 |
|
39 |
| -n = 20 |
40 |
| -t = 10 |
41 |
| -ax.set_xlim(0, t) |
42 |
| -x = np.linspace(0, t, 100) |
43 |
| -s = np.array([stats.gamma.pdf(x, a) for a in [3, 5, 7]]) |
44 |
| -d = s[:, np.newaxis, :] |
45 |
| -rs = np.random.RandomState(24) |
| 43 | + sns.tsplot(d, time=x, ax=ax) |
| 44 | + ax.set_title("tsplot()") |
46 | 45 |
|
47 |
| -d = d * np.array([1, -1])[rs.binomial(1, .3, 3)][:, np.newaxis, np.newaxis] |
48 |
| -d = d + rs.normal(0, .15, (3, n))[:, :, np.newaxis] |
49 |
| -d = d + rs.uniform(0, .25, 3)[:, np.newaxis, np.newaxis] |
50 |
| -d *= 10 |
51 |
| -d = d.transpose((1, 2, 0)) |
52 | 46 |
|
53 |
| -sns.tsplot(d, time=x, ax=ax) |
| 47 | +def violinplot(ax): |
54 | 48 |
|
55 |
| -# Violin plots |
56 |
| -# ------------ |
| 49 | + n = 40 |
| 50 | + p = 8 |
| 51 | + rs = np.random.RandomState(8) |
| 52 | + d = rs.normal(0, 1, (n, p)) |
| 53 | + d += np.log(np.arange(1, p + 1)) * -5 + 10 |
57 | 54 |
|
58 |
| -sns.set(style="whitegrid") |
| 55 | + sns.violinplot(d, inner="points", ax=ax) |
| 56 | + ax.set_title("violinplot()") |
59 | 57 |
|
60 |
| -ax = plt.subplot(gs[2:4, 0]) |
61 |
| -plt.title("violinplot()") |
62 | 58 |
|
63 |
| -n = 40 |
64 |
| -p = 8 |
65 |
| -rs = np.random.RandomState(8) |
66 |
| -d = rs.normal(0, 1, (n, p)) |
67 |
| -d += np.log(np.arange(1, p + 1)) * -5 + 10 |
| 59 | +def interactplot(ax): |
68 | 60 |
|
69 |
| -sns.violinplot(d, inner="points") |
| 61 | + rs = np.random.RandomState(11) |
70 | 62 |
|
71 |
| -#sns.despine(ax=ax) |
| 63 | + n = 80 |
| 64 | + x1 = rs.randn(n) |
| 65 | + x2 = x1 / 5 + rs.randn(n) |
| 66 | + b0, b1, b2, b3 = 1.5, 4, -1, 3 |
| 67 | + y = b0 + b1 * x1 + b2 * x2 + b3 * x1 * x2 + rs.randn(n) |
72 | 68 |
|
73 |
| -# Continuous interaction |
74 |
| -# ---------------------- |
| 69 | + sns.interactplot(x1, x2, y, colorbar=False, ax=ax) |
| 70 | + ax.set_title("interactplot()") |
75 | 71 |
|
76 |
| -sns.set(style="darkgrid") |
77 | 72 |
|
78 |
| -ax = plt.subplot(gs[2:4, 1]) |
79 |
| -plt.title("interactplot()") |
| 73 | +def corrplot(ax): |
80 | 74 |
|
81 |
| -rs = np.random.RandomState(11) |
| 75 | + rs = np.random.RandomState(0) |
| 76 | + x0, x1 = rs.randn(2, 60) |
| 77 | + x2, x3 = rs.multivariate_normal([0, 0], [(1, -.5), (-.5, 1)], 60).T |
| 78 | + x2 += x0 / 8 |
| 79 | + x4 = x1 + rs.randn(60) * 2 |
| 80 | + data = np.c_[x0, x1, x2, x3, x4] |
82 | 81 |
|
83 |
| -n = 80 |
84 |
| -x1 = rs.randn(n) |
85 |
| -x2 = x1 / 5 + rs.randn(n) |
86 |
| -b0, b1, b2, b3 = 1.5, 4, -1, 3 |
87 |
| -y = b0 + b1 * x1 + b2 * x2 + b3 * x1 * x2 + rs.randn(n) |
| 82 | + sns.corrplot(data, ax=ax) |
| 83 | + ax.set_title("corrplot()", verticalalignment="top") |
88 | 84 |
|
89 |
| -sns.interactplot(x1, x2, y, colorbar=False, ax=ax) |
90 | 85 |
|
| 86 | +def distplot_hist(ax): |
91 | 87 |
|
92 |
| -# Correlation matrix |
93 |
| -# ------------------ |
| 88 | + ax.set_xlim(0, 1) |
| 89 | + ax.set_xticklabels([]) |
94 | 90 |
|
95 |
| -ax = plt.subplot(gs[4:, 0]) |
| 91 | + g = sns.color_palette("Set2", desat=.75)[0] |
| 92 | + n = 1000 |
| 93 | + rs = np.random.RandomState(0) |
| 94 | + d = rs.beta(8, 13, n) |
96 | 95 |
|
97 |
| -rs = np.random.RandomState(0) |
98 |
| -x0, x1 = rs.randn(2, 60) |
99 |
| -x2, x3 = rs.multivariate_normal([0, 0], [(1, -.5), (-.5, 1)], 60).T |
100 |
| -x2 += x0 / 8 |
101 |
| -x4 = x1 + rs.randn(60) * 2 |
102 |
| -data = np.c_[x0, x1, x2, x3, x4] |
| 96 | + sns.distplot(d, color=g, ax=ax) |
| 97 | + sns.despine(ax=ax) |
| 98 | + ax.set_title("distplot()") |
103 | 99 |
|
104 |
| -sns.corrplot(data, ax=ax) |
105 |
| -ax.set_title("corrplot()", verticalalignment="top") |
106 | 100 |
|
| 101 | +def distplot_kde(ax): |
107 | 102 |
|
108 |
| -# Beta distributions |
109 |
| -# ------------------ |
| 103 | + ax.set_xlim(0, 1) |
110 | 104 |
|
111 |
| -sns.set(style="nogrid") |
| 105 | + p = sns.color_palette("Set2", desat=.75)[2] |
| 106 | + n = 80 |
| 107 | + rs = np.random.RandomState(0) |
| 108 | + d = rs.beta(50, 25, n) |
112 | 109 |
|
113 |
| -ax = plt.subplot(gs[4, 1]) |
114 |
| -plt.title("distplot()") |
115 |
| -plt.xlim(0, 1) |
116 |
| -ax.set_xticklabels([]) |
| 110 | + sns.distplot(d, hist=False, rug=True, color=p, |
| 111 | + kde_kws=dict(shade=True), ax=ax) |
| 112 | + sns.despine(ax=ax) |
117 | 113 |
|
118 |
| -g, _, p = sns.color_palette("Set2", 3, desat=.75) |
119 |
| -n = 1000 |
120 |
| -rs = np.random.RandomState(0) |
121 |
| -d = rs.beta(8, 13, n) |
122 | 114 |
|
123 |
| -sns.distplot(d, color=g) |
124 |
| -sns.despine(ax=ax) |
| 115 | +if __name__ == "__main__": |
125 | 116 |
|
126 |
| -ax = plt.subplot(gs[5, 1], sharey=ax) |
127 |
| -plt.xlim(0, 1) |
| 117 | + f = plt.figure(figsize=(10, 12)) |
| 118 | + gs = plt.GridSpec(6, 2) |
| 119 | + np.random.seed(0) |
128 | 120 |
|
129 |
| -d = rs.beta(50, 25, n / 15) |
| 121 | + # Linear regression |
| 122 | + ax = plt.subplot(gs[:2, 0]) |
| 123 | + lmplot(ax) |
130 | 124 |
|
131 |
| -sns.distplot(d, hist=False, rug=True, color=p, kde_kws=dict(shade=True)) |
132 |
| -sns.despine(ax=ax) |
| 125 | + # Timeseries plot |
| 126 | + ax = plt.subplot(gs[:2, 1]) |
| 127 | + tsplot(ax) |
133 | 128 |
|
| 129 | + # Violin plots |
| 130 | + sns.set(style="whitegrid") |
| 131 | + ax = plt.subplot(gs[2:4, 0]) |
| 132 | + violinplot(ax) |
134 | 133 |
|
135 |
| -# Save the plot |
136 |
| -# ------------- |
| 134 | + # Continuous interaction |
| 135 | + sns.set(style="darkgrid") |
| 136 | + ax = plt.subplot(gs[2:4, 1]) |
| 137 | + interactplot(ax) |
137 | 138 |
|
138 |
| -f.tight_layout() |
139 |
| -f.savefig("%s/example_plot.png" % os.path.dirname(__file__)) |
| 139 | + # Correlation matrix |
| 140 | + ax = plt.subplot(gs[4:, 0]) |
| 141 | + corrplot(ax) |
| 142 | + |
| 143 | + # Beta distributions |
| 144 | + sns.set(style="nogrid") |
| 145 | + ax = plt.subplot(gs[4, 1]) |
| 146 | + distplot_hist(ax) |
| 147 | + |
| 148 | + ax = plt.subplot(gs[5, 1], sharey=ax) |
| 149 | + distplot_kde(ax) |
| 150 | + |
| 151 | + # Save the plot |
| 152 | + f.tight_layout() |
| 153 | + png_template = os.path.dirname(__file__) + "/%s.png" |
| 154 | + f.savefig(png_template % "example_plot") |
| 155 | + |
| 156 | + # Carousel images |
| 157 | + plots = ["lmplot", "tsplot", "violinplot", "interactplot", |
| 158 | + "corrplot", "distplot"] |
| 159 | + styles = {p: "darkgrid" for p in plots} |
| 160 | + styles["violinplot"] = "whitegrid" |
| 161 | + styles["distplot"] = "nogrid" |
| 162 | + |
| 163 | + figsize = (6, 4.2) |
| 164 | + for plot in plots: |
| 165 | + sns.set_axes_style(styles[plot], "notebook") |
| 166 | + |
| 167 | + if plot == "distplot": |
| 168 | + f, (ax1, ax2) = plt.subplots(2, 1, sharey=True, figsize=figsize) |
| 169 | + distplot_hist(ax1) |
| 170 | + distplot_kde(ax2) |
| 171 | + else: |
| 172 | + plot_func = locals()[plot] |
| 173 | + f, ax = plt.subplots(figsize=figsize) |
| 174 | + plot_func(ax) |
| 175 | + |
| 176 | + f.tight_layout() |
| 177 | + f.savefig(png_template % plot) |
0 commit comments