0

Can you please help me edit the index in my plots? I'm trying to rotate the names in the plot below so that it's a bit more readable.

Is there a way to prevent the "\n" from showing in the table?

Is there a way to label the y-axis?

In:

index = ("Plea \n Bargain \n Felony","Plea \n Bargain \n Misdemeanor") 

df2 = pd.DataFrame({ 
'incarceration': pd.Series((23.7,1.99), index),
'probation': pd.Series((45.0,45.9), index),
'work': pd.Series((2.4,0.3), index),
'program': pd.Series((12,0), index) 
}) 
print df2.head() 

df2.plot(kind='bar', stacked=True) 

Out

                            incarceration  probation  program  work
Plea \n Bargain \n Felony               23.70       45.0       12   2.4
Plea \n Bargain \n Misdemeanor           1.99       45.9        0   0.3

[2 rows x 4 columns]

Plot

1 Answer 1

1

The pandas plot functions return an axis, which you can then interact with to change properties like the labels:

ax = df2.plot(kind='bar', stacked=True)
ax.set_ylabel('Y Axis Label')
ax.set_xticklabels(df2.index, rotation='horizontal')

Result:

Updated figure

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

2 Comments

Thank you very much! --- anyway you know how to handle the "\n" funny business?
I think Marius answer with an axis solve most of your problems. You can handle "\n" problem by using below code after plotting. df2.index = pd.Series(df2.index).str.replace('\n', '').

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.