【问题标题】:scatter polar plot contour散点极坐标图等高线
【发布时间】:2017-07-16 21:43:45
【问题描述】:

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

`

【问题讨论】:

  • 我强烈建议您在帖子中添加matplotlib 标签。这样您更有可能获得所需的帮助。
  • 我不太确定我是否明白你想要什么。是否只想要一个点所在的“彩色区域”,即单色轮廓?
  • 这些点都聚集在一个“矩形”区域内。我想要一个单色线条轮廓,它是这个“矩形”区域的周长。我只是想在“矩形”区域的 4 个角处绘制点,但我仍然需要弄清楚如何将 4 个角与线连接起来,这样我就有了一个矩形来划定数据点所在的区域是。

标签: python matplotlib plot


【解决方案1】:

这是一个计算数据凸包的示例(我不知道应该如何计算矩形)并在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 很重要,否则多边形将绘制在点的顶部。生成的图形如下所示:

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多