Home > Mobile >  How can one make a code that graphs an ellipse based on user input using matplotlib
How can one make a code that graphs an ellipse based on user input using matplotlib

Time:10-23

I am a beginner in python and we currently have a required assessment that involves conic sections and matplotlib and I am trying to make a code that accepts user inputs(eg.The equation) and using those inputs it then outputs a graph of the input.

Here is my progress so far:

import sympy as sp
import matplotlib.pyplot as plt
import numpy as np
raw_expression = input("Enter the equation here:")
h = int(input("Please input the h value:"))
k = int(input("Please input the k value:"))
denominator1 = int(input("Please input the first denominator value:"))
denominator2 = int(input("Please input the second denominator value:"))
if denominator1 > denominator2:
    denominator1 = a_squared
    denominator2 = b_squared
else:
    denominator2 = a_squared
    denominator1 = b_squared
center_tuple = ()
vertice_tuple =()
center_tuple = center_tuple   (h,)
center_tuple = center_tuple   (k,)
a = sp.sqrt(a_squared)
b = sp.sqrt(b_squared)

vertice_tuple = vertice_tuple   (a,)
vertice_tuple = vertice_tuple   (b,)

x,y,z = sp.symbols('x,y,z',real=True)
expr = raw_expression
expr
def f(x,y): return (((x-h)**2)/denominator1)   (((y-k)**2)/denominator2) - 1
def calculate_points (cpoint, radius):
    h, k = cpoint
    a, b = radius
    
    print("Right-most point (", h   a, ",", k,")" )
    print("Left-most point (", h - a, ",", k,")" )
    print("Top-most point (", h ,",", k   b,")" )
    print("Bottom-most point (", h ,",", k - b,")" )
   
calculate_points(center_tuple,vertice_tuple)
Rmp= h   a
Lmp= h - a
Tmp= k   b
Bmp= k - b
Rmp= float(Rmp)
Lmp= float(Lmp)
Tmp= float(Tmp)
Bmp= float(Bmp)
x = np.linspace(Lmp,Rmp,1000)
y = np.linspace(Bmp,Tmp,1000)
x,y = np.meshgrid(x,y)
z = f(x,y)

fig, ax= plt.subplots()
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')

ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')



ax.contour(x,y,z,[0])


plt.scatter(h   a, k)
plt.scatter(h - a, k)
plt.scatter(h, k   b)
plt.scatter(h, k - b)
plt.axhline(0, alpha =.1)
plt.axvline(0, alpha=.1)



plt.show()

I am really troubled because it keeps spitting out errors and there was one time it worked but the graph was incorrect.

edit: Someone asked for an image of the expected output and it looks like this: enter image description here

An ellipse can be drawn in different ways, I'll leave to the OP to find the relationship between the parameter they used and what I am using in the following code.

In [126]: from matplotlib.pyplot import show, subplots
     ...: from numpy import cos, linspace, pi, sin
     ...: 
     ...: fig, ax = subplots(layout='constrained')
     ...: xc, yc = 0.5, -0.5
     ...: a, b = 2, 5
     ...: # Move the left and bottom spines to x = 0 and y = 0, respectively
     ...: ax.spines[["left"  ]].set_position(("data", 0))
     ...: ax.spines[["bottom"]].set_position(("data", 0))
     ...: # Hide the top and right spines.
     ...: ax.spines[["top", "right"]].set_visible(False)
     ...: ax.plot(1, 0, ">k", transform=ax.get_yaxis_transform(), clip_on=False)
     ...: ax.plot(0, 1, "^k", transform=ax.get_xaxis_transform(), clip_on=False)
     ...: ax.set_aspect(1)
     ...: 
     ...: θ = linspace(0, 2*pi, 629)
     ...: ax.plot(xc a*cos(θ), yc b*sin(θ))
     ...: ax.set_xlim((xc-1.5*a, xc 1.5*a)) ; ax.set_ylim((yc-1.5*b, yc 1.5*b))
     ...: for x, y, name in zip(
     ...:     (xc, xc a,   xc, xc-a,   xc,),
     ...:     (yc,   yc, yc b,   yc, yc-b,),
     ...:     'O A B C D'.split()):
     ...:     label = '%s=(%s,%s)'%(name,x,y)
     ...:     ax.scatter(x, y, label=label)
     ...: fig.legend()
     ...: show()
  • Related