Python ggplot 旋转轴标签
我在用ggplot画时间序列图的时候,发现x轴的标签太挤了,互相重叠在一起了:
我的代码是:
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))
但是没有效果。而且这行代码还给我报错:
SyntaxError: keyword can't be an expression
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
有没有人知道这是怎么回事,我该怎么解决呢?谢谢!!
1 个回答
11
(这是个旧问题,发这个答案是为了帮助将来遇到这个问题的人)
在R语言中,"axis.text.x"这个格式是用来设置图表的X轴文本的。而在Python的ggplot中,你需要把"axis.text.x"换成"axis_text_x"。
这个方法对我有效:
theme(axis_text_x = element_text(angle = 90, hjust = 1))
参考链接: https://github.com/yhat/ggplot/blob/master/ggplot/themes/theme.py