Python ggplot旋转轴标签 [英] Python ggplot rotate axis labels
本文介绍了Python ggplot旋转轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试使用ggplot绘制时间序列时,x轴标签变得过于拥挤且彼此重叠:
when I tried to plot a timeseries with ggplot, the x axis lables became too crowded and overlapped each other:
代码是:
plot = ggplot(df, aes(x=df.index, weight='COUNT')) + \
geom_bar() + \
xlab('Date') + \
ylab('Incidents')
我尝试添加以下行
+ 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!!
推荐答案
(旧问题,如果将来有人遇到,请发布答案)
(Old question, posting the answer if anyone comes across this in the future)
"axis.text.x"格式用于R.将ggplot用于python时,将"axis.text.x"替换为"axis_text_x"
"axis.text.x" format is used for R. When using ggplot for python, replace "axis.text.x" with "axis_text_x"
这对我有用:
theme(axis_text_x = element_text(angle = 90, hjust = 1))
参考: https://github.com/yhat/ggplot/blob/master/ggplot/themes/theme.py
这篇关于Python ggplot旋转轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文