3

I know that it's possible to create a histogram from a pandas dataframe column with matplotlib.pyplot using the code:

df.plot.hist(y='Distance')

Which creates a graph like this:

Graph one

However what I'm looking for is a plot of relative frequency, expressed as a percentage of the total. I'd also like for the graph to have an overflow bin at 300 so that it looks something along the lines of: Graph two

1 Answer 1

3

Try this:

orders = [{'number': 1029,'brand':'XPTO','qty':50},
      {'number': 3233,'brand':'ABCD','qty':50},
      {'number': 5455,'brand':'XPTO','qty':50},
      {'number': 1234,'brand':'ABCD','qty':50},
      {'number': 7654,'brand':'TXWZ','qty':50},
      {'number': 8765,'brand':'XPTO','qty':50},
      {'number': 4354,'brand':'TXWZ','qty':50},
      {'number': 9089,'brand':'XPTO','qty':50},
      {'number': 1031,'brand':'XPTO','qty':50}]
orders_df = pd.DataFrame(orders)
series = orders_df['brand'].value_counts() / len(orders_df)
indx = [0,1,2]
plt.bar(indx, series*100)
plt.ylabel('%')
plt.title('Relative fequency')
plt.xticks(indx, series.index)

Relative frequency

Sign up to request clarification or add additional context in comments.

2 Comments

You can use series = orders_df['brand'].value_counts(True) which normalizes automatically without having to divide by len(orders_df)
since you *100, these are now percentage frequencies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.