簡體   English   中英

繪制散點圖 plot

[英]Draw the profile of a scatter plot

我有 Lc 和 Fc 值的散點 plot(請參閱 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)

我想繪制這個散點圖 plot 的 Fc 曲線,如圖 2 所示。 有沒有人有一個有效的方法來做到這一點?

情節1

情節2

這是一個想法,通過每個點繪制一條高斯曲線,然后取這些曲線中的最大值。 您可能想試驗曲線寬度。

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

高斯通過點並取最大值

這是一種消除尖角的嘗試,它可能適用於您的數據,也可能不適用於您的數據。 效果只是非常局部的; 試圖平滑更多導致也失去了一般形狀。

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)

平滑了最大的角落

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM