如何使用此列作为绘图坐标,通过此函数生成散布的极坐标图?

时间:2019-04-19 11:04:21

标签: python numpy matplotlib jupyter-notebook

我想创建我的角度(从函数,第一列生成)相对于这些角度(从同一函数,第二列生成)的正弦值的极坐标图。很少有试验方法可以得出情节,但没有要点。

 def GenerateTrigonometryTable(x):
A = np.arange (0,360,x) 
B = np.sin(A*np.pi/180)  
C = np.cos(A*np.pi/180)
D = []
F = []
G = []
for i in range (len(A)): 
    D.append(A[i])
for i in range (len(B)): 
    F.append(B[i])
for i in range (len(C)): 
    G.append(C[i])
table =([D],[F],[G]) 
table = np.dstack((table)) 
return (table) 

 Theta = (GenerateTrigonometryTable(5)[:,:,0])
 STheta = (GenerateTrigonometryTable(5)[:,:,1])
 ax1 = plt.subplot(111, projection='polar')
 ax1.plot(Theta, STheta)

 plt.show()
 plt.draw()

我希望在极坐标图上有一个典型的正弦图案,但是我希望从函数中绘制出它。

1 个答案:

答案 0 :(得分:0)

让我们首先简化您的功能:

def GenerateTrigonometryTable(x):
    A = np.arange (0,360,x) 
    B = np.sin(A*np.pi/180)  
    C = np.cos(A*np.pi/180)
    return np.dstack(([A],[B],[C])) 

t = GenerateTrigonometryTable(5)
print(t.shape)

输出是形状为(1,72,3)的3D数组。为了绘制它,您需要展平切片。

Theta = (GenerateTrigonometryTable(5)[:,:,0])
STheta = (GenerateTrigonometryTable(5)[:,:,1])
ax1 = plt.subplot(111, projection='polar')
ax1.plot(Theta.flatten(), STheta.flatten())

plt.show()

enter image description here

现在,这可能不是您想要的,所以让我建议如下修改功能

  • 使用辐射代替度数
  • 将数组堆叠为列以获取二维数组
  • 在数组中包含2个pi

代码:

import numpy as np
import matplotlib.pyplot as plt

def GenerateTrigonometryTable(x):
    A = np.deg2rad(np.arange(0,360+0.5,x))
    B = np.sin(A)  
    C = np.cos(A)
    return np.column_stack((A,B,C)) 

t = GenerateTrigonometryTable(5)
print(t.shape)

theta = (GenerateTrigonometryTable(5)[:,0])
stheta = (GenerateTrigonometryTable(5)[:,1])
ax1 = plt.subplot(111, projection='polar')
ax1.plot(theta, stheta)

plt.show()

enter image description here