要在Bokeh中自定義軸標簽格式,可以使用FuncTickFormatter
類來定義要顯示的標簽格式。下面是一個示例代碼:
from bokeh.models import FuncTickFormatter
# 定義自定義的軸標簽格式函數(shù)
def custom_format_ticks(tick):
return f"${tick:.2f}"
# 創(chuàng)建一個x軸的FuncTickFormatter對象
x_axis_formatter = FuncTickFormatter(code="""
return custom_format_ticks(tick);
""")
# 創(chuàng)建一個y軸的FuncTickFormatter對象
y_axis_formatter = FuncTickFormatter(code="""
return custom_format_ticks(tick);
""")
# 設置x軸和y軸的軸標簽格式
plot.xaxis.formatter = x_axis_formatter
plot.yaxis.formatter = y_axis_formatter
在上面的示例代碼中,我們定義了一個名為custom_format_ticks
的函數(shù)來定義要顯示的格式。然后,我們創(chuàng)建了一個FuncTickFormatter
對象,并將其應用于x軸和y軸。最后,我們將自定義的格式函數(shù)應用于軸標簽。