u/Additional_Jicama622

Help me with this Python code
▲ 0 r/ImageJ

Help me with this Python code

How can I remove the yellow area overflowing from some bins? This is the graph and the generatind Python code underneath.

https://preview.redd.it/vp0ye5z74k0h1.png?width=1175&format=png&auto=webp&s=dcb030ed81c28b7cbc3e21ef795421c023d88c0c

import math

import numpy as np

import matplotlib.pyplot as plt

from scipy.optimize import curve_fit

from matplotlib.backends.backend_pdf import PdfPages

from matplotlib.ticker import AutoMinorLocator

from matplotlib.ticker import MultipleLocator

from scipy.stats import norm

%matplotlib inline

# ISTOGRAMMA

istogramma = np.loadtxt("20h.txt")

x = istogramma[:, 0] # centroidi

y = istogramma[:, 1] # conteggi

plt.figure(figsize = (6, 6))

plt.bar(x, y / 1000, width = 1,

color = 'none', edgecolor = 'none', alpha = 0.5)

plt.bar(x[303:320], y[303:320] / 1000, width = 1,

color = 'skyblue', edgecolor = 'none', alpha = 0.6, zorder = 1)

plt.bar(x[379:385], y[379:385] / 1000, width = 1,

color = 'skyblue', edgecolor = 'none', alpha = 0.6, zorder = 1)

plt.title('Spettro gamma da 20 ore\n', fontsize = 16)

plt.xlabel('Canali', fontsize = 14)

plt.ylabel('Conteggi ($\\times$10\u00B3)', fontsize = 14)

plt.xlim(294, 396)

plt.ylim(4, 9)

m = np.linspace(0, 4096, 4096)

b = -0.0425039 # pendenza

a = 21.1643 # intercetta

n = b * m + a

plt.plot(m, n, label = "Regressione lineare", color = 'crimson', linewidth=2, zorder = 3)

plt.step(x, y / 1000, where = 'mid', color = 'black', linewidth = 1)

mask1 = (m >= 321) & (m < 380)

# intervallo desiderato

mask = (x >= 321) & (x <= 380)

x_sel = x[mask]

y_sel = y[mask] / 1000

# bordi reali dei bin

left_edges = x_sel - 0.5

right_edges = x_sel + 0.5

# numero di punti per bin

N = 100

# array espansi

x_dense = []

y_dense = []

for l, r, val in zip(left_edges, right_edges, y_sel):

xd = np.linspace(l, r, N, endpoint=False)

x_dense.extend(xd)

y_dense.extend([val] * len(xd))

x_dense = np.array(x_dense)

y_dense = np.array(y_dense)

# retta liscia sugli stessi punti

n_dense = b * x_dense + a

# fill

plt.fill_between(x_dense,n_dense,y_dense,where=(y_dense >= n_dense),color='red',alpha=0.5,zorder=2)

plt.fill_between(x_dense, n_dense, 0, color = 'orange', alpha = 0.5, zorder = 1)

plt.axvline(x = 304, color = 'blue', linestyle = '--', alpha = 0.5)

plt.axvline(x = 380, color = 'blue', linestyle = '--', alpha = 0.5)

plt.axvline(x = 321, color = 'blue', linestyle = '--', alpha = 0.5)

plt.axvline(x = 386, color = 'blue', linestyle = '--', alpha = 0.5)

plt.grid(True, alpha = 0.3)

#plt.legend(fontsize = 14)

plt.tight_layout()

plt.show()

reddit.com
u/Additional_Jicama622 — 3 days ago