Skip to content

Commit ef61aab

Browse files
committed
hyperbola plot code
1 parent 0faf55e commit ef61aab

4 files changed

Lines changed: 44 additions & 9 deletions

File tree

circles_plot.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import matplotlib.pyplot as plt
33
import numpy as np
44
# function to draw a circle
5+
center = tuple(map(float, input("Enter the center coordinates as x,y: ").split(',')))
6+
radius = float(input("Enter the radius of the circle: "))
57
def draw_circle(ax, center, radius, color):
68
circle = plt.Circle(center, radius, color=color, fill=False, linewidth=1)
79
ax.add_artist(circle)
@@ -11,20 +13,20 @@ def draw_circle(ax, center, radius, color):
1113
# set aspect of the plot to be equal
1214
ax.set_aspect('equal')
1315
# set limits
14-
ax.set_xlim(-5, 5)
15-
ax.set_ylim(-5, 5)
16-
# draw circles
17-
draw_circle(ax, (0, 0), 4, 'blue')
16+
ax.set_xlim(-10, 10)
17+
ax.set_ylim(-10, 10)
18+
# draw circle with user input
19+
draw_circle(ax, center, radius, 'blue')
1820
# add labels and title
1921
ax.set_xlabel('X-axis')
2022
ax.set_ylabel('Y-axis')
21-
ax.plot(0, 0, 'ro') # mark center
23+
ax.plot(center[0], center[1], 'ro') # mark center
2224
# add grid and highlight X and Y axes
2325
ax.axhline(0, color='yellow',linewidth=0.5, ls='--')
2426
ax.axvline(0, color='yellow',linewidth=0.5, ls='--')
2527
ax.grid(color = 'gray', linestyle = '--', linewidth = 0.5)
2628
#add title
27-
plt.title('Circle with radius 4 centered at (0,0)')
29+
plt.title(f'Circle with radius {radius} centered at {center}')
2830
plt.show()
2931
# save the plot
3032
plt.savefig('circle_plot.png')

ellipse_def_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def plot_ellipse(a, b, ax):
1717
# Set limits
1818
ax.set_xlim(-10, 10)
1919
ax.set_ylim(-10, 10)
20-
# Plot ellipse with a=8, b=4
20+
# Plot ellipse with a and b from user input
2121
plot_ellipse(a, b, ax)
2222
# Add labels and title
2323
ax.set_xlabel('X-axis')

hyperbola_plot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import numpy as np
33
import matplotlib.pyplot as plt
44
# Function to plot hyperbola
5+
a = int(input("Enter the value of a: "))
6+
b = int(input("Enter the value of b: "))
57
def plot_hyperbola(a, b, ax):
68
x = np.linspace(-10, 10, 400)
79
y1 = b * np.sqrt((x**2 / a**2) - 1)
@@ -15,8 +17,8 @@ def plot_hyperbola(a, b, ax):
1517
# Set limits
1618
ax.set_xlim(-10, 10)
1719
ax.set_ylim(-10, 10)
18-
# Plot hyperbola with a=5, b=3
19-
plot_hyperbola(5, 3, ax)
20+
# Plot hyperbola with a and b from user input
21+
plot_hyperbola(a, b, ax)
2022
# Add labels and title
2123
ax.set_xlabel('X-axis')
2224
ax.set_ylabel('Y-axis')

parabola_plot.py

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

0 commit comments

Comments
 (0)