Reputation: 103
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()
Upvotes: 0
Views: 508
Reputation: 9810
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:
Hope this helps.
Upvotes: 0