0

I have an imported excel file in python and want to create a bar chart. In the bar chart, I want the bars to be separated by profit, 0-10, 10-20, 20-30... How do I do this?

this is one of the things I have tried:

import NumPy as np
import matplotlib.pyplot as plt
%matplotlib inline  
df.plot(kind="bar",x="profit", y="people")
df[df.profit<=10]
plt.show()

and:

df[df.profit range (10,20)]
Thomas Schillaci
  • 2,348
  • 1
  • 7
  • 19
TUB
  • 1
  • 1
    Can you please provide a minimal, reproducible example (https://stackoverflow.com/help/minimal-reproducible-example) to help you better? – Kim Tang Mar 11 '20 at 13:15

1 Answers1

0

It is a bit difficult to help you better without a sample of your data, but I constructed a dataset randomly that should have the shape of yours, so that this solution can hopefully be useful to you:

import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd

# For random data
import random
%matplotlib inline

df = pd.DataFrame({'profit':[random.choice([i for i in range(100)]) for x in range(100)], 'people':[random.choice([i for i in range(100)]) for x in range(100)]})
display(df)

out = pd.cut(df['profit'], bins=[x*10 for x in range(10)], include_lowest=True)
ax = out.value_counts(sort=False).plot.bar(rot=0, color="b", figsize=(14,4))
plt.xlabel("Profit")
plt.ylabel("People")
plt.show()

I had a look at another question on here (Pandas bar plot with binned range) and there they explained how this issue can be solved.

Hope it helps :)

Kim Tang
  • 2,330
  • 2
  • 9
  • 34