3

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

asmgx
  • 7,328
  • 15
  • 82
  • 143

2 Answers2

7

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
3

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

gmds
  • 19,325
  • 4
  • 32
  • 58
  • Getting this error Out[77]: [ – asmgx Apr 08 '19 at 23:16
  • @asmgx it's not an error; you might just need to call `plt.show()` after that. – gmds Apr 08 '19 at 23:18
  • Thanks, Now it works for the example you posted but not for my data. the graph I get looks like this https://i.stack.imgur.com/Ng416.png – asmgx Apr 08 '19 at 23:23
  • @asmgx the reason for that is that you have some outliers in your data, which probably is why you passed `range=(0, 100)`. I will edit my code to rectify that. – gmds Apr 08 '19 at 23:25