gpt4 book ai didi

Python ggplot 旋转轴标签

转载 作者:太空狗 更新时间:2023-10-29 17:48:08 25 4
gpt4 key购买 nike

当我尝试使用 ggplot 绘制时间序列时,x 轴标签变得太拥挤并且彼此重叠:

screenshot1

代码是:

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

知道这是怎么发生的吗?我该如何解决?谢谢!!

最佳答案

(老问题,如果以后有人遇到这个问题,发布答案)

“axis.text.x”格式用于 R。在 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

关于Python ggplot 旋转轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23964236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com