散射极坐标图轮廓

时间:2017-07-16 21:43:45

标签: python matplotlib plot

我正在从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

1 个答案:

答案 0 :(得分: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。请注意我用于创建**kwargs的{​​{1}}。特别是PolyCollection很重要,否则多边形将被绘制在点的顶部。结果图看起来像这样:

resulting plot of the shown code

希望这有帮助。

相关问题