Isma Hène
Isma Hène

Reputation: 49

Draw the profile of a scatter plot

I have a scatter plot of Lc and Fc values (please, refer to 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)

I would like to draw the Fc profile of this scatter plot as shown in plot2. Does anyone have an efficient way to do it?

plot1

plot2

Upvotes: 1

Views: 215

Answers (1)

JohanC
JohanC

Reputation: 80409

Here is an idea drawing a Gaussian curve through each of the points and then take the maximum of these curves. You might want to experiment with the curve widths.

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

Here is an attempt to smooth out the sharp corners, which might or might not work with your data. The effect is only very local; trying to smooth out more resulted in also losing the general shape.

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

Upvotes: 1

Related Questions