|
1 | 1 | """ |
2 | 2 | =================================== |
3 | | -Ellipse with orientation arrow Demo |
| 3 | +Ellipse with orientation arrow demo |
4 | 4 | =================================== |
5 | 5 |
|
6 | 6 | This demo shows how to draw an ellipse with |
|
9 | 9 | </gallery/shapes_and_collections/ellipse_collection>`. |
10 | 10 | """ |
11 | 11 |
|
12 | | -from typing import Tuple |
13 | | - |
14 | 12 | import matplotlib.pyplot as plt |
15 | | -import numpy as np |
16 | | - |
17 | 13 | from matplotlib.markers import MarkerStyle |
18 | 14 | from matplotlib.patches import Ellipse |
19 | 15 | from matplotlib.transforms import Affine2D |
20 | 16 |
|
21 | | -# %% |
22 | | -# |
23 | | -# A method to calculate the end points of the ellipse major, minor axis |
24 | | -# """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" |
25 | | -# |
26 | | -# Calculates the minor axis and major axis end points of the ellipse. |
27 | | -# It needs the ellipse parameter like width, height, angle. |
28 | | -# The output are 2 lists of 2 xy coordinates |
29 | | -# (minor((x0, y0), (x1, y1)), major((x0, y0), (x1, y1))). |
30 | | - |
31 | | - |
32 | | -def getMinorMajor(ellipse: Ellipse) -> Tuple[list, list]: |
33 | | - """ |
34 | | - Calculates the end points of minor and major axis of an ellipse. |
35 | | -
|
36 | | - Parameters |
37 | | - ---------- |
38 | | - ellipse : ~matplotlib.patches.Ellipse |
39 | | - Ellipse patch. |
40 | | -
|
41 | | - Returns |
42 | | - ------- |
43 | | - ~typing.Tuple[list, list] |
44 | | - """ |
45 | | - # Calculate the endpoints of the minor axis |
46 | | - x0_minor = ellipse.center[0] - ellipse.height / 2 * np.sin( |
47 | | - np.deg2rad(ellipse.angle) |
48 | | - ) |
49 | | - y0_minor = ellipse.center[1] + ellipse.height / 2 * np.cos( |
50 | | - np.deg2rad(ellipse.angle) |
51 | | - ) |
52 | | - x1_minor = ellipse.center[0] + ellipse.height / 2 * np.sin( |
53 | | - np.deg2rad(ellipse.angle) |
54 | | - ) |
55 | | - y1_minor = ellipse.center[1] - ellipse.height / 2 * np.cos( |
56 | | - np.deg2rad(ellipse.angle) |
57 | | - ) |
58 | | - |
59 | | - # Calculate the endpoints of the major axis |
60 | | - x0_major = ellipse.center[0] - ellipse.width / 2 * np.cos(np.deg2rad(ellipse.angle)) |
61 | | - y0_major = ellipse.center[1] - ellipse.width / 2 * np.sin(np.deg2rad(ellipse.angle)) |
62 | | - x1_major = ellipse.center[0] + ellipse.width / 2 * np.cos(np.deg2rad(ellipse.angle)) |
63 | | - y1_major = ellipse.center[1] + ellipse.width / 2 * np.sin(np.deg2rad(ellipse.angle)) |
64 | | - return [(x0_minor, y0_minor), (x1_minor, y1_minor)], [ |
65 | | - (x0_major, y0_major), |
66 | | - (x1_major, y1_major), |
67 | | - ] |
68 | | - |
69 | | - |
70 | | -# Define the ellipse |
| 17 | + |
| 18 | +# Create a figure and axis |
| 19 | +fig, ax = plt.subplots(1, 1, subplot_kw={"aspect": "equal"}) |
| 20 | + |
| 21 | +# Define an ellipse clockwise |
71 | 22 | center = (2, 4) |
72 | 23 | width = 30 |
73 | 24 | height = 20 |
74 | 25 | angle = 35 |
75 | | -ellipse = Ellipse( |
| 26 | +ellipse_clockwise = Ellipse( |
76 | 27 | xy=center, |
77 | 28 | width=width, |
78 | 29 | height=height, |
79 | 30 | angle=angle, |
80 | 31 | facecolor="none", |
81 | 32 | edgecolor="b", |
| 33 | + label="clockwise" |
82 | 34 | ) |
83 | 35 |
|
84 | | -minor, major = getMinorMajor(ellipse) |
85 | | - |
86 | | -# Create a figure and axis |
87 | | -fig, ax = plt.subplots(1, 1, subplot_kw={"aspect": "equal"}) |
| 36 | +# Get position of vertex for arrow marker |
| 37 | +vertices = ellipse_clockwise.get_co_vertices() |
88 | 38 |
|
89 | 39 | # Add the ellipse patch to the axis |
90 | | -ax.add_patch(ellipse) |
| 40 | +ax.add_patch(ellipse_clockwise) |
91 | 41 |
|
92 | 42 | # Plot a arrow marker at the end point of minor axis |
93 | | -t = Affine2D().rotate_deg(angle) |
| 43 | +t = Affine2D().rotate_deg(ellipse_clockwise.angle) |
94 | 44 | ax.plot( |
95 | | - minor[0][0], |
96 | | - minor[0][1], |
| 45 | + vertices[0][0], |
| 46 | + vertices[0][1], |
97 | 47 | color="b", |
98 | 48 | marker=MarkerStyle(">", "full", t), |
99 | | - markersize=10, |
| 49 | + markersize=10 |
100 | 50 | ) |
101 | 51 |
|
| 52 | +# Define an second ellipse counterclockwise |
| 53 | +center = (2, 4) |
| 54 | +width = 30 |
| 55 | +height = 15 |
| 56 | +angle = -20 |
| 57 | +ellipse_counterclockwise = Ellipse( |
| 58 | + xy=center, |
| 59 | + width=width, |
| 60 | + height=height, |
| 61 | + angle=angle, |
| 62 | + facecolor="none", |
| 63 | + edgecolor="g", |
| 64 | + label="counterclockwise" |
| 65 | +) |
| 66 | + |
| 67 | +# Add the ellipse patch to the axis |
| 68 | +ax.add_patch(ellipse_counterclockwise) |
| 69 | + |
| 70 | +# Get position of vertex for arrow marker |
| 71 | +vertices = ellipse_counterclockwise.get_co_vertices() |
| 72 | + |
| 73 | +# Plot a arrow marker at the end point of minor axis |
| 74 | +t = Affine2D().rotate_deg(ellipse_counterclockwise.angle) |
| 75 | +ax.plot( |
| 76 | + vertices[0][0], |
| 77 | + vertices[0][1], |
| 78 | + color="g", |
| 79 | + marker=MarkerStyle("<", "full", t), |
| 80 | + markersize=10 |
| 81 | +) |
| 82 | + |
| 83 | + |
| 84 | +plt.legend() |
102 | 85 | plt.show() |
103 | 86 |
|
| 87 | +center = (2, 4) |
| 88 | +width = 30 |
| 89 | +height = 20 |
| 90 | +angle = 35 |
| 91 | +ellipse = Ellipse( |
| 92 | + xy=center, |
| 93 | + width=width, |
| 94 | + height=height, |
| 95 | + angle=angle |
| 96 | +) |
| 97 | +print(ellipse.get_vertices()) |
| 98 | +print(ellipse.get_co_vertices()) |
104 | 99 |
|
105 | 100 | # %% |
106 | 101 | # |
|
0 commit comments