0

I have a histogram which is being generated from one of the dataframe column. But I want to make line similar to histogram.

I tried to extract histogram data & plot a line chart but didn't worked.

data = [
    202.91,
    236.04,
    306.28,
    ...
    315.83,
    190.03
]

plt.hist(data)
plt.show()

This code generate a histogram, but I need a line chart which shows the trend

  • What do you mean by "line similar to histogram"? – Yevhen Kuzmovych Sep 04 '19 at 11:40
  • 1
    Possible duplicate of [Is there a clean way to generate a line histogram chart in Python?](https://stackoverflow.com/questions/27872723/is-there-a-clean-way-to-generate-a-line-histogram-chart-in-python) – PV8 Sep 04 '19 at 11:41
  • https://stackoverflow.com/questions/27872723/is-there-a-clean-way-to-generate-a-line-histogram-chart-in-python – PV8 Sep 04 '19 at 11:41
  • 1
    title should be changed to 'how to make a line trend chart' – DaveR Sep 04 '19 at 11:45
  • @DaveR do you have solution in your mind for it.? –  Sep 04 '19 at 12:29
  • @ragrwl gave the right answer. If this is what you wanted, the title should be 'how to make a line trend chart overlaying an histogram' or something like this – DaveR Sep 04 '19 at 13:07

3 Answers3

0

You can just use the histtype = 'step' option. This would be the easiest. In your specific example,

plt.hist(data, histtype='step')
plt.show()
ssm
  • 5,277
  • 1
  • 24
  • 42
0

This shows the step histogram line and a line plot on it passing from the center of each bin.

import numpy as np
data = np.random.randn(1000)
n, bins, patches = plt.hist(data, histtype=u'step')
plt.plot(bins[:-1]+(bins[1]-bins[0])/2, n)
plt.show()

enter image description here

JacoSolari
  • 1,226
  • 14
  • 28
0

Given that you are extracting data from a dataframe you could use pandas only as in the following example

%matplotlib inline
import pandas as pd
import numpy as np

df = pd.DataFrame({"data":np.random.randn(1000)})

ax = df["data"].plot.kde();
df['data'].hist(rwidth=0.8, density=True,ax=ax);

enter image description here

rpanai
  • 12,515
  • 2
  • 42
  • 64