簡體   English   中英

點集之間的平滑線

[英]Smooth line between set of points

我希望將值聯合起來的線條是平滑的圓線。 我嘗試了 wit interp1d,但似乎並沒有完全理解一維數組的要求。

from matplotlib import pyplot as plt

a = int(input("what number?\n"))

X_Values, Y_Values, fibonacci = [0] * a, [0] * a, [0] * a
X_Values[0], Y_Values [0] = 0, 0
X_Values[1], Y_Values[1] = 1, 1
fibonacci[0] = 1
fibonacci[1] = 1
counter = 2

for i in range(2,a):
   fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]
   if counter == 0:
       X_Values[i] = 0
       Y_Values[i] = -fibonacci[i]
       counter += 1
   elif counter == 1:
       X_Values[i] = fibonacci[i]
       Y_Values[i] = 0
       counter += 1
   elif counter == 2:
       X_Values[i] = 0
       Y_Values[i] = fibonacci[i]
       counter += 1
   elif counter == 3:
       X_Values[i] = -fibonacci[i]
       Y_Values[i] = 0
       counter = 0


plt.axhline(y=0, color='k') 
plt.axvline(x=0, color='k')
plt.plot(X_Values, Y_Values, '*-g')
plt.title("Flow Chart of a Fibonacci series with %i elements" %a)
plt.figtext(0.5, 0.025, "The {0}th number of the Fibonacci serie is: {1}".format(a, fibonacci[-1]), ha="center", fontsize=8, bbox={"facecolor":"orange", "alpha":0.5, "pad":5})
print("The ", a, "th Fibonacci number is: ", fibonacci[a-1])
plt.show()

有沒有辦法縮短 if elif 循環以將坐標分配給 X 和 Y _values?

要創建平滑插值,您可能可以使用Bézier splines 您可以通過添加X[i]X[i+1]以及Y[i]Y[i+1]來計算中間點。

使用圓弧看起來會更好。 起始角度將為-90, 0, 90, 180, 270, ... 結束角度將是開始角度加上 90。寬度和高度有點棘手,因為它們的角色不斷切換。 它要么是斐波那契數 i 和 i+1,要么相反。 i % 2(i+1) % 2將負責切換。

要使用更少的代碼進行計算,首先請注意不需要counter ,因為它只是i mod 4 旋轉方向可以存儲在兩個列表中, dir_xdir_y 此外,前兩個值可以通過 if-test ( if i < 2 ) 設置,因此它們可以對 x 和 y position 使用相同的分配。

from matplotlib import pyplot as plt
from matplotlib.patches import Arc

a = int(input("what number?\n"))

X_Values, Y_Values, fibonacci = [0] * a, [0] * a, [0] * a

dir_x = [0, 1, 0, -1]
dir_y = [-1, 0, 1, 0]
for i in range(0, a):
    if i < 2:
        fibonacci[i] = 1
    else:
        fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]
    X_Values[i] = dir_x[i % 4] * fibonacci[i]
    Y_Values[i] = dir_y[i % 4] * fibonacci[i]

fig, ax = plt.subplots()
for i in range(a - 1):
    ax.add_patch(Arc((0, 0), width=fibonacci[i + (i + 1) % 2] * 2, height=fibonacci[i + i % 2] * 2,
                     theta1=(i - 1) * 90, theta2=i * 90, color='crimson', lw=2))
# ax.relim() # needed to calculate the x and y limits if no lines are added to the plot
# ax.autoscale()
ax.axis('equal') # equal distances on x and on y
ax.axis('off') # hide the surrounding axes

ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')
ax.plot(X_Values, Y_Values, '*--g', lw=0.5)
ax.set_title("Flow Chart of a Fibonacci series with %i elements" % a)
ax.text(0.5, -0.05, f"The {a}th number of the Fibonacci serie is: {fibonacci[-1]}", ha="center",
        fontsize=8, bbox={"facecolor": "orange", "alpha": 0.5, "pad": 5}, transform=ax.transAxes)
print("The ", a, "th Fibonacci number is: ", fibonacci[a - 1])

plt.tight_layout()
plt.show()

示例圖

PS:創建弧的代碼可以使用它的angle參數進一步簡化。 angle是弧線的額外旋轉,它消除了交替寬度和高度的需要。

ax.add_patch(Arc((0, 0), width=fibonacci[i] * 2, height=fibonacci[i + 1] * 2,
                 angle=(i - 1) * 90, theta1=0, theta2=90, color='crimson', lw=2))

繪制二次貝塞爾曲線的代碼:

from matplotlib.patches import PathPatch, Path

x, y = X_Values, Y_Values
for i in range(a - 1):
    ax.add_patch(PathPatch(
        Path([(x[i], y[i]), (x[i] + x[i + 1], y[i] + y[i + 1]), (x[i + 1], y[i + 1])],
             [Path.MOVETO, Path.CURVE3, Path.CURVE3]),
        fc="none", ec='navy', transform=ax.transData))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM