@@ -39,15 +39,17 @@ def confidence_ellipse(x, y, ax, n_std=3.0, **kwargs):
39
39
Parameters
40
40
----------
41
41
x, y : array_like, shape (n, )
42
- Input data
42
+ Input data.
43
43
44
- ax : matplotlib.axes object to the ellipse into
44
+ ax : matplotlib.axes.Axes
45
+ The axes object to draw the ellipse into.
45
46
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.
47
49
48
50
Returns
49
51
-------
50
- None
52
+ matplotlib.patches.Ellipse
51
53
52
54
Other parameters
53
55
----------------
@@ -83,7 +85,7 @@ def confidence_ellipse(x, y, ax, n_std=3.0, **kwargs):
83
85
.translate (mean_x , mean_y )
84
86
85
87
ellipse .set_transform (transf + ax .transData )
86
- ax .add_patch (ellipse )
88
+ return ax .add_patch (ellipse )
87
89
88
90
89
91
#############################################################################
@@ -107,91 +109,45 @@ def get_correlated_dataset(n, dependency, mu, scale):
107
109
108
110
#############################################################################
109
111
#
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
+ # """""""""""""""""""""""""""""""""""""""
143
114
#
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.
144
120
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
+
150
132
mu = np .array ([2 , 4 ]).T
151
133
scale = np .array ([3 , 5 ]).T
152
134
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 )
155
142
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' )
158
144
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
+
163
148
plt .show ()
164
149
165
150
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
-
195
151
#############################################################################
196
152
#
197
153
# Different number of standard deviations
0 commit comments