Lets say I have a pandas Dataframe of cars prices and some features in other columns including heated seats. I'm struggling to plot the price of the cars in relation to if the car has heated seats (1==True,0==False). I've started with this so far which doesn't work properly.
cars[['Price','Heated Seats']].plot.hist(stacked=True)
I've been looking for a while and couldn't find a simple solution as well as a way to hist plot side by side bars.
Start by creating price points using cut
:
df = pd.DataFrame({'Price': np.random.randint(1000, size=100), 'Heated Seats': np.random.choice([1, 0, 0], size=100)})
df['Price Point'] = pd.cut(df.Price, bins=range(0, 1000, 100))
# Price Heated Seats Price Point
# 0 726 1 (700, 800]
# 1 257 1 (200, 300]
# .. ... ... ...
# 98 809 1 (800, 900]
# 99 668 1 (600, 700]
Then groupby
the price points and unstack
the value_counts
to plot the stacked bars:
df.groupby('Price Point')['Heated Seats'].value_counts().unstack().plot.bar(stacked=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.