0

How can I modify the below code to colour age group 0-20 as orange and 60 above as red and Rest as yellow?

Below is the code:

Import matplotlib. pyplot as plt
dataset. Plot(Kind='bar', x='Patient_Age_Grp',y='Claim_amount', color='red') 
plt.xlabel('Age')
plt.ylabel('claimed amount') 
plt.show() 

Regard Newbee

1

1 Answer 1

0

I prefer to use matplotlib's direct function rather than using any other library's implementation of it like Pandas, as sometimes they don't function right (in case of twinx() for date axis for example). So I'd write your code like the following:

Import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.bar(dataset['Patient_Age_Grp'], dataset['Claim_amount'], 
       color = ['orange' if 20=>i>=0 else 'red' if i>=60 else 'yellow' for i in dataset['Patient_Age_Grp']]
       )
ax.set_xlabel('Age')
ax.set_ylabel('claimed amount') 
plt.show()

(P.S. Idk if those spaces in your code were because of your phone's autocorrection or not, just be wary of it)

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

Comments

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.