3

Could you help me to find out how to plot histogram with preaggregated data. What I mean is I have grouped data into bins to load them from SQL Server and save to xls file. Now I have two variables: frequency and bin variable (price). For example I have a bin 0 - 10 dollar price bin. There is 120 occurences in there. Then I have 10 - 20 dollar price bin, there is 500 occurences in there and so on.

The problem is that I have too much of that preaggregated bins. Because the price changes from 0 to 50 000 with a step of 10.

Could I somehow plot a histogram in pandas, that could automatically build histogram and treats each observation not as a single item but with already precalculated number of occurences.

Now I have histogram with 322 bins - I need to cut them with Python to 5 - 10:

tmdavison
  • 64,360
  • 12
  • 187
  • 165
User201801
  • 53
  • 5
  • Possible duplicate of [python plot simple histogram given binned data](http://stackoverflow.com/questions/12303501/python-plot-simple-histogram-given-binned-data) – tmdavison Mar 16 '16 at 13:55
  • No, just bar chart would not help me. Because I have 322 bins. See the image inside my post. I want to decrease number of bins – User201801 Mar 18 '16 at 11:08

1 Answers1

0

You can plot a barchart based on your data (using matplotlib):

import matplotlib.pyplot as plt

n, bins = your_data()

binwidth = 0.8 * (bins[1] - bins[0])

# you might not need this, if your bins are already the centervalue
bincenter = (bins[:-1] + bins[1:]) / 2.

plt.bar(bincenter, n, align='center', width=binwidth)
plt.show()
Tom Kooij
  • 21
  • 4
  • Thank you for your answer! I probably could do it with a bar plot, but I need to decrease the number of bins. I posted the plot I have now in Excel. – User201801 Mar 18 '16 at 11:07