自定义蜘蛛图 --> 在 matplotlib 中的极坐标图上显示曲线而不是点之间的线

Custom Spider chart --> Display curves instead of lines between point on a polar plot in matplotlib

我已经测量了不同产品在不同角度位置的位置(在一个完整的旋转中以 60 度为步长的 6 个值)。我不想在 0 和 360 是同一点的笛卡尔图上表示我的值,而是想使用极坐标图。

使用 matplotlib,我得到了一张蜘蛛图类型的图表,但我想避免点之间的直线以及这些点之间的显示和外推值。我有一个不错的解决方案,但我希望有一个很好的 "one liner" 我可以用来对某些点进行更逼真的表示或更好的切线处理。

有没有人有改进我下面代码的想法?

# Libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# Some data to play with
df = pd.DataFrame({'measure':[10, -5, 15,20,20, 20,15,5,10], 'angle':[0,45,90,135,180, 225, 270, 315,360]})

# The few lines I would like to avoid...
angles = [y/180*np.pi for x in [np.arange(x, x+45,5) for x in df.angle[:-1]] for y in x]
values = [y for x in [np.linspace(x, df.measure[i+1], 10)[:-1] for i, x in enumerate(df.measure[:-1])] for y in x]
angles.append(360/180*np.pi)
values.append(values[0])

# Initialise the spider plot
ax = plt.subplot(polar=True)

# Plot data
ax.plot(df.angle/180*np.pi, df['measure'], linewidth=1, linestyle='solid', label="Spider chart")
ax.plot(angles, values, linewidth=1, linestyle='solid', label='what I want')
ax.legend()

# Fill area
ax.fill(angles, values, 'b', alpha=0.1)

plt.show()

结果如下,我想要类似于橙色线的东西,带有某种样条曲线以避免我目前得到的尖角

我有一个解决方案是其他解决方案的拼凑而成。它需要清理和优化,但它完成了工作!

随时欢迎提出意见和改进,见下文

# 

from scipy import interpolate

x=df.measure[:-1] * np.cos(df.angle[:-1]/180*np.pi)
y=df.measure[:-1] * np.sin(df.angle[:-1]/180*np.pi)
x = np.r_[x, x[0]]
y = np.r_[y, y[0]]

# fit splines to x=f(u) and y=g(u), treating both as periodic. also note that s=0
# is needed in order to force the spline fit to pass through all the input points.
tck, u = interpolate.splprep([x, y], s=0, per=True)

# evaluate the spline fits for 1000 evenly spaced distance values
xi, yi = interpolate.splev(np.linspace(0, 1, 1000), tck)

def cart2pol(x, y):
    rho = np.sqrt(x**2 + y**2)
    phi = np.arctan2(y, x)
    return(rho, phi)

# Initialise the spider plot
plt.figure(figsize=(12,8))
ax = plt.subplot(polar=True)

# Plot data
ax.plot(df.angle/180*np.pi, df['measure'], linewidth=1, linestyle='solid', label="Spider chart")
ax.plot(angles, values, linewidth=1, linestyle='solid', label='Interval linearisation')
ax.plot(cart2pol(xi, yi)[1], cart2pol(xi, yi)[0], linewidth=1, linestyle='solid', label='Smooth interpolation')
ax.legend()

# Fill area
ax.fill(angles, values, 'b', alpha=0.1)

plt.show()