class hyperbola(): def __init__(self,ax_range=100,line_range=200): import matplotlib.pyplot as plt # self.a = a # self.b = b self.x = [] self.y = [] self.fig, self.ax = plt.subplots() self.plt = plt self.ax_range = ax_range self.line_range = line_range # self.focus = focus def hb_data(self,a,b,focus='x'): import math import numpy as np self.a, self.b, self.focus = a, b, focus line_range = self.line_range c = math.sqrt(a**2 + b**2) foucs_1 = [-c,0] foucs_2 = [c,0] x1 = list(np.linspace(-line_range,-a,line_range*10)) y = [] for x_ in x1: dot = math.sqrt(abs((b*x_/a)**2-b**2)) y.append(dot) y.append(-dot) x1 = x1 + x1[::-1] y.sort(reverse=True) x2 = list(np.linspace(a,line_range,line_range*10)) x2 = x2[::-1] + x2 return x1,y,x2 def hb_draw(self,data): ax = self.ax if self.focus == 'x': ax.plot(data[0], data[1], color='lightcoral', label='hyperbola') ax.plot(data[2], data[1], color='lightcoral') elif self.focus == 'y': ax.plot(data[1], data[0], color='lightcoral', label='hyperbola') ax.plot(data[1], data[2], color='lightcoral') else: print('You maked an error on focus!!!') self.decorate() def decorate(self): ax, ax_range = self.ax, self.ax_range ax.spines['top'].set_visible(False) #顶边界不可见 ax.xaxis.set_ticks_position('bottom') # ticks 的位置为下方,分上下的。 ax.spines['right'].set_visible(False) #右边界不可见 ax.yaxis.set_ticks_position('left') # 移动边界,按 Axes 的百分比位置 ax.spines['bottom'].set_position(('axes', 0.5)) ax.spines['left'].set_position(('axes', 0.5)) ax.set(title='Draw Hyperbola', xlim=[-ax_range,ax_range], ylim=[-ax_range,ax_range]) ax.legend() def asymptote(self): import numpy as np line_range, focus = self.line_range, self.focus x = list(np.linspace(-line_range,line_range,10)) #渐近线 y = [(self.b/self.a)*x_ for x_ in x] if self.focus == 'x': self.ax.plot(x, y, color='skyblue') self.ax.plot(x[::-1], y, color='skyblue') elif self.focus == 'y': self.ax.plot(y, x, color='skyblue') self.ax.plot(y, x[::-1], color='skyblue') def display(self): self.plt.show() if __name__ == '__main__': h = hyperbola(30,100) d1 = h.hb_data(10,13,focus='x') h.hb_draw(d1) h.asymptote() d2 = h.hb_data(10,13,focus='y') h.hb_draw(d2) h.asymptote() h.display()