2017-07-16 160 views
0

我從excel文件導入數據以便繪製數據的散點極座標圖。數據全部收集在極軸的特定區域,而不是具有點集中點(參見下面的圖像,藍色點和下面的代碼),我寧願在整個點上有一個輪廓。分散極座標輪廓

有沒有一種方法可以在Python中完成它?我嘗試過使用方法'contourf'(比較stackover flow:Polar contour plot in matplotlib - best (modern) way to do it?)。但我陷入了困境,我嘗試應用它失敗了。還有另一種方法來繪製一組點的輪廓嗎?

謝謝!

` 
df = pd.read_excel('BW3/BW3StartValues.xlsx') 
fig = plt.figure() 
ax1 = fig.add_subplot(111, projection='polar')  
C = df.C 
h = df.h 
h = np.radians(h) # convert values of the angle from degrees to radians 
ax1.scatter(h,C, s = 5, marker = 'o', color='b') 
ax1.set_rmax(60) 
plt.show() 

` enter image description here

+0

我強烈建議您將'matplotlib'標記添加到您的帖子中。你更有可能以這種方式得到你需要的幫助。 – saintsfan342000

+0

我不太清楚,如果我明白你想要什麼。現在只需要一個「彩色區域」,即單色輪廓? –

+0

點都聚集在一個'矩形'區域內。我想要有一個單色線輪廓,就是這個「矩形」區域的周長。我只是有想法在「矩形」區域的四個角上繪製點,我仍然必須弄清楚如何將四個角連接起來,以便我有一個矩形形狀來劃定數據點的區域是。 – gus

回答

0

這裏是計算的數據(我不知道該矩形應如何計算)的凸包,並顯示凸包具有PolyCollection的幫助的例子。由於我沒有數據,我生成了一些隨機數據點,但它應該很容易適應。

from matplotlib import pyplot as plt 
import numpy as np 
from scipy.spatial import ConvexHull 
from matplotlib.collections import PolyCollection 


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

#generating some data: 
C = np.random.rand(30)*15+40 
h = np.random.rand(30)*15+290 
h = np.radians(h) # convert values of the angle from degrees to radians 

#following scipy example 
points = np.concatenate([h,C]).reshape((2,30)).T 
hull = ConvexHull(points) 

ax1.scatter(h,C, s = 5, marker = 'o', color='b') 

#adding the convex hull to ax1 as a PolyCollection: 
ax1.add_collection(PolyCollection(
    [points[hull.vertices,:]], 
    edgecolors='r', 
    facecolors='w', 
    linewidths=2, 
    zorder=-1, 
    )) 

ax1.set_rmax(60) 

plt.show() 

如果您已經計算出的矩形,你可以離開了凸包計算,並從那裏產生PolyCollection。請注意我用來創建PolyCollection**kwargs。特別是zorder是重要的,否則多邊形將被繪製在點的頂部。由此得出的數字是這樣的:

resulting plot of the shown code

希望這有助於。

+0

謝謝!它確實有幫助! – gus