Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Histogram to curve in Python

I have this code

df1 = df['T1'].values

df1 = df1 [~np.isnan(df1 )].tolist()

plt.hist(df1 , bins='auto', range=(0,100))
plt.show()

Which gives me this graph

enter image description here

and this code

df2 = df['T2'].values

df2 = df2 [~np.isnan(df2 )].tolist()

plt.hist(df2 , bins='auto', range=(0,100))
plt.show()

which gives me this

enter image description here

Is there any way I can convert Histograms to Curves and then combine them together?

Something like this

enter image description here

like image 465
asmgx Avatar asked Oct 24 '25 04:10

asmgx


2 Answers

Possibly, you want to draw steps like this

import numpy as np
import matplotlib.pyplot as plt

d1 = np.random.rayleigh(30, size=9663)
d2 = np.random.rayleigh(46, size=8083)

plt.hist(d1 , bins=np.arange(100), histtype="step")
plt.hist(d2 , bins=np.arange(100), histtype="step")
plt.show()

enter image description here

like image 147
ImportanceOfBeingErnest Avatar answered Oct 26 '25 02:10

ImportanceOfBeingErnest


You can use np.histogram:

import numpy as np
from matplotlib import pyplot as plt

fig, ax = plt.subplots()

chisq = np.random.chisquare(3, 1000)
norm = np.random.normal(10, 4, 1000)

chisq_counts, chisq_bins = np.histogram(chisq, 50)
norm_counts, norm_bins = np.histogram(norm, 50)

ax.plot(chisq_bins[:-1], chisq_counts)
ax.plot(norm_bins[:-1], norm_counts)

plt.show()

In the specific case of your data, which has outliers, we will need to clip it before plotting:

clipped_df1 = np.clip(df1, 0, 100)
clipped_df2 = np.clip(df2, 0, 100)

# continue plotting

enter image description here

like image 22
gmds Avatar answered Oct 26 '25 00:10

gmds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!