【问题标题】:Python ggplot rotate axis labelsPython ggplot旋转轴标签
【发布时间】:2014-07-20 18:46:51
【问题描述】:

当我尝试使用 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

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

【问题讨论】:

    标签: python python-ggplot


    【解决方案1】:

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

    “axis.text.x”格式用于R。当使用ggplot for python时,将“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

    【讨论】:

    • 这会为我旋转刻度标签,但它也会将它们更改为从 0 到 n 的整数,而不是原始标签。
    • 说真的。对整数的整个更改使此选项的功能大大降低。有人找到解决方法吗?
    • 将索引转换为整数。有没有办法把它映射回来?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2016-10-28
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多