0

I am importing data from a excel file in order to make a scatter polar plot of the data. The data are all gather in a specific area of the polar axes and instead of having a concentration of points (cf image below, blue points and code below), I would rather have a the contour the whole group of point.

Is there a method to do it in Python ? I have tried to use the method 'contourf' (cf stackover flow: Polar contour plot in matplotlib - best (modern) way to do it?). But I am getting stuck into it, my attempts to apply it have failed. Is there another method to plot contour of a group of points ?

Thank you !

`
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

Y. Luo
  • 5,622
  • 1
  • 18
  • 25
gus
  • 103
  • 8
  • I strongly recommend you add the `matplotlib` tag to your post. You're more likely to get the help you need that way. – saintsfan342000 Jul 16 '17 at 22:00
  • I'm not quite sure if I understand what you want. Do just want a 'coloured area' where the points are right now, i.e. a single-colour contour? – Thomas Kühn Jul 17 '17 at 06:46
  • The points are all gather inside a 'rectangular' area. I would like to have a single-colour line contour that is the perimeter of this 'rectangular' area. I just had the idea to plot the points at the 4 corners of the 'rectangular' area and I would still have to figure out how to link with line the 4 corners so that I have a rectangular shape that delimits the area where the data points are. – gus Jul 17 '17 at 11:49

1 Answers1

0

Here is an example that calculates the convex hull of the data (I didn't know how the rectangle should be calculated) and displays that convex hull with help of a PolyCollection. As I don't have your data, I generated some random data points, but it should be easy enough to adapt.

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

If you have the rectangle already calculated, you can just leave out the convex hull calculation and generate the PolyCollection from there. Note the **kwargs that I used to create the PolyCollection. Especially the zorder is important, as otherwise the polygon will be drawn on top of the points. The resulting figure looks like this:

resulting plot of the shown code

Hope this helps.

Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63