I am trying to create a histogram for each rolling window across a DataFrame. The rolling function in Python (df.WaveData.rolling(14).mean()) can be used for calculating sum or average, but how can we use it to plot histogram for data in each window?
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 1000)
y = np.sin(x)
plt.plot(x, y)
plt.show()
df = pd.DataFrame(y, columns=['WaveData'])
print(df)
print(df.WaveData.rolling(14).mean())
**Ideal**:
for data in window:
histogram(data_in_window)
n, edges = np.histogram(data, bins=25)
CodePudding user response:
Here you go:
import matplotlib.pyplot as plt
import pandas as pd
# generate random dataframe
df = pd.DataFrame(np.random.randint(0,1000,size=(1000, 4)), columns=list('ABCD'))
window_size = 100
for i in range(len(df.A.values)):
window = df.A.values[i:i window_size]
n, bins, patches = plt.hist(window, 25)
plt.show()