绘制散点图的轮廓

2024-06-15 08:48:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有Lc和Fc值的散点图(请参考图1)

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


Tags: 方法plot绘制lcfc剖面scatter
1条回答
网友
1楼 · 发布于 2024-06-15 08:48:56

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

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

相关问题 更多 >