0
votes

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

3
What do you mean by "line similar to histogram"?Yevhen Kuzmovych
title should be changed to 'how to make a line trend chart'DaveR
@DaveR do you have solution in your mind for it.?user7930903

3 Answers

0
votes

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

0
votes

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

plt.hist(data, histtype='step')
plt.show()
0
votes

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