Reputation: 33
ranges = (-0.4, -0.35, -0.3, -0.25, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15 ,0.2, 0.25, 0.3, 0.35, 0.4)
number_observations =
df.groupby(pandas.cut(df['price_variation'], ranges)).count()
matplotlib.pyplot.bar(x = ranges, y = number_observations)
I am sorry, relatively new to StackOverflow as well as Python. I have a database of 20.000 price_variation. Thanks to the second line of code I grouped them by a range, but I am unable to do display the result in a graph. Does someone understand the error message? Or have another proposition for me to display the result?
Thanks a lot for the help!!
Upvotes: 1
Views: 661
Reputation: 59549
y
is not a valid argumnt for bar
plots. You need to specify the height
. Since you have continuous bins with equal spacing you can specify the bin width and the edge alignment to make it so they appear as they should.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'price_variation': np.random.normal(0,0.12,10000)})
ranges = (-0.4, -0.35, -0.3, -0.25, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15 ,0.2, 0.25, 0.3, 0.35, 0.4)
number_observations = df.groupby(pd.cut(df['price_variation'], ranges)).count()
# Get the bins and alignment correct
plt.bar(x=ranges[:-1],
height=number_observations.price_variation.values,
width=np.diff(ranges)[0],
ec='k',
align='edge')
plt.show()
Upvotes: 2