Skip to main content
 首页 » 编程设计

Python ggplot 格式轴数作为百分比不起作用

2024年11月01日8飞鱼

一直喜欢 Python 中新的 ggplot 模块,但我无法将我的 y 标签格式化为百分比而不是小数。下面的代码产生下面的图像。请注意,labels = 'percent' 代码不会生成预期的格式。

plot = ggplot(aes(x='Date', y='return', color='Stocks'),data=rx) +\ 
geom_line() +\ 
scale_x_date(breaks=date_breaks('1 day'), labels='%b %d %Y') +\ 
scale_y_continuous(labels= 'percent') +\ 
ylab('Cumulative Return') + ggtitle('S&P Sector Performance')  

请您参考如下方法:

现在可以在 0.5.3 版中使用(我刚刚推送了这个)。

>>> from ggplot import * 
>>> import numpy as np 
>>> import pandas as pd 
>>> df = pd.DataFrame({ "x": np.arange(0, 10), "y": np.arange(0, 1, 0.1) }) 
>>> ggplot(df, aes(x='x', y='y')) +\ 
... geom_point() +\ 
... scale_y_continuous(labels='percent')