Skip to main content
 首页 » 编程设计

python之Pandas /matplotlib : change tick label format (force plain not scientific)

2024年10月25日9arxive

我有一个 pandas dataframe,其中包含 2 列,格式如下:

df.head() 
                            column1  column2 
time                                                                         
1900-01-01 12:30:05.089883  19042.5  19042.8615    
1900-01-01 12:30:05.210055  19042.5  19042.8615    
1900-01-01 12:30:05.290165  19042.5  19042.8615    
1900-01-01 12:30:05.449856  19042.5  19042.8615    

我在同一 matplotlib 轴子图上绘制列

def plot(df): 
    ax = df.plot(y='column1', figsize=(20, 8)) 
    df.plot(y='column2', ax=ax) 
 
    # this doesn't make a difference 
    ax.ticklabel_format(style='plain', axis='y')     
 
    mpl.pyplot.show() 
 
plot(df) 

我想以 plain 格式打印 y 轴 - 如何强制它停止使用 scientific?

也就是说,y 轴从 0 到 6,并且有一个额外的标签 1.9037e4。我希望它从 19037 到 19043

我已经试过了,但没有什么不同:

ax.ticklabel_format(style='plain', axis='y')  

请您参考如下方法:

来自docs :

To reduce the chances that the ticklabels overlap the ticks are labeled as deltas from a fixed offset. For example:

ax.plot(np.arange(2000, 2010), range(10))

will have tick of 0-9 with an offset of +2e3. If this is not desired turn off the use of the offset on the default formatter:

ax.get_xaxis().get_major_formatter().set_useOffset(False)

set the rcParam axes.formatter.useoffset=False to turn it off globally, or set a different formatter.

所以,你可以这样写

 ax.get_yaxis().get_major_formatter().set_useOffset(False) 

关闭偏移。