2

我想在 plotnine 的一行中添加一个标签。使用 geom_text 时出现以下错误:

'NoneType' object has no attribute 'copy'

下面的示例代码:

df = pd.DataFrame({
    'date':pd.date_range(start='1/1/1996', periods=4*25, freq='Q'),
    'small': pd.Series([0.035]).repeat(4*25) ,
    'large': pd.Series([0.09]).repeat(4*25),
})


fig1 = (ggplot()
    + geom_step(df, aes(x='date', y='small'))
    + geom_step(df, aes(x='date', y='large'))
    + scale_x_datetime(labels=date_format('%Y')) 
    + scale_y_continuous(labels=lambda l: ["%d%%" % (v * 100) for v in l])
    + labs(x=None, y=None) 
    + geom_text(aes(x=pd.Timestamp('2000-01-01'), y = 0.0275, label = 'small'))
)

print(fig1)

编辑:

has2k1's answer below解决了错误,但我得到:

在此处输入图像描述

我想要这个:(来自R)

代码:

ggplot() + 
  geom_step(data=df, aes(x=date, y=small), color='#117DCF', size=0.75) +
  geom_step(data=df, aes(x=date, y=large), color='#FF7605', size=0.75) +
  scale_y_continuous(labels = scales::percent, expand = expand_scale(), limits = c(0,0.125)) +
  labs(x=NULL, y=NULL) +  
  geom_text(aes(x = as.Date('1996-01-07'), y = 0.0275, label = 'small'), color = '#117DCF', size=5)

在此处输入图像描述

https://plotnine.readthedocs.io/en/stable/index.html之外的任何文档?我已经阅读了 geom_text 那里,但仍然无法产生我需要的东西......

4

1 回答 1

5

geom_text没有数据框。如果要打印文本,请将其放在引号中,即 '"small"' 或将标签映射放在外面aes(),但使用起来更有意义annotate

(ggplot(df)
 ...
 # + geom_text(aes(x=pd.Timestamp('2000-01-01'), y = 0.0275, label = '"small"'))
 # + geom_text(aes(x=pd.Timestamp('2000-01-01'), y = 0.0275), label = 'small')
 + annotate('text', x=pd.Timestamp('2000-01-01'), y = 0.0275, label='small')
)
于 2019-11-27T10:45:37.987 回答