python bokeh 绘图如何格式化坐标轴显示

12 投票
2 回答
15369 浏览
提问于 2025-04-19 18:06

y轴的刻度似乎把数字像500000000格式化成了5.000e+8。有没有办法控制显示,让它显示成500000000?

我在使用Python 2.7和Bokeh 0.5.2。

我正在尝试Bokeh教程页面上的时间序列示例。

这个教程是把“调整后的收盘价”与“日期”进行绘图,但我是在把“成交量”与“日期”进行绘图。

2 个回答

12

你需要在你的代码中添加这个选项 p.left[0].formatter.use_scientific = False。在时间序列的教程中,应该这样写:

p1 = figure(title="Stocks")
p1.line(
    AAPL['Date'],
    AAPL['Adj Close'],
    color='#A6CEE3',
    legend='AAPL',
)

p1.left[0].formatter.use_scientific = False # <---- This forces showing 500000000 instead of 5.000e+8 as you want

show(VBox(p1, p2))
22

你还可以使用NumeralTickFormatter,就像下面这个简单的图表一样。除了'00',还有其他可以使用的值,具体可以在这里查看。

import pandas as pd
import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.models import NumeralTickFormatter

df = pd.DataFrame(np.random.randint(0, 90000000000, (10,1)), columns=['my_int'])
p = figure(plot_width=700, plot_height=280, y_range=[0,100000000000])
output_file("toy_plot_with_commas")

for index, record in df.iterrows():
    p.rect([index], [record['my_int']/2], 0.8, [record['my_int']], fill_color="red", line_color="black")

p.yaxis.formatter=NumeralTickFormatter(format="00")
show(p)

撰写回答