8000 Apply some improvements concerning style and conventions. First three… · matplotlib/matplotlib@c845c88 · GitHub
[go: up one dir, main page]

Skip to content

Commit c845c88

Browse files
author
Carsten
committed
Apply some improvements concerning style and conventions. First three plots are closely related and thus go into one figure.
1 parent 701e171 commit c845c88

File tree

1 file changed

+36
-80
lines changed

1 file changed

+36
-80
lines changed

examples/statistics/confidence_ellipse.py

Lines changed: 36 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ def confidence_ellipse(x, y, ax, n_std=3.0, **kwargs):
3939
Parameters
4040
----------
4141
x, y : array_like, shape (n, )
42-
Input data
42+
Input data.
4343
44-
ax : matplotlib.axes object to the ellipse into
44+
ax : matplotlib.axes.Axes
45+
The axes object to draw the ellipse into.
4546
46-
n_std : number of standard deviations to determine the ellipse's radiuses
47+
n_std : float
48+
The number of standard deviations to determine the ellipse's radiuses.
4749
4850
Returns
4951
-------
50-
None
52+
matplotlib.patches.Ellipse
5153
5254
Other parameters
5355
----------------
@@ -83,7 +85,7 @@ def confidence_ellipse(x, y, ax, n_std=3.0, **kwargs):
8385
.translate(mean_x, mean_y)
8486

8587
ellipse.set_transform(transf + ax.transData)
86-
ax.add_patch(ellipse)
88+
return ax.add_patch(ellipse)
8789

8890

8991
#############################################################################
@@ -107,91 +109,45 @@ def get_correlated_dataset(n, dependency, mu, scale):
107109

108110
#############################################################################
109111
#
110-
# Positive correlation
111-
# """"""""""""""""""""
112-
#
113-
114-
fig, ax_pos = plt.subplots(figsize=(6, 6))
115-
np.random.seed(1234)
116-
117-
dependency_pos = np.array([
118-
[0.85, 0.35],
119-
[0.15, -0.65]
120-
])
121-
mu = np.array([2, 4]).T
122-
scale = np.array([3, 5]).T
123-
124-
# Indicate the x- and y-axis
125-
ax_pos.axvline(c='grey', lw=1)
126-
ax_pos.axhline(c='grey', lw=1)
127-
128-
x, y = get_correlated_dataset(500, dependency_pos, mu, scale)
129-
confidence_ellipse(x, y, ax_pos, facecolor='none', edgecolor='red')
130-
131-
# Also plot the dataset itself, for reference
132-
ax_pos.scatter(x, y, s=0.5)
133-
# Mark the mean ("mu")
134-
ax_pos.scatter([mu[0]], [mu[1]], c='red', s=3)
135-
ax_pos.set_title(f'Positi 8000 ve correlation')
136-
plt.show()
137-
138-
139-
#############################################################################
140-
#
141-
# Negative correlation
142-
# """"""""""""""""""""
112+
# Positive, negative and weak correlation
113+
# """""""""""""""""""""""""""""""""""""""
143114
#
115+
# Note that the shape for the weak correlation (right) is an ellipse,
116+
# not a circle because x and y are differently scaled.
117+
# However, the fact that x and y are uncorrelated is shown by
118+
# the axes of the ellipse being aligned with the x- and y-axis
119+
# of the coordinate system.
144120

145-
fig, ax_neg = plt.subplots(figsize=(6, 6))
146-
dependency_neg = np.array([
147-
[0.9, -0.4],
148-
[0.1, -0.6]
149-
])
121+
np.random.seed(0)
122+
123+
PARAMETERS = {
124+
'Positive correlation': np.array([[0.85, 0.35],
125+
[0.15, -0.65]]),
126+
'Negative correlation': np.array([[0.9, -0.4],
127+
[0.1, -0.6]]),
128+
'Weak correlation': np.array([[1, 0],
129+
[0, 1]]),
130+
}
131+
150132
mu = np.array([2, 4]).T
151133
scale = np.array([3, 5]).T
152134

153-
ax_neg.axvline(c='grey', lw=1)
154-
ax_neg.axhline(c='grey', lw=1)
135+
fig, axs = plt.subplots(1, 3, figsize=(9, 3))
136+
for ax, (title, dependency) in zip(axs, PARAMETERS.items()):
137+
x, y = get_correlated_dataset(800, dependency, mu, scale)
138+
ax.scatter(x, y, s=0.5)
139+
140+
ax.axvline(c='grey', lw=1)
141+
67E6 ax.axhline(c='grey', lw=1)
155142

156-
x, y = get_correlated_dataset(500, dependency_neg, mu, scale)
157-
confidence_ellipse(x, y, ax_neg, facecolor='none', edgecolor='red')
143+
confidence_ellipse(x, y, ax, facecolor='none', edgecolor='red')
158144

159-
ax_neg.scatter(x, y, s=0.5)
160-
161-
ax_neg.scatter([mu[0]], [mu[1]], c='red', s=3)
162-
ax_neg.set_title(f'Negative correlation')
145+
ax.scatter([mu[0]], [mu[1]], c='red', s=3)
146+
ax.set_title(title)
147+
163148
plt.show()
164149

165150

166-
#############################################################################
167-
#
168-
# Weak correlation
169-
# """"""""""""""""
170-
#
171-
# This is still an ellipse, not a circle because x and y
172-
# are differently scaled. However, the fact that x and y are uncorrelated
173-
# is shown by the axes of the ellipse being aligned with the x- and y-axis
174-
# of the coordinate system.
175-
176-
fig, ax_uncorrel = plt.subplots(figsize=(6, 6))
177-
178-
in_dependency = np.array([
179-
[1, 0],
180-
[0, 1]
181-
])
182-
mu = np.array([2, 4]).T
183-
scale = np.array([5, 3]).T
184-
185-
ax_uncorrel.axvline(c='grey', lw=1)
186-
ax_uncorrel.axhline(c='grey', lw=1)
187-
188-
x, y = get_correlated_dataset(500, in_dependency, mu, scale)
189-
confidence_ellipse(x, y, ax_uncorrel, facecolor='none', edgecolor='red')
190-
ax_uncorrel.scatter(x, y, s=0.5)
191-
ax_uncorrel.scatter([mu[0]], [mu[1]], c='red', s=3)
192-
ax_uncorrel.set_title(f'Weak correlation')
193-
194-
195151
#############################################################################
196152
#
197153
# Different number of standard deviations

0 commit comments

Comments
 (0)
0