如何绘制半圆的方程式

How to plot the equation for a semicircle

我的半圈和我想象的不太一样。

我做的对还是我漏掉了一些重要的东西?

import math
import numpy as np
import matplotlib.pyplot as plt

coord_list = []


h = 0
k = 0
r = 6


for x in range((1 + h - r), (h + r - 1), 1):
    y1 = k + math.sqrt(r**2 - (x-h)**2)
    coord_list.append([x, y1])
   
    
for each in coord_list:
    print(each)

data = np.array([coord_list])

x, y = data.T

figure = plt.scatter(x, y)
figure = plt.grid(color = 'green', linestyle = '--', linewidth = 0.2)
figure = plt.show()

  • 使用 np.linspace 为 x 值创建一个数组。使用许多点来创建圆圈
  • 使用np.sqrt求解数组,而不是遍历每个值。
import numpy as np
import matplotlib.pyplot as plt

# function for semicircle
def semicircle(r, h, k):
    x0 = h - r  # determine x start
    x1 = h + r  # determine x finish
    x = np.linspace(x0, x1, 10000)  # many points to solve for y

    # use numpy for array solving of the semicircle equation
    y = k + np.sqrt(r**2 - (x - h)**2)  
    return x, y


x, y = semicircle(6, 0, 0)  # function call
plt.scatter(x, y, s=3, c='turquoise')  # plot
plt.gca().set_aspect('equal', adjustable='box')  # set the plot aspect to be equal

回答我自己的问题。

绘制代码的输出:

import math
import numpy as np
import matplotlib.pyplot as plt

coord_list = []


h = 0
k = 0
r = 6


for x in range((1 + h - r), (h + r - 1), 1):
    y1 = k + math.sqrt(r**2 - (x-h)**2)
    coord_list.append([x, y1])
   
    
for each in coord_list:
    print(each)

data = np.array([coord_list])

x, y = data.T

figure = plt.scatter(x, y)
figure = plt.grid(color = 'green', linestyle = '--', linewidth = 0.2)
figure = plt.show()



[-5, 3.3166247903554]
[-4, 4.47213595499958]
[-3, 5.196152422706632]
[-2, 5.656854249492381]
[-1, 5.916079783099616]
[0, 6.0]
[1, 5.916079783099616]
[2, 5.656854249492381]
[3, 5.196152422706632]
[4, 4.47213595499958]

看输出的坐标,好像不是圆

但是如果我们把方程和坐标绘制在这个网站上,我们会发现它们确实是一个圆。他们不是,这是一种错觉。部分原因是图表显示不均匀,还因为在步长为 1 的范围函数中绘制点(第 13 行)不会绘制彼此等弧长距离的点。