def draw_visa(base_line, up_base_line, start, duration, color, text): rect_w = duration * 0.1 rect_h = 0.08 x = start * 0.1 + rect_w / 2 if up_base_line: y = base_line - 0.1 else: y = base_line - 0.15 rect = Ellipse((x, y), rect_w, rect_h, alpha=1, facecolor=color) cx, cy = rect.get_center() currentAxis.annotate(str(start), (cx, cy), color='w', weight='bold', fontsize=6, ha='center', va='center') currentAxis.add_patch(rect)
def test_corner_center(): loc = [10, 20] width = 1 height = 2 # Rectangle # No rotation corners = ((10, 20), (11, 20), (11, 22), (10, 22)) rect = Rectangle(loc, width, height) assert_array_equal(rect.get_corners(), corners) assert_array_equal(rect.get_center(), (10.5, 21)) # 90 deg rotation corners_rot = ((10, 20), (10, 21), (8, 21), (8, 20)) rect.set_angle(90) assert_array_equal(rect.get_corners(), corners_rot) assert_array_equal(rect.get_center(), (9, 20.5)) # Rotation not a multiple of 90 deg theta = 33 t = mtransforms.Affine2D().rotate_around(*loc, np.deg2rad(theta)) corners_rot = t.transform(corners) rect.set_angle(theta) assert_almost_equal(rect.get_corners(), corners_rot) # Ellipse loc = [loc[0] + width / 2, loc[1] + height / 2] ellipse = Ellipse(loc, width, height) # No rotation assert_array_equal(ellipse.get_corners(), corners) # 90 deg rotation corners_rot = ((11.5, 20.5), (11.5, 21.5), (9.5, 21.5), (9.5, 20.5)) ellipse.set_angle(90) assert_array_equal(ellipse.get_corners(), corners_rot) # Rotation shouldn't change ellipse center assert_array_equal(ellipse.get_center(), loc) # Rotation not a multiple of 90 deg theta = 33 t = mtransforms.Affine2D().rotate_around(*loc, np.deg2rad(theta)) corners_rot = t.transform(corners) ellipse.set_angle(theta) assert_almost_equal(ellipse.get_corners(), corners_rot)
def confidence_ellipse(self, x, y, ax, n_std=1, facecolor='none', **kwargs): """ Create a plot of the covariance confidence ellipse of *x* and *y*. Parameters ---------- x, y : array-like, shape (n, ) Input data. ax : matplotlib.axes.Axes The axes object to draw the ellipse into. n_std : float The number of standard deviations to determine the ellipse's radiuses. Returns ------- matplotlib.patches.Ellipse Other parameters ---------------- kwargs : `~matplotlib.patches.Patch` properties """ if x.size != y.size: raise ValueError("x and y must be the same size") cov = np.cov(x, y) pearson = cov[0, 1] / np.sqrt(cov[0, 0] * cov[1, 1]) # Using a special case to obtain the eigenvalues of this # two-dimensionl dataset. ell_radius_x = np.sqrt(1 + pearson) ell_radius_y = np.sqrt(1 - pearson) ellipse = Ellipse((0, 0), width=ell_radius_x * 2, height=ell_radius_y * 2, facecolor=facecolor, **kwargs) self.g_ell_center = ellipse.get_center() self.g_ell_width = ell_radius_x * 2 self.g_ell_height = ell_radius_y * 2 self.angle = 45 # Calculating the stdandard deviation of x from # the squareroot of the variance and multiplying # with the given number of standard deviations. scale_x = np.sqrt(cov[0, 0]) * n_std mean_x = np.mean(x) # calculating the stdandard deviation of y ... scale_y = np.sqrt(cov[1, 1]) * n_std mean_y = np.mean(y) transf = transforms.Affine2D() \ .rotate_deg(45) \ .scale(scale_x, scale_y) \ .translate(mean_x, mean_y) ellipse.set_transform(transf + ax.transData) self.ellipse = ellipse return ax.add_patch(ellipse)