13

when I tried to plot a timeseries with ggplot, the x axis lables became too crowded and overlapped each other:

screenshot1

The code is:

plot = ggplot(df, aes(x=df.index, weight='COUNT')) + \
    geom_bar() + \
    xlab('Date') + \
    ylab('Incidents') 

I tried to add the following line

+ theme(axis.text.x = element_text(angle = 90, hjust = 1))

to the plot, but it doesn't work. And this extra line gives me error:

SyntaxError: keyword can't be an expression
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

Any idea how this happened and how should I fix it? Thanks!!

1 Answer 1

11

(Old question, posting the answer if anyone comes across this in the future)

"axis.text.x" format is used for R. When using ggplot for python, replace "axis.text.x" with "axis_text_x"

This worked for me:

theme(axis_text_x  = element_text(angle = 90, hjust = 1))

Reference: https://github.com/yhat/ggplot/blob/master/ggplot/themes/theme.py

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

3 Comments

This rotates the tick labels for me, but it also changes them for integers from 0 to n, instead of the original labels.
Seriously. That whole change to integers thing makes this option a lot less functional. Anybody discover a workaround for this?
Converted the indices to integers. is there a way to map it back?

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.