简体   繁体   中英

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

在此处输入图片说明

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

在此处输入图片说明

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

Something like this

在此处输入图片说明

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

在此处输入图片说明

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM