Skip to content

Commit 8b4bdbe

Browse files
committed
ellipse code usiing def
1 parent 26a45cb commit 8b4bdbe

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

ellipse_def_plot.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Plot parbolas of the form (x^2/a^2) + (y^2/b^2) = 1
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
# Function to plot Ellipse
5+
def plot_ellipse(a, b, ax):
6+
x = np.linspace(-10, 10, 400)
7+
y1 = b * np.sqrt(1 - (x**2 / a**2))
8+
y2 = -b * np.sqrt(1 - (x**2 / a**2))
9+
ax.plot(x, y1, 'r', label=f'ellipse: (x^2/{a}^2) + (y^2/{b}^2) = 1')
10+
ax.plot(x, y2, 'r')
11+
# Create figure and axis
12+
fig, ax = plt.subplots()
13+
# Set aspect of the plot to be equal
14+
ax.set_aspect('equal')
15+
# Set limits
16+
ax.set_xlim(-10, 10)
17+
ax.set_ylim(-10, 10)
18+
# Plot ellipse with a=8, b=4
19+
plot_ellipse(8, 4, ax)
20+
# Add labels and title
21+
ax.set_xlabel('X-axis')
22+
ax.set_ylabel('Y-axis')
23+
plt.title('Ellipse Plot')
24+
# Add grid and highlight X and Y axes
25+
ax.grid(True)
26+
ax.axhline(0, color='yellow', linewidth=0.5)
27+
ax.axvline(0, color='yellow', linewidth=0.5)
28+
plt.show()
29+
# Save the plot
30+
plt.savefig('ellipse_def_plot.png')

0 commit comments

Comments
 (0)