简体   繁体   中英

How do I generate a scatter polar plot, from this function, Using the columns as my plot coordinates?

I want to create a polar graph of my angles (generated from a function, first column) against the sine values of those angles (generated from same function, second column). Few trial methods have produced plot but no points.

 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()

I'm hoping for a typical sine pattern on polar graphs, but I'm expected to draw it from my function.

Let's first simplify your function:

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)

The output is a 3D array of shape (1,72,3) . In order to plot it, you would need to flatten the slices.

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

plt.show()

在此输入图像描述

Now, this may or may not be what you're after, so let me suggest to modify the function as follows

  • use radiants instead of degrees
  • stack the arrays as columns to get a 2D array out
  • include 2 pi in the array

Code:

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()

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM