绘制散点图的轮廓

时间:2021-04-07 09:57:11

标签: python python-3.x numpy matplotlib scatter-plot

我有一个 Lc 和 Fc 值的散点图(请参考 plot1)。

Lc= [360.66832393 388.26294316 392.9410819  ... 384.31751584 403.52581547
 384.22929343]

Fc= [77.3294787  47.5926941  44.53032575 ... 50.44012265 38.99666318
 50.54763385]

plot.scatter(Lc, Fc)

我想绘制这个散点图的 Fc 曲线,如图 2 所示。有没有人有有效的方法来做到这一点?

plot1

plot2

1 个答案:

答案 0 :(得分:1)

这里有一个想法,通过每个点绘制一条高斯曲线,然后取这些曲线的最大值。您可能想要试验曲线宽度。

import matplotlib.pyplot as plt
import numpy as np

low_lim = 30
fc = np.random.rand(120) * np.random.rand(120) * 120
fc = fc[fc > low_lim]
lc = np.random.uniform(50, 250, len(fc))

x = np.linspace(0, 300, 5000)
sigma = 15
ys = np.exp(- np.power((x.reshape(-1, 1) - lc) / sigma, 2) / 2) * fc
ymax = ys.max(axis=1)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(15, 4))
for ax in (ax1, ax2):
    if ax == ax1:
        ax.plot(x, ymax, color='black', ls=':', lw=3)
        for l, f, y in zip(lc, fc, ys.T):
            ax.plot(x, y)
            ax.fill_between(x, 0, y, color='r', alpha=0.05)
    else:
        ax.plot(x, ymax, color='black', lw=2)
        ax.fill_between(x, 0, ymax, color='r', alpha=0.2)
    ax.scatter(lc, fc, color='darkorange')
    ax.axhline(low_lim, ls='--', color='skyblue')
    ax.set_ylim(ymin=0)
    ax.margins(x=0)
plt.tight_layout()
plt.show()

Gaussians through points and taking maximum

这里尝试平滑尖角,这可能适用于您的数据,也可能不适用于您的数据。效果只是非常局部;试图平滑更多导致也失去了总体形状。

from scipy.special import softmax

ys = np.exp(- np.power((x.reshape(-1, 1) - lc) / sigma, 2) / 2) * fc
softmax_weights = softmax(np.power(ys, 0.8), axis=1)
ymax = np.sum(ys * softmax_weights, axis=1)

smoothed out maximum of corners

相关问题