试图把标准的北极星改成更优雅的椭圆

Trying to change the standard polar star to a more elegant ellipse

我正在尝试在极坐标图中可视化环形空间中的测量压力。问题是我只有 6 个测量点,因此绘图是星形的,而由于液体中的垂直压力梯度,它应该更像椭圆形。请注意,它不一定是圆形,因为左右注射压力可能不同。

首先我尝试使用 matplotlib 的极坐标函数绘制图表,得到了一个星形图表。然后我尝试分散数据,但现在我无法通过数据点拟合椭圆。

loc_deg =(71, 11, 306, 234, 169, 109, 71) #  location of sensors 1, 2, 3, 4, 5, 6, 1 1 is repeated to complete the star/circle
loc_rad = np.radians(loc_deg) # use radians

P = (2.7269999999999999, 3.0019999999999998, 0.39800000000000002, 2.9729999999999999, 2.5099999999999998, 2.5609999999999999, 2.7269999999999999)

fig = plt.figure() 
ax = fig.add_subplot(111,projection = 'polar')

ax.set_ylim(0,10)
ax.set_xticks(loc_rad)

tv = ax.plot(loc_rad, P)# create star
lis = ax.scatter(loc_rad, P, color = '#ff7f00', marker = '.') # create scatter

现在我正在尝试通过散点图拟合一个椭圆,否则星星应该在椭圆中变化。

编辑 This is a plot of the three solutions, including the one proposed by Ardweaden

在尝试 Ardweadens 解决方案后,我意识到合身并不是我正在寻找的东西,因此我的问题并不清楚。我正在寻找一种方法来连接极地表面上的点,而不仅仅是一条直线。

例如:如果有 2 个测量点:1 个测量点为 1 度,1 个测量点为 179 度,这两个测量值均为 10。通过使用绘图函数,一条直线将显示几乎为 0 的值90 度,在 10 到 10 之间进行插值时,您会期望该值也为 10。所以更像是一个半圆。

刚拟合一个椭圆。

from scipy.optimize import curve_fit

def ellipse(phi,b,e,f):
    return b/(np.sqrt(1 - e * np.cos(phi + f)**2))

popt, pcov = curve_fit(ellipse, loc_rad, P)
x = np.arange(0,2*np.pi,0.01)

ax = fig.add_subplot(111,projection = 'polar')
ax.plot(x,ellipse(x,popt[0],popt[1],popt[2]))

对数据进行硬编码并绘制梯度:

loc_deg =(306, 234, 169, 109,71,11) #  location of sensors 1, 2, 3, 4, 5, 6, 1 1 is repeated to complete the star/circle
loc_rad = np.radians(loc_deg) # use radians

P = (0.39800000000000002, 2.9729999999999999, 2.5099999999999998, 2.5609999999999999,3.0019999999999998,2.7269999999999999)

fig = plt.figure() 
ax = fig.add_subplot(111,projection = 'polar')

ax.set_ylim(0,10)
ax.set_xticks(loc_rad)

def generate_fit(deg,P,deg_f=1):
    x,y = [],[]

    for i in range(1,len(P)):
        x.extend(np.arange(deg[i-1],deg[i],-1))
        incy = abs(deg[i]-deg[i-1])
        y.extend(np.linspace(P[i-1],P[i],incy))

    x.extend(np.arange(11,0,-1))
    x.extend(np.arange(360,306,-1))
    y.extend(np.linspace(P[-1],P[0],360-306 + 11))
    return np.radians(x),y

x,y = generate_fit(loc_deg,P)
ax.plot(x,y)

ax.scatter(loc_rad, P, color = '#ff7f00', marker = '.')