簡體   English   中英

使用 Python 繪制蝴蝶曲線

[英]Plotting Butterfly Curve using Python

我一直在嘗試用 python 繪制蝴蝶曲線,這是我的代碼。

import math
r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r
r_of_theta=math.exp(np.sin(theta))-2*np.cos(4*theta)+sin((2*theta-np.pi)/24)**5
t_of_theta.astype(int)
ax = plt.subplot(projection='polar')
ax.plot(theta,t,color='blue')
ax.set_rticks([0.5, 1, 1.5, 2]) 
ax.set_rlabel_position(-22.5)
plt.show()

但我不斷收到此錯誤

TypeError                                 Traceback (most recent call last)
<ipython-input-66-876098af4358> in <module>()
      2 r = np.arange(0, 2, 0.01)
      3 theta = 2 * np.pi * r
----> 4 r_of_theta=math.exp(np.sin(theta))-2*np.cos(4*theta)+sin((2*theta-np.pi)/24)**5
      5 t_of_theta.astype(int)
      6 ax = plt.subplot(projection='polar')

TypeError: only size-1 arrays can be converted to Python scalars

有人可以幫我嗎? 提前致謝。

看,你有幾個打字錯誤。 你應該使用np. 到處。

import numpy as np
import matplotlib.pyplot as plt

r = np.arange(0, 2, 0.01)

theta = 2 * np.pi * r
r_of_theta = []
r_of_theta = np.exp(np.sin(theta[:]))-2*np.cos(4*theta[:])+np.sin((2*theta[:]-np.pi)/24)**5
r_of_theta.astype(int)

ax = plt.subplot(projection='polar')
ax.plot(r_of_theta,color='blue')
ax.set_rticks([0.5, 1, 1.5, 2]) 
ax.set_rlabel_position(-22.5)
plt.show()

math.exp處理單個值,並且您給它一個數組。 不過,您可以使用np.vectorize對函數進行矢量化:

import math
r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r
vec_exp = np.vectorize(math.exp)
r_of_theta=vec_exp(np.sin(theta))-2*np.cos(4*theta)+np.sin((2*theta-np.pi)/24)**5

現在您的代碼將至少運行到該行。

暫無
暫無

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

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